简体   繁体   English

如何在 PM2 上为 Next js 设置端口

[英]How to set port for Next js on PM2

i have an ordinary next js application with the following scripts in my package.json file.我的 package.json 文件中有一个普通的 next js 应用程序,其中包含以下脚本。

  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start -p 3002"
  },

I have a start script with the -p tag to start the application on port 3002.我有一个带有-p标签的启动脚本,用于在端口 3002 上启动应用程序。

I hope to run this application using pm2 with the following script.我希望使用带有以下脚本的pm2运行此应用程序。

PORT=3002 NODE_PORT=3002 pm2 start npm --name "nextjs-website" -- start

Using the env variables PORT and NODE_PORT don't work.使用环境变量 PORT 和 NODE_PORT 不起作用。 How do i start this application using the same port as mentioned in the package.json file.如何使用 package.json 文件中提到的相同端口启动此应用程序。

Also is it possible to run the pm2 service the run the start in the package.json file?也可以运行 pm2 服务并在 package.json 文件中运行start吗?

In package.json add this start script "start": "next start -p 8000" and then use PM2 to start it with在 package.json 添加这个启动脚本 "start": "next start -p 8000" 然后使用 PM2 启动它

pm2 start yarn --name "nextjs" --interpreter bash -- start
pm2 show nextjs

check this article for more details.查看这篇文章了解更多详情。 https://www.willandskill.se/en/setup-a-next-js-project-with-pm2-nginx-and-yarn-on-ubuntu-18-04/ https://www.willandskill.se/en/setup-a-next-js-project-with-pm2-nginx-and-yarn-on-ubuntu-18-04/

Setting port number in npm script is not good idea at all.在 npm 脚本中设置端口号根本不是一个好主意。

Use this command to start server at any port使用此命令在任何端口启动服务器

sudo pm2 start npm --name "dev-staging" -- run start -- --port=8000 sudo pm2 start npm --name "dev-staging" -- 运行开始 -- --port=8000

or use an alias或使用别名

sudo pm2 start npm --name "dev-staging" -- run start -- -p 8000 sudo pm2 start npm --name "dev-staging" -- 运行开始 -- -p 8000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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