How to start directus using pm2?

Viewed 1534

Documentation says:

Instead of starting the app using pm2 start hello.js, you can start Directus using pm2 start npm -- start

pm2 start npm -- start

I have tried

pm2 start npm /home/user/my-app/node_modules/.bin/directus start

https://docs.directus.io/guides/installation/ubuntu/

3 Answers

you can add the start script to your package.json file

"scripts": {                                                   
  "test": "echo \"Error: no test specified\" && exit 1",       
  "start": "directus start"                                    
},  

Then start it using pm2 start npm -- start

If you need a specific configuration, pm2 start npm --start will not be sufficient. You will need a pm2 configuration file.

  1. Create a ecosystem.config.js file in your directus root directory
  2. Add this content to the file:
module.exports = {
  apps : [
    {
      name: "directus-app",
      script: "npx",
      args: "directus start",
      restart_delay: 500
    }
  ]
}
  1. Edit the file with your preferred configuration

The documentation is not wrong and it's that simple if you don't have any other websites or are using virtual hosting with apache or Nginx.

In my case, I'm doing both. I have multiple sites with multiple domains and some of them are using subdomains through the use of virtualhosting.

Here are a few tips if you are in a similar situation:

  1. Open the correct port in the firewall, directus uses 8055 by default. sudo ufw allow 8055
  2. Use port forwarding with your web server. Link: https://www.ionos.com/digitalguide/websites/web-development/nodejs-for-a-website-with-apache-on-ubuntu/
  3. Add your (sub)domain to your /etc/hosts file. Run sudo nano /etc/hosts/ and the following line 127.0.0.1 ${{domain here}}

I would also recommend reading the pm2 documentation just to get a better understanding of it.

Related