pm2經過配置文件部署nodejs代碼到服務器

1、pm2文檔地址:

  http://pm2.keymetrics.io/node

2、在本地項目下新建ecosystem.config.js文件,而且推送到遠端(記得node_modules這個文件夾也得推送,在.gitignore裏面刪除):

module.exports = {
  apps: [{
    name: "blogserver",
    script: "./code/server/index.js",
    env: {
      "NODE_ENV": "production"
    },
    env_production: {
        "NODE_ENV": "production"
    }
  }],
  deploy: {
    production: {
      user: "root",
      host: ["47.94.208.76"],
      port: "8290",
      // SSH options with no command-line flag, see 'man ssh'
      // can be either a single string or an array of strings
      ssh_options: "StrictHostKeyChecking=no",
      // GIT remote/branch
      ref: "origin/master",
      // GIT remote
      repo: "git@gitee.com:tian_zz/blog.git",
      // path in the server
path: "/www/my-blog/blogserver",
        "post-deploy": "npm install --registry=https://registry.npm.taobao.org && pm2 startOrRestart ecosystem.config.js --env production",
      env: {
        "NODE_ENV": "production"
      }
    },
  }
}

3、在服務器端根目錄新建文件夾/www/my-blog/

  mkdir /wwwnginx

  cd /wwwgit

  mkdir my-blognpm

  cd my-blog後端

4、在本地運行pm2命令,經過配置文件鏈接服務器,而且通知服務器拉取代碼:

  一、本地 pm2 deploy ecosystem.config.js production setup服務器

  二、服務器端會看到多出的文件夾:    app

5、在本地運行pm2:

  pm2 deploy ecosystem.config.js productionssh

 6、在服務器端能夠看到pm2運行的進程:

   pm2 list  tcp

  發現報錯,打印下日誌:pm2 logs blogserver  post

  沒有找到模塊,進入服務器/www/my-blog/blogserver/source目錄:npm install 安裝包,再回到本地從新部署一下 pm2 deploy ecosystem.config.js production,回到服務器:部署成功。

7、在阿里雲後臺添加域名記錄,須要時間生效:  

8、若是本地後端是在3000端口下,在防火牆容許3000端口訪問:

  firewall-cmd --zone=public --add-port=3000/tcp --permanent

  firewall-cmd --permanent --list-port

  firewall-cmd --reload

9、配置nginx代理,將3000端口轉發到服務器的80端口

  cd /etc/nginx/conf.d

upstream locusy {
        server 127.0.0.1:3000;
}

server {
        listen 80;
        server_name www.locusy.top;

        location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-Nginx-Proxy true;

                proxy_pass http://locusy;
                proxy_redirect off;        }
}

 重啓nginx:nginx -s reload

相關文章
相關標籤/搜索