简体   繁体   中英

Can I use pm2 to keep a Next.js app running

I have an AWS EC2 that is just for keeping my Next.js client side running (there is no backend for it right now). Currently if I go into the terminal and enter npm run develop , the site runs perfectly. However, I want this process to always be running in the background of the instance. Is there a way I can do that with pm2?

I have it installed globally, but it won't let me run pm2 start npm run develop . BTW yes I want it running in development mode right now.

yes, but make sure the command exists in the package.json

  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  }

so in this case, the pm2 command will be

pm2 start npm --  dev
#in your case
pm2 start npm --  develop

在此处输入图像描述

Or to have a nice name as mentioned by @brc

pm2 start npm --name "next-js" -- dev

在此处输入图像描述

While running on EC2, it also good practice to save process for startup

PM2 can generate startup scripts and configure them in order to keep your process list intact across expected or unexpected machine restarts.

https://pm2.keymetrics.io/docs/usage/startup/

pm2 startup
pm2 save

Adding to best answer: You should really consider using:

pm2 start npm --  start

as it will run production build, not development (I know OP asked question with develop , but it is important feature considering app being in production.

Yes you can

 pm2 start npm --name name_of_the_app -- start --port your_port

eg

pm2 start npm --name abhimanyu -- start --port 3000

Here my app name is (abhimanyu) which is running on port 3000

Note: If you do not provide the port number then it will use the default port which is specified on your project

To get the list of the project which is running in pm2 type

pm2 list

在此处输入图像描述

I have adde full working step to deploy nextjs app on ubuntu platform using pm2 and nginx.

https://anuragdeep.com/deploy-nextjs-strapi-on-ubuntu-with-nginx-and-pm2/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM