Skip to content

nuxt项目部署

date: 2020-05-02 21:00:00 tags: [linux,vue,nginx]

项目打包

运行端口设置

修改 package.json 文件scripts.start

bash
"scripts": {
  "serve": "nuxt",
  "build": "nuxt build",
  "start": "PORT=3002 nuxt start", // 端口改为3002
}

打包上传

在本地项目根目录下运行

bash
$ npm run build

命令生成.nuxt文件夹。 把本地文件的这四个文件夹传到服务器目录下,服务器目录为

服务端安装依赖,运行

在服务端,项目文件夹

bash
$ npm install -production
$ npm start

此时运行的是 ;

Nginx设置

配置

下打开默认配置文件
bash
$ vim nginx.conf

在原server{} 下方添加

bash
upstream nodenuxt {
  server localhost:3002; # nuxt 项目监听PC端端口
  keepalive 64;
}
server {
  listen       80;
  server_name  chart.coolo.top; # 域名

  location / {
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_set_header Host $host;
      proxy_set_header X-Nginx-Proxy true;
      proxy_cache_bypass $http_upgrade;
      proxy_pass http://nodenuxt;
  }

  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
      root   html;
  }
}

重新载入nginx

bash
$ nginx -s reload

这个时候浏览器通过ip:3002访问,关闭窗口会结束进程

PM2程序守护进程

全局安装pm2

bash
$ npm i pm2 -g

pm2启动nuxt项目

bash
$ pm2 start npm --name xxx -- start // xxx是项目名称,即package.json中的name

pm2其他命令

bash
$ pm2 list
$ pm2 show 0                           #查看进程详细信息,0为PM2进程id 
$ pm2 stop all                         #停止PM2列表中所有的进程
$ pm2 stop 0                           #停止PM2列表中进程为0的进程
$ pm2 reload all                       #重载PM2列表中所有的进程
$ pm2 reload 0                         #重载PM2列表中进程为0的进程
$ pm2 delete 0                         #删除PM2列表中进程为0的进程
$ pm2 delete all                       #删除PM2列表中所有的进程

京ICP备2024093538号-1