DEV Community

Cover image for Deploy Strapi v4 on Ubuntu 22.04 VPS
Caner Tuncay
Caner Tuncay

Posted on

Deploy Strapi v4 on Ubuntu 22.04 VPS

Introduction

These days i have been interested in Strapi and Strapi Design Systems a lot. Also i use for my IoT projects as backend. Strapi is an open-source Node.js headless CMS and it has a lot of flexibility to build backend for blog as a CMS or even headless commerce with Next JS. I thought the documentation about Deploying on Vps is a little bit blurry for beginners.

So,

I will manage this writing in 4 sections:

  1. Creating VPS
  2. Installation of essential packages on VPS.
  3. Creating PostgreSql user and database for Strapi.
  4. Clone and building the project. (Creating first admin user)

1. Creating VPS

According to Strapi Deployment Specs, hardware requirements must be minimum:

CPU: Minimum 1 Core
Memory: Minimum 2GB
Disk: Minimum 8GB

Any kind of Vps Provider and plans will be perfect if these minimum requirements are provided.


After creating Vps,

  1. Login and set up root user access with SSH
  2. Create a new user
  3. Set Up a basic firewall

2. Installation of Essential Packages

Install Node.js

First, update local packages.

$ sudo apt update
Enter fullscreen mode Exit fullscreen mode

Then install Node.js

$ sudo apt install nodejs
Enter fullscreen mode Exit fullscreen mode

Verify the version with

$ node -v
Enter fullscreen mode Exit fullscreen mode

Note: according to the output version of node, if you would like to install specific version you can follow these steps and install nvm;

## Run nvm installer
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

## Update profile
$ export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

## Reload shell config.
$ source ~/.bashrc  
Enter fullscreen mode Exit fullscreen mode

After that;

$ nvm -v
Enter fullscreen mode Exit fullscreen mode

Then you can install the specific version of Node with

$ nvm install 16 #Or Specific version
Enter fullscreen mode Exit fullscreen mode

Side note: Strapi supports only LTS versions (v16 - v18)
Node v18.x recommended for Strapi v4.3.9 and above.

Node v16.x is recommended for Strapi v4.0.x to v4.3.8

Also npm v6 and above.


After that you need to install build-essentials package:

$ sudo apt install build-essentials
Enter fullscreen mode Exit fullscreen mode

After completing these steps, we will manually change npm's default directory. We will create .npm-global directory and set the path:

cd ~
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
Enter fullscreen mode Exit fullscreen mode

Create (or modify) ~/.profile file:

sudo nano ~/.profile
Enter fullscreen mode Exit fullscreen mode

And add this line:

export PATH=~/.npm-global/bin:$PATH
Enter fullscreen mode Exit fullscreen mode

Then update the system variables:

source ~/.profile
Enter fullscreen mode Exit fullscreen mode

Install and configure Git versioning

We can check if Git is installed:

git --version
Enter fullscreen mode Exit fullscreen mode

If not:

$ sudo apt update

$ sudo apt install git

$ git --version
Enter fullscreen mode Exit fullscreen mode

After that we can set up Git:

$ git config --global user.name "Your Name"
$ git config --global user.email "youremail@domain.com"
Enter fullscreen mode Exit fullscreen mode

To display config:

$ git config --list
Enter fullscreen mode Exit fullscreen mode

3. Creating PostgreSql user and database

We need to install postgresql first with :

$ sudo apt update
$ sudo apt install postgresql postgresql-contrib
Enter fullscreen mode Exit fullscreen mode

Ensure system is running:

$ sudo systemctl start postgresql.service
Enter fullscreen mode Exit fullscreen mode

To create postgres user, while you are logged in as linux user (vps user), with sudo command:

$ sudo -u postgres createuser --interactive
Enter fullscreen mode Exit fullscreen mode

You will be asked name of role to add:

Output
Enter name of role to add: your_sudo_account_name
Shall the new role be a superuser? (y/n) y
Enter fullscreen mode Exit fullscreen mode

Then Create Database with same name:

$ sudo -u postgres createdb your_vps_linux_username
Enter fullscreen mode Exit fullscreen mode

Then Open Postgres prompt with postgres Role:

$ sudo -u your_vps_linux_username psql

## And then with command:

\password 

## You can exit with:

\q and then

exit 

## Note that password you will be created. It will be your db password for ecosystem.config.js and strapi database.js.
Enter fullscreen mode Exit fullscreen mode

To login with ident based authentication, you will need a Linux user with the same name as your Postgres role and database. You have just created with your current linux user. If you want to create another user and database, you have to create also another linux user account later with:

