How To Deploy Strapi In Ubuntu Server With pm2

Introduction:

Deploying Strapi, the popular open-source headless CMS, on a server is a great way to manage and scale your content management system. In this guide, we’ll walk you through the steps to Deploy Strapi In Ubuntu Server using PM2, a process manager for Node.js applications. By following these instructions, you’ll have your Strapi instance up and running in no time, ensuring a smooth and efficient content management experience.

Step 1: Set Up an Ubuntu Server:

First, make sure you have an Ubuntu Server instance ready for deployment. You can set up a virtual machine on a cloud provider or use a physical server. Ensure that your server has Node.js and npm installed, as Strapi requires these dependencies to run.

Step 2: Install PM2:

PM2 is a powerful process manager for Node.js applications. It lets you keep your application running continuously, even after the system restarts. To install PM2, open a terminal on your Ubuntu Server and run the following command:

npm install -g pm2

Step 3: Download and Install Strapi:

Next, you must download and install Strapi on your Ubuntu Server. In the terminal, navigate to the desired directory where you want to install Strapi and run the following command:

npx create-strapi-app@latest my-project --quickstart
Bash

This command will create a new Strapi project in the “my-project” directory.

Step 4: Configure Strapi:

Once the installation is complete, navigate to the “my-project” directory and configure your Strapi application. You can set up your desired database, authentication methods, and other configurations according to your project requirements.

Step 5: Start Strapi with PM2:

To start Strapi using PM2, navigate to the root directory of your Strapi project and run the following command:

pm2 start npm --name "my-project" -- start
Bash

PM2 will start your Strapi application as a background process and automatically restart it if the server reboots or if any crashes occur.

Step 6: Set Up PM2 to Start Automatically:

To ensure that PM2 starts automatically whenever the server reboots, run the following command

pm2 startup systemd
Bash

This command will generate a system script for PM2 and print out a command you need to run as sudo. Copy the generated command and execute it in the terminal.

Step 7: Monitoring and Managing Strapi with PM2:

ecosystem.config.js

module.exports = {
  apps: [
    {
      name: 'strapi_app_development',
      cwd: '/home/kanhu/apps/strapi.flipverse.info',
      script: 'npm',
      args: 'run develop',
      env: {
        NODE_ENV: 'development',
        DATABASE_HOST: 'localhost',
        DATABASE_PORT: '5432',
        DATABASE_NAME: 'strapidb',
        DATABASE_USERNAME: 'strapiuser',
        DATABASE_PASSWORD: 'qwer1234',
      },
    },
    {
      name: 'strapi_app_production',
      cwd: '/home/kanhu/apps/strapi.flipverse.info',
      script: 'npm',
      args: 'start',
      env: {
        NODE_ENV: 'production',
        DATABASE_HOST: 'localhost',
        DATABASE_PORT: '5432',
        DATABASE_NAME: 'strapidb',
        DATABASE_USERNAME: 'strapiuser',
        DATABASE_PASSWORD: 'qwer1234',
      },
    },
  ],
};
JavaScript

Development

pm2 start ecosystem.config.js --only strapi_app_development
Bash

Production

pm2 start ecosystem.config.js --only strapi_app_production
Bash

PM2 provides various commands to monitor and manage your Strapi application. Here are some useful ones:

  • To monitor your application’s logs: pm2 logs my-project
  • To stop your application: pm2 stop my-project
  • To restart your application: pm2 restart my-project
  • To view a list of running processes: pm2 list
Deploy Strapi In Ubuntu Server - Admin Preview

Conclusion:

By following these steps, you’ve successfully deployed Strapi on an Ubuntu Server using PM2. You now have a robust and scalable setup for managing your content with Strapi. Utilizing PM2 ensures that your Strapi instance runs continuously, even after the server restarts or crashes. Enjoy the benefits of Strapi’s flexible and customizable headless CMS system on your Ubuntu Server!

Remember to adapt the instructions to your specific server setup and project requirements. Happy deploying!

Build and deploy next js app – https://www.developernoob.com/how-to-deploy-next-js-application-in-ubuntu-22-04-with-nginx/

More help in strapi please go through the docs here

Leave a Comment