Please note: Since we initially published this blog post, we released new versions of Strapi and tutorials may be outdated. Sorry for the inconvenience if it's the case. Please help us by reporting it here. Please follow the official documentation for deploying your Strapi project.
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 a 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.
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.
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:
1$ curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
2$ sudo apt-get install -y nodejs
Check that Node has been successfully installed:
1$ node -v
It should print v9.x.x
.
Strapi uses MongoDB as the default database system. Except if you decided to use another database system or host the MongoDB database on another server or service (what we strongly recommend), you have to install MongoDB on the Ubuntu server.
Import the public key:
1$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
Create a list file:
1$ 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:
1$ sudo apt-get update
Install MongoDB packages:
1$ sudo apt-get install -y mongodb-org
Start MongoDB:
1$ 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 port 27017. We highly advise you to restrict access by adjusting your Firewall rules.
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:
1$ cd ~
2$ git clone https://github.com/strapi/sample-strapi-app
Except if you want to get a clean Strapi app, replace the URL above by one of your projects.
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:
1$ NODE_ENV=production npm start
Note: NODE_ENV=production
informs Strapi that the app is running in production mode. The configuration files taken into 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
.
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 sure the app will be automatically restarted if it crashes.
This is where PM2, the de-facto process manager for Node.js, comes to the rescue.
Install PM2:
1$ 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:
1$ 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:
1$ pm2 list
To make sure your API is running well, check out the logs:
1$ pm2 logs api
Press crtl
+ c
to leave the logs view.
Note: take a look at the PM2 documentation for more advanced usage.
Your application is now up and running on port 1337
. To make it accessible on the web port (80) you need to install a reverse proxy.
For this tutorial, we are going to use nginx: the most famous HTTP and reverse proxy server.
Install it:
1$ sudo apt-get update
2$ sudo apt-get install -y nginx
Open the main configuration file:
1$ sudo nano /etc/nginx/sites-available/default
Next, replace the content in the location /
directive by the following:
1proxy_pass http://localhost:1337;
2proxy_http_version 1.1;
3proxy_set_header Upgrade $http_upgrade;
4proxy_set_header Connection 'upgrade';
5proxy_set_header Host $host;
6proxy_cache_bypass $http_upgrade;
Restart nginx:
1$ sudo systemctl restart nginx
At this point, your Strapi API should be accessible at the following URL: http://yourIP
.
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 into 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 set up 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 to do is to set up 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 deployments: 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?
Pierre created Strapi with Aurélien and Jim back in 2015. He's a strong believer in open-source, remote and people-first organizations. You can also find him regularly windsurfing or mountain-biking!