Self-host multiple Strapi apps on one VPS: A step-by-step guide demonstrating how to setup strap on a VPS.

How it all started and why

Have you been in a situation where you’re using a headless CMS like Strapi? Most times because you wanted to avoid the hustle of building your own Content Management System so that you could go to the market faster?

I was in a similar situation.

At my startup, we were building our mobile app, cutt.ly/insightify to empower developers with the latest opportunities in tech. But we were extremely on a low budget and development team so small. This is when I discovered Strapi, Amplication and I was already a WordPress guru. I liked the out-of-box REST API Strapi provides and proceeded to learning it.

After a few weeks, I gained the needed confidence in it and setup a custom backend on it. But, the next step was to deploy and make it consumable by the frontend so that our ReactNative developer could go ahead to integrate in our mobile app.

Little did I know that I would fail to deploy it on familiar services like Vercel and Netlify.

In this tutorial, I am happy to share a step-by-step guide showing you how I was able to deploy Strapi on a Compute Engine (CE) on Google Cloud Platform (GCP) but same as VPS in the context of other server providers.

As you read along you’ll gain more understanding of Strapi, a Headless CMS, and VPS. Most importantly you learn how to deploy Strapi on your own VPS, save a tone of hours and costs. Before concluding I propose 3 options you can try out to the deployment.
Are you ready? Let's dive in.

What is Strapi?

Strapi is a headless CMS built in JavaScript particularly in Node.js and the React. It is open-source. It is among the most popular headless CMSs by the time of this writing in 2024. Sometimes it is referred to as a low-code REST API builder.

Why Developers choose Strapi?

  1. It generates a REST API out-of-the-box for your content, thus making your content accessible through versatile endpoints. In case you want to work with GraphQL there is a free GraphQL extension you can install and have fun.

  2. Like other headless Content Management Systems, Strapi provides a fully-fledged administration UI saving developers thousands of hours.

  3. Strapi has a robust and growing plugin ecosystem, giving developers confidence that whatever isn’t in Strapi out-of-box could be achieved by installing an additional free plugin.

  4. Strapi can easily be deployed on Strapi Cloud and for teams which have a budget this doesn’t only save them time but it gives them confidence knowing that they’re relying on the service provided by the Strapi developers themselves therefore fast technical support could be around the corner may anything go wrong.

  5. Strapi is highly customisable through the 3rd-party plugins, controllers, routes and UI. You can customise the API to behave in any way you want to fit your unique needs.

What is a Headless CMS?

A headless CMS is a web app that provides an administration user interface for managing content. It may also enable administrators to customise content types, custom fields, users, and layouts to suit their unique business needs.

It also provides an out-of-box API to allow content consumption via endpoints so that frontends of various natures can rely on it.

How to install Strapi

You need two environments to have a working instance of Strapi. That is (1) the production and (2) the development environment.

(1) The Production environment

  1. Get a VPS from a provider of your choice. A VPS could be an EC2 instance on AWS or Compute Engine on GCP or Node on Linode or Droplet on Digital Ocean. They’re just many different words used to refer to the same thing.

    For cost effectiveness my favourite VPS provider is contabo.com. I have used them for more than 5 years and I don’t have major complaints. They are affordable and reliable.

    VPS Requirements

    I recommend that the VPS meets these following requirements.

    • RAM: 4GB
  • CPU: 2 vCPUs
  • Storage: 10GB
  • IP: Public IPv4
  1. After getting the VPS, assign a static public IP address. This will enable users to access the Strapi Instance. You can also connect a domain name to it to make things more easier for your team members and yourself.

  2. Access the VPS via SSH and setup the necessary tools in order to provide a secure and functional production environment that power your Strapi apps, static websites and normal node apps.

    These are the 8 tools you should setup on your VPS.

    1. Operating System: Ubuntu server 22.04 LTS

    2. Version control: Git

    3. Runtime environment: Node.js 18.0.0

    4. Web server: latest Nginx

    5. Firewall: UFW

    6. HTTP Security: Letsencrypt for generating FREE SSL

    7. Node Process Manager: PM2 or Forever (I recommend PM2)

    8. Database: Postgres or MySQL (I recommend Postgres)

Make sure that you install and configure all the above 8 tools on your chosen VPS.

(2) Developer environment

The developer environment could be on your computer or any other where you intend to test configure and test Strapi before taking it to your production VPS.

Here your basic needs are; a running version of Node.js 18.0.0 and Git

