当前位置:   article > 正文

linux 使用nginx部署next项目,并使用pm2进程守护_pm2 查看端口

pm2 查看端口

前言

基于:操作系统 CentOS 7.6

使用的工具:Xshell7、Xftp7

服务器基础环境:

  1. node
  2. pm2
  3. nginx

所需基础环境本篇文章不进行介绍,请自行百度了解、安装。

1.将整个项目上传至服务器

除 .git、.next、node_modules 之外的项目文件上传至服务器

在这里插入图片描述

2.安装依赖

npm install
  • 1

在这里插入图片描述

3.项目打包

npm run build
  • 1

在这里插入图片描述

4.使用pm2启动Next.js生产服务器

pm2 start npm --watch --name next.js -- start
  • 1

pm2 start: 启动命令 npm: 包管理器名称
–name: 进程名称
– start: 对应的是 package.json 中的启动命令(如本地启动为 npm run dev,那么这里的start就替换成dev)

在这里插入图片描述

5.查看端口占用情况

netstat -nultp
  • 1

next 启动后默认端口为3000,可以使用 netstat -nultp 查看端口占用情况

在这里插入图片描述

如果需要更改启动端口的话,有两种方法。

第一种(适用于端口固定的情况)

 "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start -p 8888",
    "lint": "next lint"
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

第二种(适用于启动pm2时定义端口的情况)

 "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start -p $PORT",
    "lint": "next lint"
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

第二种方式示例:

PORT=8888 pm2 start npm --watch --name next.js -- start
  • 1

杀死指定进程

kill PID
  • 1

6.配置nginx.conf

location / {
  proxy_pass    http://127.0.0.1:3000/;
}
  • 1
  • 2
  • 3

7.效果图

在这里插入图片描述

如果本篇文章对你有帮助的话,很高兴能够帮助上你。

当然,如果你觉得文章有什么让你觉得不合理、或者有更简单的实现方法又或者有理解不来的地方,希望你在看到之后能够在评论里指出来,我会在看到之后尽快的回复你。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/804681
推荐阅读
相关标签
  

闽ICP备14008679号