$ sudo adduser another_account
Enter fullscreen mode Exit fullscreen mode

Once we create our role and database with our Linux user, we have to alter the postgres role because it has no password.

$ sudo -i -u postgres psql

$ \password


Enter fullscreen mode Exit fullscreen mode

Now your database name: your_linux_user_name
Database User: your_vps_linux_username
Database password: Password that i pointed out above.


Now on development computer and project folder, (if you did not choose PostgreSQL when you first set-up Strapi) you have to install pg dependency package.

$ npm install pg --save

#or

$ yarn add pg
Enter fullscreen mode Exit fullscreen mode

After that, (development computer) Replace the content in ./config/database.js with below:

module.exports = ({ env }) => ({
  connection: {
    client: 'postgres', 
  connection: {
        host: env('DATABASE_HOST', '127.0.0.1'),
        port: env.int('DATABASE_PORT', 5432),
        database: env('DATABASE_NAME', ''),
        user: env('DATABASE_USERNAME', ''),
        password: env('DATABASE_PASSWORD', ''),
        ssl: {
          rejectUnauthorized:env.bool('DATABASE_SSL_SELF', false),
       },
      },
      debug: false,
  },
});
Enter fullscreen mode Exit fullscreen mode

Now you are ready to push the changes to Github:

git add .
git commit -m "Configured database.json"
git push
Enter fullscreen mode Exit fullscreen mode

4. Cloning and building the project.

Now you are ready to clone your project to Vps.

$ cd ~
$ git clone https://github.com/your-name/your-project-repo.git
Enter fullscreen mode Exit fullscreen mode

Next navigate to your project folder.

$ cd ./your-project-folder

## and then

$ npm install 

## or

$ yarn install
Enter fullscreen mode Exit fullscreen mode

Strapi uses port 1337 default. We have to open the port 1337 for Strapi.

cd ~
sudo ufw allow 1337/tcp
sudo ufw enable
Enter fullscreen mode Exit fullscreen mode

After we install and configure Nginx we are going to close the port 1337 with:

sudo ufw deny 1337
Enter fullscreen mode Exit fullscreen mode

Install and Configure PM2 Runtime

PM2 Runtime allows you to keep your Strapi project alive and to reload it without downtime.

npm install pm2@latest -g
Enter fullscreen mode Exit fullscreen mode

After that:

cd ~
pm2 init
sudo nano ecosystem.config.js  ## nano or any kind of editor
Enter fullscreen mode Exit fullscreen mode

Now replace the file with below:

module.exports = {
  apps: [
    {
      name: 'strapi',
      cwd: 'cloned-strapi-project-directory/',  
      script: 'npm',  // Or 'yarn'
      args: 'start',
      env: {
        NODE_ENV: 'production',
        DATABASE_HOST: 'localhost', // database endpoint
        DATABASE_PORT: '5432',
        DATABASE_NAME: 'your-db-name', // DB name
        DATABASE_USERNAME: 'your-username', // your username for psql
        DATABASE_PASSWORD: 'your-password', // your password for psql
      },
    },
  ],
};
Enter fullscreen mode Exit fullscreen mode

Also set your .env file (create if does not exist).

Now start the ecosystem.config.js file with:

cd ~
pm2 start ecosystem.config.js
Enter fullscreen mode Exit fullscreen mode

Now PM2 Active and Running. You should be able to see Create Admin Page on Port 1337.

Strapi-Login-Screen

Last thing we have to do is generate startup script to launch PM2:

$ cd ~
$ pm2 startup systemd
Enter fullscreen mode Exit fullscreen mode

Follow the instruction with the command line; copy and paste the path that generated. You will be prompted:

...

[PM2] [v] Command successfully executed.

...
Enter fullscreen mode Exit fullscreen mode

Then type :

$ pm2 save
Enter fullscreen mode Exit fullscreen mode

Then make sure if the script works with:

$ pm2 list

## and 

$ systemctl status pm2-your-username
Enter fullscreen mode Exit fullscreen mode

Now you can set up and configure Nginx, install SSL with Certbot by Let's Encrypt, and create a webhook with Git.

If you realize any typo or something, please contact me.

SOURCE:

  1. Strapi Deployment DigitalOcean Droplets

  2. How To Install and Use PostgreSQL on Ubuntu 18.04

  3. How To Install Node.js on Ubuntu 18.04

  4. Strapi Quick Start Guide

  5. How To Set Up a Node.js Application for Production on Ubuntu 18.04

  6. How To Install Nginx on Ubuntu 20.04

Oldest comments (0)