Development environment

  1. Download Strapi to your local machine.

  2. Then cd into the strapi directory

  3. Install the necessary node dependences by executing npm install

  4. Run Strapi and start using it on your developer environment by executing the following command npm run develop

  5. Strapi will require you to create a super admin account and login

Deployment steps

  1. Connect Strapi to Github.

    1. Create Github repo for the project

    2. Connect Github repo to the local Strapi repo

  2. Create the desired content types

  3. If needed create and attach custom fields to your content types

  4. Commit changes and push to Github

  5. SSH into my VPS. Open the terminal on your computer and execute ssh username@vpsIPAddress -p port number e.g ssh root@257.XXX.XXX.XXX -p 2222

  6. If you want to use a friendly domain while accessing your Strapi deployment, go ahead and map the domain name to the VPS. You can accomplish this by purchasing a domain name from any domain provider and pointing it to the IP address of the VPS.

  7. Add a domain name to the VPS

  8. Create a server block for this domain

  9. Create a Postgres database and credentials

  10. cd to domain name directory

  11. Pull from Github

  12. Copy .env.example to .env and open the file for editing, usually by executing sudo nano .env

  13. Set values to the PORT, DATABASE_NAME, DATABASE_USERNAME, DATABASE_CLIENT and DATABASE_PASSWORD variables in the environment file. Additionally, generate the necessary tokens and secrets and set them too. Save your changes.

  14. Excute npm install

  15. Execute NODE_ENV=production npm run build this will execute database migrations and build the static Strapi admin for you.

  16. Then execute pm2 start npm --name "my-app-name" -- run start

Note: Replace my-app-name with the appropriate name for your project.

  1. Visit the domain in the browser.

  2. Sign up for a super admin account in the production environment.

Login, and you will find all your content types and fields readily available from the local to the production environment. If there is some content you want to migrate from local to production, install the Export/Import plugin.

Now you can setup the API tokens that will be used to authenticate the frontends.

To use your Strapi API, use API consumers like Postman, Insomnia, or even command-line programs like CURL. Then access the https://{domain.com}/api/{endpoint}

Replace {domain.com} and {endpoint} with your domain and endpoints.

How do you update your Strapi deployment?

Now that you have succeeded at setting up web server and deployed Strapi on your VPS or Compute, let's look at how you can update this deployment. In this section I will show you both manual and automated way of updating your Strapi deployments.

I have the assumption that like me, your workflow is from development environment machine you push to Github then pull to the VPS but not the other way round.

Manual Update Deployment

  1. Create an ssh key on your server and add it your Github account. Open Github and log in, then go to settings and choose SSH and GPG keys.

  2. Login to your VPS using the root user account ssh root@domain.tld -p XXXX Replace domain.tld and XXX with the actual domain and ssh port number, respectively. Then provide the password to be logged in.

  3. Stop the particular Strapi instance you want to update. Use pm2 list to find out the names and ids of the Strapi instance then use pm2 stop id|name to stop the instance.

  4. Navigate to the linux user who owns the project the strapi project in your web server. E.g sudo su username and provide the password of username if prompted.

  5. Navigate to strapi project directory. cd /var/www/domain.ltd/public_html

    Note: Replace domain.tld with the actual domain name of your Strapi project.

  6. Pull updates from your repository. git pull origin main

  7. If your update needs to make changes to the node_modules, you can run npm install and sometimes npm install --force

  8. Make a build to perform the migrations and UI updates necessary to Strapi. E.g NODE_ENV=production npm run build

  9. Now switch back to the root user. sudo su - and provide the password when prompted.

  10. Finally, start or restart Strapi by executing pm2 start id|name where id should be replaced with the actual process id in PM2 and name should be replaced with the actual process name in PM2. To know the id and name just execute pm2 list in the right user context.

Automate the Deployment

If you’ve reached this far, I think you’ve deployed Strapi successfully. But you are wondering should I be deploying manually whenever the team makes changes on Strapi?

I will suggest you look at the following three options

  • Using Coolify which turns a VPS into a Vercel or Netlify alternative but can host both backend and frontend apps.

  • Implementing github actions

  • Implementing a bash script and use `crontab` to automate the entire process.

Conclusion

In this short post, we’ve learned what Strapi, and headless cms are, why developers choose Strapi, and how to setup Strapi on a VPS and your computer. We’ve also discovered the three options that can help you automate the deployment process. You can now build APIs without writing a line of code.

Was this helpful? Do you have any questions? Let me know in the comment section below.

Follow me @davidofug