Effective Methods for Deploying Strapi
Deploying Strapi on a Remote Server Using OpenSSH: A Step-by-Step Guide
In this guide, I’ll walk you through the process of deploying Strapi on a remote server using OpenSSH. Before we dive into the deployment, let’s briefly explore what Strapi is and why it might be the ideal CMS for your project.
What is Strapi?
Strapi is a free and open-source content management system (CMS) built with Node.js. It’s a "headless" CMS, meaning it provides a backend for managing content through APIs while allowing you to choose any frontend technology. Strapi also comes with a pre-built dashboard user interface, making it easier for non-developers to manage content.
One of Strapi's standout features is its role-based access control, allowing you to create roles and invite team members to collaborate securely. For more information on role management and other advanced features, check out the official Strapi blog.
What This Guide Covers
This article focuses on deploying Strapi on a remote server using the command line interface (CLI). We won’t be covering instant creation or server setup; instead, we'll concentrate on getting Strapi up and running on your server.
Prerequisites
Before you begin, ensure you have your own instance (server) ready to deploy Strapi.
Strapi Setup
First, generate your Strapi project using the quick start command. Open your terminal and run the following command:
npx create-strapi-app@latest my-strapi-project --quickstart
This command will generate your Strapi project and direct you to the Strapi signup page. After creating your credentials, you'll have access to the Strapi dashboard.
If you need to restart your local server, navigate to your project directory and use the following command:
cd your-strapi-project
npm run develop
For more detailed setup instructions, refer to the official Strapi documentation.
Deploying Strapi on a Remote Server
A remote server is a computer located within a network that you can access over the internet. To deploy Strapi on your remote server, follow these steps:
Install Required Packages:
First, you need to install Node.js using NVM (Node Version Manager) and Git.Clone Your Strapi Repository:
After installing Node.js and Git, clone your Strapi repository to the remote server:git clone your-git-url
Install Project Dependencies:
Navigate to your project directory and install the necessary packages:npm install NODE_ENV=production npm run build
Configure Environment Variables:
Set up your environment variables in a.env
file. Below is an example configuration for an SQLite database:bashCopy codeHOST=0.0.0.0 PORT=1337 APP_KEYS="toBeModified1,toBeModified2" API_TOKEN_SALT=tobemodified ADMIN_JWT_SECRET=tobemodified TRANSFER_TOKEN_SALT=tobemodified JWT_SECRET=tobemodified DATABASE_CLIENT=sqlite DATABASE_FILENAME=.tmp/data.db JWT_SECRET=GtYCaJGFFYUFg7uNFGXmig==
Start Strapi with PM2:
Use PM2, a process manager for Node.js applications, to start your Strapi server and keep it running in the background:cd ~ pm2 init sudo nano ecosystem.config.js
Replace the content in
ecosystem.config.js
with the following:module.exports = { apps: [ { name: 'strapi-app', cwd: '/home/your-name/your-project', // must have absolute path script: 'npm', args: 'start', env: { NODE_ENV: 'production', }, }, ], };
then run the below command to your server root directory
pm2 start ecosystem.config.js
Your Strapi instance should now be running on your remote server. Next, we’ll configure Nginx as a load balancer
Setting Up Nginx as a Load Balancer
Nginx is designed to handle a large number of concurrent connections efficiently, making it ideal for high-traffic websites. It can easily scale to accommodate growing traffic and serve as a load balancer to distribute requests across multiple servers.
First, install Nginx on your Ubuntu server (version 20.04 or any version). After installation, you’ll find the Nginx configuration files in the following directories:
/etc/nginx/nginx.conf
/etc/nginx/sites-available
/etc/nginx/sites-enabled
Nginx uses the "sites-available" and "sites-enabled" directories for efficient configuration management. The "sites-available" directory stores all server block configurations, while "sites-enabled" contains symbolic links to active configurations.
Create a new file for your server configuration:
sudo nano /etc/nginx/sites-enabled/mystrapi
Before proceeding, ensure you have generated an SSL certificate using Certbot. Then, paste the following configuration:
server {
server_name yourdomain.com www.yourdomain.com;
location / {
proxy_pass http://localhost:1337;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = yourdomain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 404; # managed by Certbot
}
This configuration directs traffic from yourdomain.com
to your Strapi server running on port 1337. However, if you encounter issues with Strapi’s authentication API paths (which default to /admin
), you may need to adjust your Nginx configuration.
If problems persist, you can revert to the default Nginx configuration, which might look like this:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
proxy_pass http://localhost:1337;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Your Strapi instance should now be successfully deployed and accessible via your domain. If you have multiple projects on the same server, ensure that your Nginx configuration is set up correctly to avoid routing issues.
Subscribe to my newsletter
Read articles from Devapraveen directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Devapraveen
Devapraveen
Passionate full-stack developer from india. Sharing knowledge and experiences through my blog to help fellow developers tackle challenges and navigate the evolving world of software development.