Are you looking to build a modern website using a flexible and customizable Content Management System (CMS)? Look no further than Strapi CMS. In this step-by-step guide, we will walk you through the process of installing Strapi CMS on a Ubuntu 22.04 server, along with Nginx as a reverse proxy server. With Strapi, you can build your website using popular frameworks like React and Next.js. Let’s dive in!
Prerequisites
Before we begin, let’s make sure you have everything you need:
- A server running Ubuntu 22.04.
- A non-root user with sudo privileges.
- A fully qualified domain name (FQDN) like strapi.example.com.
Make sure your system is up to date by running the following commands:
sudo apt update sudo apt upgrade
Next, install some necessary packages by executing the following command:
sudo apt install wget curl nano software-properties-common dirmngr apt-transport-https gnupg2 ca-certificates lsb-release ubuntu-keyring unzip -y
Step 1 – Configure Firewall
The first step is to configure the firewall to ensure the security of your server. Ubuntu comes with ufw (Uncomplicated Firewall) by default. Let’s check if the firewall is running:
sudo ufw status
You should see the following output:
Status: inactive
To allow SSH connections, run the following command:
sudo ufw allow OpenSSH
Next, open HTTP and HTTPS ports:
sudo ufw allow http sudo ufw allow https
Enable the firewall:
sudo ufw enable
You will be prompted to proceed with the operation. Type y
and press Enter.
To verify that the firewall is active, run:
sudo ufw status
The output should show that the firewall is active and the necessary ports are allowed.
Step 2 – Install and Configure PostgreSQL
Strapi CMS works with PostgreSQL 11 and above. Since Ubuntu 22.04 ships with PostgreSQL 14, we will use that version. Let’s start by adding the PostgreSQL GPG key:
curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /usr/share/keyrings/postgresql-key.gpg >/dev/null
Now, add the APT repository to your sources list:
sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/postgresql-key.gpg arch=amd64] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
Update the system repository:
sudo apt update
Install PostgreSQL and its contrib package:
sudo apt install postgresql postgresql-contrib
To check the status of the PostgreSQL service, run:
sudo systemctl status postgresql
You should see that the service is active and running.
Next, launch the PostgreSQL shell:
sudo -i -u postgres psql
Inside the shell, create the Strapi database:
CREATE DATABASE strapidb;
Create a user for Strapi CMS and set a strong password:
CREATE USER strapiuser WITH PASSWORD 'Your_Password';
Change the owner of the database to the newly created user:
ALTER DATABASE strapidb OWNER TO strapiuser;
Exit the PostgreSQL shell:
q
To verify that your credentials work, run:
psql --username strapiuser --password --host localhost strapidb
You will be prompted to enter the password you set earlier. If everything is correct, you should be able to access the PostgreSQL shell.
Step 3 – Install Node.js
Ubuntu 22.04 ships with an outdated version of Node.js. To install the latest LTS version of Node.js, follow these steps:
First, grab the Node.js v18 installer from Nodesource:
curl -sL https://deb.nodesource.com/setup_18.x-o nodesource_setup.sh
Run the installer script:
sudo bash nodesource_setup.sh
Install Node.js:
sudo apt install nodejs
Verify the Node.js version:
node -v
You should see the latest LTS version of Node.js installed.
Delete the installer file:
rm nodesource_setup.sh
Step 4 – Install Strapi
Now that all the prerequisites are in place, it’s time to install Strapi CMS. Run the following command:
npx create-strapi-app@latest my-project
Replace my-project
with the desired name for your Strapi project. This command will create a new Strapi project in the current directory.
During the installation process, you will be prompted to choose the installation type, preferred language, default database client, and other configuration options. Make the appropriate selections based on your requirements.
Once the installation is complete, navigate to the project directory:
cd my-project
Build the project, including the Strapi Admin UI:
NODE_ENV=production npm run build
Start the Strapi server:
node_modules/.bin/strapi start
Your Strapi CMS is now up and running! You can access the Strapi admin panel by opening the following URL in your browser: http://<your-server-IP>:1337/admin
.
Step 5 – Install and Configure PM2
Instead of manually starting the Strapi server every time, we can use PM2 (Process Manager 2) to manage the process and create a systemd service for automatic startup.
Switch to the home directory:
cd ~
Install PM2 globally:
sudo npm install pm2@latest -g
Create and open the PM2 configuration file for editing:
sudo nano ecosystem.config.js
Paste the following content into the file, replacing the directory path and database credentials with your own:
module.exports = { apps: [ { name: 'strapi', cwd: '/home/shapehost/my-project', script: 'npm', args: 'start', env: { NODE_ENV: 'production', DATABASE_HOST: 'localhost', DATABASE_PORT: '5432', DATABASE_NAME: 'strapidb', DATABASE_USERNAME: 'strapiuser', DATABASE_PASSWORD: 'Your_Password', }, }, ], };
Save the file and exit the editor.
Start your Strapi instance in the background using PM2:
pm2 start ecosystem.config.js
To ensure that PM2 starts automatically at server boot, run the following command:
pm2 startup
Follow the instructions provided to set up the startup script.
Save the PM2 process list:
pm2 save
Your Strapi CMS is now running as a background process managed by PM2.
Step 6 – Install Nginx
To serve your Strapi CMS over the internet, we need to install Nginx as a reverse proxy server. Ubuntu 22.04 ships with an older version of Nginx, so we’ll install the latest version. Here’s how:
First, import Nginx’s signing key:
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
Next, add the repository for Nginx’s stable version:
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg arch=amd64] http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
Update the system repositories:
sudo apt update
Install Nginx:
sudo apt install nginx
Verify the installation:
nginx -v
You should see the version of Nginx installed.
Start the Nginx server:
sudo systemctl start nginx
Step 7 – Install SSL
To secure your Strapi CMS with an SSL certificate, we’ll use Certbot to generate and manage the certificate. Here’s how to install Certbot using Snapd:
Since Ubuntu 22.04 comes with Snapd installed by default, ensure that your version of Snapd is up to date:
sudo snap install core sudo snap refresh core
Install Certbot:
sudo snap install --classic certbot
Create a symbolic link to the Certbot command:
sudo ln -s /snap/bin/certbot /usr/bin/certbot
Generate an SSL certificate for your domain:
sudo certbot certonly --nginx --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m test@example.com -d strapi.example.com
Replace strapi.example.com
with your own domain. The certificate files will be stored in the /etc/letsencrypt/live/strapi.example.com
directory on your server.
Generate a Diffie-Hellman group certificate for enhanced security:
sudo openssl dhparam -dsaparam -out /etc/ssl/certs/dhparam.pem 4096
To test the SSL renewal process, run the following command:
sudo certbot renew --dry-run
If there are no errors, your certificate will renew automatically when necessary.
Step 8 – Configure Nginx
To configure Nginx as a reverse proxy for your Strapi CMS, follow these steps:
Open the Nginx configuration file for editing:
sudo nano /etc/nginx/nginx.conf
Add the following line before the line include /etc/nginx/conf.d/*.conf;
:
server_names_hash_bucket_size64;
Save the file and exit the editor.
Create a new Nginx configuration file for your Strapi CMS:
sudo nano /etc/nginx/conf.d/strapi.conf
Paste the following code into the file, replacing strapi.example.com
with your own domain and adjusting other paths and settings as necessary:
server { # Redirect any http requests to https listen 80; listen [::]:80; server_name strapi.example.com; return 301 https://$host$request_uri; } server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name strapi.example.com; access_log /var/log/nginx/strapi.access.log; error_log /var/log/nginx/strapi.error.log; # TLS configuration ssl_certificate /etc/letsencrypt/live/strapi.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/strapi.example.com/privkey.pem; ssl_trusted_certificate /etc/letsencrypt/live/strapi.example.com/chain.pem; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384'; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:50m; ssl_session_timeout 1d; # OCSP Stapling ssl_stapling on; ssl_stapling_verify on; # Diffie-Hellman group certificate ssl_dhparam /etc/ssl/certs/dhparam.pem; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Host $http_host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:1337; } }
Save the file and exit the editor.
Verify the Nginx configuration file syntax:
sudo nginx -t
If the syntax is correct, restart the Nginx service:
sudo systemctl restart nginx
Congratulations! Your Strapi CMS is now accessible over the internet via the secure URL https://strapi.example.com
. You can now create an administrator user and start building your content.
Step 9 – Upgrade Strapi
Periodically, new versions of Strapi CMS are released with bug fixes and new features. To upgrade your Strapi installation, follow these steps:
First, stop the server:
cd ~ pm2 stop ecosystem.config.js
Navigate to the project directory and open the package.json
file for editing:
cd my-project nanopackage.json
Update the version numbers of all the Strapi packages to the latest stable version. You can find the latest version on Strapi’s GitHub releases page.
For example, update the dependencies
section like this:
"dependencies": { "@strapi/strapi": "4.5.5", "@strapi/plugin-users-permissions": "4.5.5", "@strapi/plugin-i18n": "4.5.5", "pg": "8.6.0" },
Save the file and exit the editor.
Install the upgraded version of Strapi:
npm install
Rebuild the administration panel:
NODE_ENV=production npm run build
Start the server again:
cd ~ pm2 start ecosystem.config.js
Your Strapi CMS is now upgraded and running with the latest features and improvements.
Conclusion
Congratulations on successfully installing and configuring Strapi CMS on your Ubuntu 22.04 server! You now have a powerful and flexible CMS at your disposal for building modern websites. Strapi’s extensive customization options, headless architecture, and user-friendly admin panel make it an excellent choice for developers and content creators.
Remember to regularly update your Strapi installation to benefit from the latest bug fixes and features. If you have any questions or run into any issues, please let us know in the comments below.
Don’t forget to check out Shape.host for reliable, scalable, and secure Linux SSD VPS hosting services. Shape.host is your trusted partner for all your cloud hosting needs.