Nginx部署node笔记

说明
提到的路径以我操作的机子为主,可以自行定义存放位置

基本条件

  • 1、centos7虚拟机,安装了nginx(nginx安装
  • 2、FinalShell链接虚拟机进行操作(或者其他连接虚拟机的工具)

参考教程
linux安装npm

linux后台运行nodejs项目

nodejs服务器部署教程一

一个简单的node运行文件
/usr/share/nginx/hello/hello.js

const http = require('http')
http.createServer(function(req,res) {
res.writeHead(200,{'Content-Type':'text/plain'})
res.end('hello world')
}).listen(8081)

console.log('server test')

一个vue前端项目,打包好的dist文件
存放路径:/usr/share/nginx/html

/usr/local/webserver/nginx/conf/nginx.conf 配置

worker_processes  1; # 机子cpu核数

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root /usr/share/nginx/html;  ## 存放打包好的dist文件内容
            index  index.html index.htm;
        }
        ## 配置服务地址映射,对应hello.js的端口
        location /hello {
            proxy_pass http://127.0.0.1:8081;
        }

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

运行nodepm2 start hello.js ,执行成功界面如下:

浏览器访问
⚠️注意:访问前要确认防火墙是否关闭
关闭防火墙参考命令

访问nodeurl:http://[centos主机ip地址]/hello

访问页面url:http://[centos主机ip地址]/

常用命令

$ pm2 start app.js # 启动app.js应用程序

$ pm2 start app.js –name=”api” # 启动应用程序并命名为 “api”

$ pm2 start app.js –watch # 当文件变化时自动重启应用

$ pm2 start script.sh # 启动 bash 脚本

$ pm2 list # 列表 PM2 启动的所有的应用程序

$ pm2 monit # 显示每个应用程序的CPU和内存占用情况

$ pm2 show [app-name] # 显示应用程序的所有信息

$ pm2 logs # 显示所有应用程序的日志

$ pm2 logs [app-name] # 显示指定应用程序的日志

$ pm2 stop all # 停止所有的应用程序

$ pm2 stop 0 # 停止 id为 0的指定应用程序

$ pm2 restart all # 重启所有应用

$ pm2 reload all # 重启 cluster mode下的所有应用

$ pm2 gracefulReload all # Graceful reload all apps in cluster mode

$ pm2 delete all # 关闭并删除所有应用

$ pm2 delete 0 # 删除指定应用 id 0

$ pm2 scale api 10 # 把名字叫api的应用扩展到10个实例

$ pm2 reset [app-name] # 重置重启数量

$ pm2 startup # 创建开机自启动命令

$ pm2 save # 保存当前应用列表

$ pm2 resurrect # 重新加载保存的应用列表

$ pm2 update # Save processes, kill PM2 and restore processes

$ pm2 generate # Generate a sample json configuration file
posted @ 2022-02-23 17:08  antguo  阅读(754)  评论(0编辑  收藏  举报