記一次完整的項目部署

前段時間在公司部署項目的時候,運維同窗說了一堆關於服務器的東西,頓時感受不知所云,雲裏霧裏。。。html

banner

索性拿這個項目練習一下,簡單瞭解項目部署到服務器的這個流程是怎麼處理的node

預備知識

pm2

pm2 是啓動node進程管理工具react

經常使用命令

···nginx

pm2 start app.js : 啓動服務,入口文件是app.jsgit

pm2 list 查看有哪些進程啓動github

pm2 show xxx 查看某一個服務的詳情npm

npm restart [name or id] : 重啓服務json

pm2 monit : 對服務進行監控vim

···瀏覽器

一個項目的package.json 文件

package.json

平時啓動服務咱們能夠使用 node run app.js

若是藉助pm2 來啓動服務 就能夠輸入 pm2 start app.js

在命令行輸入 pm2 list 能夠查看正在運行的項目

pm2 list

pm2支持配置文件啓動

pm2配置文件

  • script 啓動腳本路徑
  • exec_mode 應用啓動模式,支持fork和cluster模式
  • instances 應用啓動實例個數,僅在cluster模式有效,默認爲fork

fork和cluster模式

fork爲單進程 cluster能夠啓動多個進程

在項目中新增一個pm2配置文件 pm2.config.json

{
  "name": "mxx-project",
  "script": "./index.js",
  "error_file": "./logs/err.log",
  "out_file": "./logs/out.log",
  "log_date_format": "YYYY-MM-DD HH:mm Z",
  "instances": 3,
  "merge_logs": true,
  "exec_mode": "cluster",
  "node_args": "",
  "ignore_watch": ["node_modules"],
  "env": {
    "NODE_ENV": "development"
  }
}
複製代碼

在命令行輸入

pm2 start pm2.config.json
複製代碼

則會有三個進程被建立

cluster

注: 若是你的服務器是多核的 那麼頗有可能在cluster 模式下 被建立多個進程

能夠查看一下本身的服務器是幾核的

cluster

shipit

shipit 是自動化的服務器部署工具

一個簡單的shipit配置文件

module.exports = shipit => {
  require('shipit-deploy')(shipit)
  shipit.initConfig({
    default: {
      workspace: '/tmp/myapp',
      deployTo: '/var/myapp',
      repositoryUrl: '你的GitHub地址',
      ignores: ['.git', 'node_modules'],
      keepReleases: 2,
      deleteOnRollback: false,
      key: '/path/to/key',
      shallowClone: true,
    },
    staging: {
      servers: '你的服務器地址',
    },
  })
}

複製代碼

鏈接服務器

購買服務器以後 獲得IP地址 能夠嘗試登錄服務器

通常本身的服務器能夠使用root身份登錄操做

ssh root@ipipip
複製代碼

因爲後面項目要部署到服務器中 建議能夠先作ssh-copy-id 創建信任 這樣就不用重複輸入密碼了

ssh-copy-id

在服務器上配置環境

通常來講 須要安裝 npm cnpm node pm2 git nginx

  • 安裝npm
yum install npm  
複製代碼
  • 安裝 cnpm
npm install -g cnpm --registry=https://registry.npm.taobao.org
複製代碼
  • 安裝pm2
npm i pm2 
複製代碼
  • 安裝node
npm i n
複製代碼
  • 安裝git
yum install git
複製代碼
  • 安裝nvm
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
複製代碼

須要調整 vim ~/.bashrc

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
複製代碼

指定默認node版本

nvm alias defalut 8
複製代碼

啓動一個簡易服務

咱們來寫一個簡單的腳本 在服務器上, 我這裏是在 /home/work 目錄下新建了一個app.js (依次執行)

cd home
mkdir work
touch app.js
複製代碼

在app.js中 寫一個最簡單的服務

const http = require('http')
const port = 3389

http.createServer((req, res) => {
  res.end('Hello word!')
}).listen(port, () => {
  console.log(port, 'port')
})

複製代碼

