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.

Hosting a Strapi project is an important mission during both development and production phases. However, as developer, you are probably more an expert in JavaScript than DevOps. Don't worry, this article is here to help you.

In this tutorial, you will learn how to deploy a Strapi v3 project for production on a clean Ubuntu server.

Prerequisites

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 sudo privileges.

Install Node.js

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 v9.x.x.

Install MongoDB

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

Start MongoDB:

$ 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).

Install dependencies:

$ cd sample-strapi-app
$ npm install --production

Start the server to make sure everything is going well:

$ NODEENV=production npm start

Note: NODEENV=production informs Strapi that the app is running in production mode. The configuration files taken in consideration are the ones located in config/environments/production.

Your Strapi API should be accessible at the following URL: http://yourIP:1337.

Then, stop the server by running ctrl + c.

Install PM2

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.

Install PM2:

$ npm install pm2 -g

Note: if you encounter npm permissions issues, change the permissions to npm default directory or use sudo.

Start your Strapi server:

$ NODE_ENV=production pm2 start server.js --name api

Your Strapi API should be accessible at the following URL: http://yourIP:1337.

List the processes:

$ pm2 list

To make sure your API is running is running well, check out the logs:

$ pm2 logs api

Press crtl + c to leave the logs view.

Note: take a look at the PM2 documentation for a more advanced usage.

Install nginx

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.

Install it:

$ 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:

proxypass http://localhost:1337; proxyhttpversion 1.1; proxysetheader Upgrade $httpupgrade; proxysetheader Connection 'upgrade'; proxysetheader Host $host; proxycachebypass $http_upgrade; 

Restart nginx:

$ sudo systemctl restart nginx

At this point, your Strapi API should be accessible at the following URL: http://yourIP.

Conclusion

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?