美文网首页
nodejs项目部署上线(nginx版)

nodejs项目部署上线(nginx版)

作者: 出来打我略略略 | 来源:发表于2020-07-07 09:15 被阅读0次

上线

  • 确定服务器已经安装了nginx, nginx -v,如果没有安装, 执行 yum install nginx (centos7.x为例)

  • 通过命令行或者可视化工具连接服务器(windows推荐WinScp,mac推荐FileZilla),将项目传到服务器上(注意不要传node_modules,特别耗时

  • 服务器安装node环境 yum install nodejs

  • 安装cnpm或者设置npm淘宝镜像(懂得自然懂)

  • 安装pm2 cnpm install pm2 -g, 查看pm2是否安装成功 pm2 -v,如果报错,升级node版本

    npm cache clean -f      //第一步:先清除npm缓存:
    npm install -g n        //第二步:安装n模块:
    n stable                //第三步:升级node.js到最新稳定版:
  • 进入node项目目录,安装项目依赖 cnpm install

  • 创建pm2任务 pm2 start ./bin/www --watch

  • 查看任务列表及状态 pm2 list

  • 重启nginx nginx -s reload

  • 打开网址(服务器ip:3000),node默认是3000端口,如果被占用可在 ./bin/www 文件中更改端口

绑定域名

  • 新建nginx配置文件,写入反向代理配置
    upstream web{
        server 127.0.0.1:3000;
        keepalive 64;
    }
    server {
        listen 80;
        server_name 域名;
        #access_log /var/log/nginx/log/host.access.log main;
        #client_max_body_size 100m;
        location / {
            proxy_read_timeout 300;
            proxy_pass http://web;
            proxy_set_header Host $http_host;
        }
        error_page 404 /404.html;
        location = /50x.html {
            root /usr/share/nginx/html;
            proxy_set_header Host $http_host;
        }
    }
  • 重启nginx nginx -s reload

  • --- END ---

相关文章

网友评论

      本文标题:nodejs项目部署上线(nginx版)

      本文链接:https://www.haomeiwen.com/subject/hhfuahtx.html