啓動app.js

node app.js
複製代碼

node

而後打開瀏覽器,輸入IP+端口號,就能在頁面中看到hello word了

hello word

--補充--

1 這裏使用的端口號是 3389 是由於個人服務器中 安全組中已經配置了 3389 這個端口號

若是你使用的是其他端口 要注意去查看有沒有配置安全組

2 關於啓動服務

若是使用的是node 那麼一旦退出服務器,則沒法再訪問此服務

能夠換成使用pm2 來啓動 這樣一旦啓動 除非報錯,將會一直有此進程存在

pm2

準備部署

  • 準備項目

項目是用koa1 + React

react的項目地址 點我查看GitHub地址

啓動的端口號調整到3389

  • 準備pm2啓動文件

建立pm2文件夾 在其中寫一個 production.json 用於啓動pm2

  • 在項目中建立一個 shipit.js 基本配置以下
module.exports = function (shipit) {
  require('shipit-deploy')(shipit)
  require('shipit-cnpm')(shipit)
  require('shipit-pm')(shipit)
  shipit.initConfig({
    default: {
      workspace: '/tmp/deploy/your-project',
      deployTo: '/home/work/your-project',
      repositoryUrl: 'https://github.com/youproject.git',
      ignores: ['.git', 'node_modules'],
      keepReleases: 2,
      deleteOnRollback: false,
      key: '/path/to/key',
      shallowClone: true,
      cnpm: {
        flags: '--production',
        local: false,
        npm: 'cnpm',
        remote: true
      },
      pm: {
        production: {
          path: '/home/work/your-project/current/pm2/production.json'
        }
      }
    },
    production: {
      servers: ['root@你的IP地址']
    }
  })
}
複製代碼

這裏 pm2 使用的配置文件來啓動 因此配置了 pm 參數 會根據這個文件路徑來啓動pm2

  • 在項目的package.json中 添加兩個字段
"deploy": "shipit production deploy",
    "rollback": "shipit production rollback",
複製代碼
  • 將項目push到GitHub中
  • 執行 npm run deploy

項目部署完畢後,去服務器查看下項目進程

pm2 list
複製代碼

pm2

打開頁面 看下效果

server

到目前爲止,算是部署完畢了

部署過程出現的問題

  • 報錯1

git

解決: 服務器上忘記安裝git了

  • 報錯2

cnpm

這個就很明顯了,沒有安裝 cnpm

  • 部署成功以後去服務器查看 項目並無啓動

原來部署完畢是

shipit

而後求助一位大神,發現是shipit配置文件中 沒寫

require('shipit-pm')(shipit)
複製代碼

[摔桌子!]

調整文件後從新上線

pm2

會啓動三個進程

根據項目端口號配置阿里雲

若是你的項目啓動時候端口號並無在安全組中配置,須要在後臺中添加安全組

9093

在訪問頁面的時候 還須要輸入端口號 3389。很是懶,不想輸入端口號

在安全組中添加默認端口80,將啓動文件app.js中端口調整爲80。這樣就不須要輸入端口號了

port

port

那若是不想修改app.js文件怎麼辦,使用NGINX

配置NGINX

  • 安裝 nginx

yum install nginx

查看NGINX配置文件

cat  /etc/nginx/nginx.conf
複製代碼

很簡單的配置

# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

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

}

複製代碼

將項目入口文件app.js中端口號調整爲 9999

處理Nginx.conf.js

最後調整爲 (比較重要的是server 這裏只展現server部分)

server {
        listen       80;
        server_name  localhost;
 
        location / {
          proxy_pass  http://127.0.0.1:9999/;
        }
    }
複製代碼

啓動Nginx

nginx -s reload
複製代碼

Nginx配置出現的問題

  • 重啓Nginx

在重啓Nginx的時候 一直報錯

error

查閱文章後處理

nginx -c /etc/nginx/nginx.conf
nginx -s reload
複製代碼

搞定~

歡迎訪問 點我查看項目

參考文章

相關文章
相關標籤/搜索