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:
- Creating VPS
- Installation of essential packages on VPS.
- Creating PostgreSql user and database for Strapi.
- 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,
2. Installation of Essential Packages
Install Node.js
First, update local packages.
$ sudo apt update
Then install Node.js
$ sudo apt install nodejs
Verify the version with
$ node -v
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
After that;
$ nvm -v
Then you can install the specific version of Node with
$ nvm install 16 #Or Specific version
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
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'
Create (or modify) ~/.profile
file:
sudo nano ~/.profile
And add this line:
export PATH=~/.npm-global/bin:$PATH
Then update the system variables:
source ~/.profile
Install and configure Git versioning
We can check if Git
is installed:
git --version
If not:
$ sudo apt update
$ sudo apt install git
$ git --version
After that we can set up Git
:
$ git config --global user.name "Your Name"
$ git config --global user.email "youremail@domain.com"
To display config:
$ git config --list
3. Creating PostgreSql user and database
We need to install postgresql first with :
$ sudo apt update
$ sudo apt install postgresql postgresql-contrib
Ensure system is running:
$ sudo systemctl start postgresql.service
To create postgres user, while you are logged in as linux user (vps user), with sudo command:
$ sudo -u postgres createuser --interactive
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
Then Create Database with same name:
$ sudo -u postgres createdb your_vps_linux_username
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.
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
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
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
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,
},
});
Now you are ready to push the changes to Github:
git add .
git commit -m "Configured database.json"
git push
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
Next navigate to your project folder.
$ cd ./your-project-folder
## and then
$ npm install
## or
$ yarn install
Strapi uses port 1337
default. We have to open the port 1337 for Strapi.
cd ~
sudo ufw allow 1337/tcp
sudo ufw enable
After we install and configure Nginx
we are going to close the port 1337
with:
sudo ufw deny 1337
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
After that:
cd ~
pm2 init
sudo nano ecosystem.config.js ## nano or any kind of editor
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
},
},
],
};
Also set your .env file (create if does not exist).
Now start the ecosystem.config.js
file with:
cd ~
pm2 start ecosystem.config.js
Now PM2 Active and Running. You should be able to see Create Admin Page on Port 1337
.
Last thing we have to do is generate startup script to launch PM2:
$ cd ~
$ pm2 startup systemd
Follow the instruction with the command line; copy and paste the path that generated. You will be prompted:
...
[PM2] [v] Command successfully executed.
...
Then type :
$ pm2 save
Then make sure if the script works with:
$ pm2 list
## and
$ systemctl status pm2-your-username
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:
Top comments (0)