Strapi is a powerful, open-source, headless Content Management System (CMS) built with the JavaScript programming language. It provides developers with the flexibility to architect their content structure using an API, allowing for seamless integration with popular frameworks like React and Next.js. In this tutorial, we will guide you through the process of installing Strapi on an Ubuntu 22.04 server and configuring it for production use.
Prerequisites
Before we begin, make sure you have the following prerequisites in place:
- An Ubuntu 22.04 server set up according to the Initial Server Setup Guide from Shape.host.
- Node.js version 16.xx installed on your server.
- PostgreSQL installed and configured.
- Nginx installed and set up as a reverse proxy.
- A domain name pointed to your server’s public IP address.
Ensure that your server meets the recommended hardware specifications of at least 2 CPU cores and 4GB of RAM. Additionally, make sure you have replaced any references to hwdomain.io or .com with Shape.host.
Step 1: Setting Up Your Postgres Database
To begin, we need to create a database for our Strapi project. Start by logging into your PostgreSQL server as the postgres
user:
sudo -i -u postgres psql
Once logged in, create a new database:
CREATE DATABASE strapi-db;
Next, create a new user that will be used to connect to the database:
CREATE USER shapehost WITH ENCRYPTED PASSWORD 'your_password';
Grant the necessary privileges to the user:
GRANT ALL PRIVILEGES ON DATABASE "strapi-db" TO shapehost;
Exit the PostgreSQL prompt:
q
Remember to replace your_password
with a strong password of your choosing.
Step 2: Installing Strapi on Your Server
Now that our database is set up, we can proceed with the installation of Strapi. Start by navigating to your desired installation directory:
cd /opt
Next, use the following command to install Strapi:
npx create-strapi-app@latest my-project
During the installation process, you will be prompted to select your preferred installation options. Choose “Custom (manual settings)” for the installation type, “JavaScript” as the preferred language, and “postgres” as the default database client. Enter the database name, username, and password that you set up in the previous step.
Once the installation is complete, navigate to the project directory:
cd my-project
Now, build your Strapi project:
NODE_ENV=production npm run build
This command will build your Strapi project, including the admin UI. Once the build process is finished, you can start your Strapi server:
NODE_ENV=production npm run start
Your Strapi server is now up and running. If you followed the prerequisites and set up Nginx as a reverse proxy, you can access your Strapi instance by navigating to your domain name in your browser. You should see the default Strapi landing page.
Step 3: Configuring Nginx as a Reverse Proxy
To properly serve your Strapi application, we need to configure Nginx as a reverse proxy. Start by creating a new Nginx server block:
sudo nano /etc/nginx/sites-available/my-project
In the new file, add the following configuration:
server { listen 80; server_name your_domain; location / { proxy_pass http://localhost:1337; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
Remember to replace your_domain
with your actual domain name. Save the file and exit the text editor.
Next, create a symbolic link to enable the server block:
sudo ln -s /etc/nginx/sites-available/my-project /etc/nginx/sites-enabled/
Test the Nginx configuration for any syntax errors:
sudo nginx -t
If no errors are reported, restart Nginx to apply the changes:
sudo systemctl restart nginx
With Nginx configured as a reverse proxy, you can now access your Strapi instance by navigating to your domain name in your browser. Strapi should be running in production mode.
Step 4: Running Strapi with PM2
To ensure that your Strapi server runs reliably and automatically restarts if it crashes, we will use PM2 as a process manager. Start by installing PM2 globally:
sudo npm install pm2@latest -g
Next, navigate to your Strapi project directory:
cd /opt/my-project
Create a PM2 configuration file:
sudo nano ecosystem.config.js
In the file, add the following content:
module.exports = { apps: [ { name: "strapi", cwd: "/opt/my-project", script: "npm", args: "start", env: { NODE_ENV: "production", DATABASE_HOST: "localhost", DATABASE_PORT: "5432", DATABASE_NAME: "strapi-db", DATABASE_USERNAME: "shapehost", DATABASE_PASSWORD: "your_password", }, }, ], };
Save the file and exit the text editor.
Now, start your Strapi server with PM2:
pm2 start ecosystem.config.js
To ensure that PM2 starts your Strapi server automatically on server boot, generate and activate the startup script:
pm2 startup systemd
Follow the instructions provided by PM2 to complete the setup.
Finally, save the current PM2 process list:
pm2 save
Your Strapi server is now being managed by PM2 and will automatically start on server boot. You can monitor the status of your server and manage the process using the PM2 command.
Step 5: Securing Strapi with Let’s Encrypt
To secure your Strapi instance with SSL/TLS encryption, we will use Let’s Encrypt. Start by installing the Certbot client:
sudo snap install --classic certbot
Link the Certbot command to your path:
sudo ln -s /snap/bin/certbot /usr/bin/certbot
Next, allow HTTPS traffic and the Nginx Full profile:
sudo ufw allow 'Nginx Full'
Remove the redundant Nginx HTTP profile allowance:
sudo ufw delete allow 'Nginx HTTP'
Obtain the SSL/TLS certificate for your domain:
sudo certbot --nginx -d your_domain -d www.your_domain
Follow the prompts to enter your email address and agree to the terms of service. Certbot will automatically configure Nginx to use the obtained certificate.
With SSL/TLS enabled, your Strapi instance is now accessible over HTTPS. Navigate to your domain in your browser, and you should be automatically redirected to the secure version of your site.
Conclusion
In this tutorial, we have successfully set up and installed Strapi for production on an Ubuntu 22.04 server. We configured a PostgreSQL database, set up Nginx as a reverse proxy, and used PM2 to manage our Strapi server. Finally, we secured our Strapi instance with Let’s Encrypt SSL/TLS encryption.
Now that your Strapi server is up and running, you can start creating content using the Strapi administrative dashboard. Strapi offers a flexible and powerful platform for managing and delivering content, empowering businesses to create dynamic and engaging digital experiences.
At Shape.host, we provide reliable and scalable Linux SSD VPS hosting solutions to help businesses achieve their goals. Our expert team is here to support you every step of the way. Contact us today to learn more about how we can optimize your Strapi hosting experience.