How to deploy a Strapi API on Ubuntu 16.04
Strapi is a Node.js Content Management Framework dedicated to API creation. Halfway between a Node.js Framework and a Headless CMS, it has been designed to build APIs in seconds instead of weeks thanks to its built-in features: Admin Panel, Authentication & Permissions management, Content Management, API Generator, etc.
In this tutorial, you will learn how to deploy a Strapi v3 project for production on a clean Ubuntu server.
First of all, you will need the following a clean Ubuntu 16.04 server, configured with
git installed on it a non-root user having the
A Strapi app is nothing else than a Node.js application. Obviously, it requires Node.js (8 or higher).
Run the following commands to install Node.js:
$ curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash - $ sudo apt-get install -y nodejs
Check that Node has been successfully installed:
$ node -v
It should print
Strapi uses MongoDB as default database system. Except if you decided to use an other database system or host the MongoDB database on an other server or service (what we strongly recommand), you have to install MongoDB on the Ubuntu server.
Import the public key:
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
Create a list file:
$ echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
Reload local package database:
$ sudo apt-get update
Install MongoDB packages:
$ sudo apt-get install -y mongodb-org
$ sudo service mongod start
If you have any issue here, give a look at the MongoDB documentation.
Note: by default, MongoDB is automatically accessible from outside through the port 27017. We highly advise you to restrict access by adjusting your Firewall rules.
Setup your project
Node.js and MongoDB are installed. It’s time to import our Strapi project.
Move to your user’s home directory and clone a Strapi project from a GitHub (or GitLab, BitBucket…) repository.
Clone your project:
$ cd ~ $ git clone https://github.com/strapi/sample-strapi-app
Except if you want to get a clean Strapi app, replace the URL above by the one of your project.
Note: if your project is hosted in a private repository, the terminal may prompt your credentials (email and password).
$ cd sample-strapi-app $ npm install --production
Start the server to make sure everything is going well:
$ NODE_ENV=production npm start
NODE_ENV=production informs Strapi that the app is running in production mode. The configuration files taken in consideration are the ones located in
Then, stop the server by running
ctrl + c.
As you may have seen, if you quit the
npm start script or exit the SSH connection, the Node.js process is stopped. This is quite annoying. Also, we want to be sured the app will be automatically restarted if it crashes.
This is where PM2, the de-facto process manager for Node.js, comes at the rescue.
$ npm install pm2 -g
Note: if you encounter npm permissions issues, change the permissions to npm default directory or use
Start your Strapi server:
$ NODE_ENV=production pm2 start server.js --name api
List the processes:
$ pm2 list
To make sure your API is running is running well, check out the logs:
$ pm2 logs api
c to leave the logs view.
Note: take a look at the PM2 documentation for a more advanced usage.
Your application is now up and running on port
1337. To make it accessible on the web port (80) you need install a reverse proxy.
For this tutorial, we are going to use nginx: the most famous HTTP and reverse proxy server.
$ sudo apt-get update $ sudo apt-get install -y nginx
Open the main configuration file:
$ sudo nano /etc/nginx/sites-available/default
Next, replace the content in the
location / directive by the following:
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
$ sudo systemctl restart nginx
At this point, your Strapi API should be accessible at the following URL:
Congratulations, you successfully deployed a Strapi application on a clean Ubuntu server!
This tutorial covers the basic parts to deploy a Strapi application. We strongly advise you take the following points in high consideration: security (open ports, IP restrictions, etc.), SSL, auto-scaling, etc.
To make your life even easier, we published a complete bash script to setup your environment. This script installs Node.js, Strapi, PM2, MongoDB and nginx (correctly configured). To try it, run the following command:
wget -qO - https://strapi.io/install.sh | sudo -E bash && source ~/.bashrc on a clean Ubuntu server. After running it, the only thing you need do is to setup your own Strapi project or create a new one through the CLI.
In the next few weeks, we will publish other articles relating to Strapi APIs deployements: Deploy a Strapi on Heroku, Using mLab with Strapi, Using Strapi with Docker, Monitor a Strapi API with Keymetrics, etc.
What tutorials would you like next?
Originally published at blog.strapi.io on January 31, 2018.