System Information
-
Strapi Version: 3.4.0
-
Operating System: Linux
-
Database: Mysql
-
Node Version: v12.18.3
-
NPM Version: 6.14.6
-
Yarn Version: 1.22.4
I following the strapi docs to serve the strapi project via pm2. I had create a ./server.js file - Process Manager - Strapi Developer Documentation like this link.
./server.js
module.exports = {
apps: [
{
name: "strapi-backend",
script: "npm",
args: "dev",
},
],
};
It’s work to serve developerment mode in droplet. How we can run it as production mode? I saw a docs from strapi.
NODE_ENV=production yarn start
How we run this in pm2?
show a list of app serve by pm2
$ pm2 list
restart app serve by pm2
$ pm2 restart [numberApp]
How we run NODE_ENV=production yarn start in pm2? Because there has a list of project. How it know to point strapi-backend app?
1 Like
@wilkersoh
To run strapi with pm2 in production mode, I would recommend using ecosystems file.
Create an ecosystems.config.js
file in the root folder of your project (near package.json).
module.exports = {
apps: [
{
name: 'yourapp-name',
script: 'npm',
args: 'start',
env: {
NODE_ENV: 'production',
},
exp_backoff_restart_delay: 100,
},
],
};
Now run it with following command: pm2 start ecosystems.config.js
4 Likes
Tried pm2 today in ec2 and got some error, any idea?
/home/ubuntu/.pm2/pm2.log last 15 lines:
PM2 | at Process.ChildProcess._handle.onexit (node:internal/child_process:283:19)
PM2 | at onErrorNT (node:internal/child_process:478:16)
PM2 | at processTicksAndRejections (node:internal/process/task_queues:83:21)
PM2 | 2022-08-19T15:24:27: PM2 log: App [strapi-cms:0] starting in -fork mode-
PM2 | 2022-08-19T15:24:27: PM2 log: App [strapi-cms:0] online
PM2 | 2022-08-19T15:24:27: PM2 error: Error: spawn node ENOENT
PM2 | at Process.ChildProcess._handle.onexit (node:internal/child_process:283:19)
PM2 | at onErrorNT (node:internal/child_process:478:16)
PM2 | at processTicksAndRejections (node:internal/process/task_queues:83:21)
PM2 | 2022-08-19T15:34:46: PM2 log: App [strapi-cms:0] starting in -fork mode-
PM2 | 2022-08-19T15:34:46: PM2 log: App [strapi-cms:0] online
PM2 | 2022-08-19T15:34:46: PM2 error: Error: spawn node ENOENT
PM2 | at Process.ChildProcess._handle.onexit (node:internal/child_process:283:19)
PM2 | at onErrorNT (node:internal/child_process:478:16)
PM2 | at processTicksAndRejections (node:internal/process/task_queues:83:21)
here is my ecosystem.config.js,
module.exports = {
apps: [
{
name: ‘strapi-cms’,
cwd: ‘/home/ubuntu/strapi_cms’,
script: ‘npm’,
args: ‘start’,
env: {
NODE_ENV: ‘production’,
DATABASE_HOST: ‘something-exxxxxast-1.rds.amazonaws.com’,
DATABASE_PORT: ‘5432’,
DATABASE_NAME: ‘sdfadsfaf’,
DATABASE_USERNAME: ‘adfadfasd’,
DATABASE_PASSWORD: ‘adfasdfasfd’,
AWS_ACCESS_KEY_ID:‘yyyyyy’,
AWS_SECRET_ACCESS_KEY:‘xxxx’,
AWS_REGION:‘xx-xx-x’,
AWS_BUCKET_NAME:‘xxxx-xxx’
}
},
],
}
I tried different node version 16,17, 18, all the same.
With all these creds in .env, I can start it with nohup.
I solved it, pretty dumb, delete the pm2 job pm2 delete 0
,
and pm2 start ecosystem.conf.js. just a bad day maybe.
Maybe it will help someone. At the moment running pm2 in win via npm causes errors because pm2 is trying to run a win script as js.
That’s why we need to add a starting point as a js file in root.
Process Manager - Strapi Developer Docs
Then you can add the config as a json in root of project.
{
"apps": [
{
"name": "app-name",
"script": "server.js",
"cwd": "./"
}
]
}
And then in package json scripts section jsut:
"scripts": {
"develop": "strapi develop",
"build": "strapi build",
"strapi": "strapi",
"start": "strapi start",
"start-pm": "pm2 start pm2.json"
}