Search code examples
nginxserverstrapi

How to run strapi in the background?


I am able to deploy my strapi project to a remote server. How it works? - I connect via the console to the remote nginx server, go to the root of the project and write npm run develop, but unfortunately, if I close the terminal (through which I connected to the remote server), the project will close, and I need it to work 24/7.

Question: How to do it?

PS: I could attach nginx configurations here, but I don't know if they are needed.


Solution

  • the easiest way of doing this is thru pm2:

    in root of your project add file strapi.js with following config: /strapi.js

    const strapi = require('@strapi/strapi');
    strapi().start();
    

    connect to the server via ssh, let's assume that your strapi project is located in /var/www/strapi

    so

    yarn global add pm2 
    OR
    npm install pm2@latest -g
    
    cd /var/www/strapi
    pm2 start strapi.js
    

    After the process is created you can do:

    pm2 stop strapi    // stops server
    pm2 start strapi   // starts server
    pm2 monit strapi   // monitor server
    pm2 list           // lists pm2 processes
    

    you can reference docs