node服务器部署系列———自动化


在进行node部署完成后,日常开发运维中免不了需要经常需要更新维护服务器代码,但是一次又一次的登录服务器,手动更新代码实在是非常麻烦,因此我们需要一套自动化部署方案,大家经常用的方案有很多,在此只讲讲PM2的自动化

准备

本地: pm2 git node(npm) ssh(生成密匙)
服务器:pm2 git node(npm) nginx ssh(生成密匙)
本地可以clone / push(拉取/提交)代码到git
服务器可以clone远程git的代码

设置部署配置文件 ecosystem.json

在项目根目录生成ecosystem.json文件,用作配置

{
    "apps" : [{
      "name": "server API",    //项目名称
      "script": "app.js",       //启动入口文件
      "env": {
        "COMMON_VARIABLE": "true"
      },
     "env_production" : {
        "NODE_ENV": "production"
      }
    }],
   "deploy" : {
      "production" : {
        "user" : "root",                      //登录服务器用户名
        "host" : ["xxx.xxx.xxx.xxx"],         //服务器地址ip
        "port": "22",                         //登录的ssh端口
        "ref"  : "origin/master",             //服务器应该拉取的分支
        "repo" : "[email protected]:chuang_zao_zhi/server-api.git",  //服务器应该拉取的git地址
        "path" : "/www/server",               //拉取后放置的目录位置
        "post-deploy" : "npm install",        //拉取完后要执行的指令,多个指令用 “ ; ”分割
        "ssh_options": "StrictHostKeyChecking=no",
        "env"  : {
          "NODE_ENV": "production"
        }
      }
    }
  }

上面是经典的PM2部署配置文件,创建完成后就可以进行下一步安装部署:

先确保git代码为最新

pm2 deploy ecosystem.json production setup
本地运行完上面指令后,PM2会在目标目录生成项目部署文件,中间会需要多次输入服务器密码,按要求输入即可

部署完后再来进行发布:
运行pm2 deploy ecosystem.json production
上面命令会发布最新的git代码到服务器部署就此完成

有几个需要注意的点

  1. 次部署完后服务器使用命令pm2 list查看项目状态是否为online,如果不是,手动pm2 restart启动下,以后更新代码仅仅需要运行pm2 deploy ecosystem.json production即可

  2. 行部署时会多次要求输入服务器密码,非常麻烦,如果不想要输入可以使用ssh免蜜方式登录,就像git的ssh一样,通常使用ssh-copy-id来进行密匙复制
    使用
    ssh-copy-id -i .ssh/id_rsa.pub [email protected]
    将公钥复制到远程机器中(ssh-copy-id 将key写到远程机器的 ~/.ssh/authorized_key.文件中)
    ssh-copy-id在linux里自动带有,在mac上可以通过brew安装,但唯独没有在windows上有支持,所以我们需要自己创建一个.sh脚本文件来运行命令:
    创建ssh-copy-id.sh脚本文件,写入一下内容,一般放在ssh目录附近,便于使用

#!/bin/sh

# Shell script to install your public key on a remote machine
# Takes the remote machine name as an argument.
# Obviously, the remote machine must accept password authentication,
# or one of the other keys in your ssh-agent, for this to work.

ID_FILE="${HOME}/.ssh/id_rsa.pub"

if [ "-i" = "$1" ]; then
  shift
  # check if we have 2 parameters left, if so the first is the new ID file
  if [ -n "$2" ]; then
    if expr "$1" : ".*\.pub" > /dev/null ; then
      ID_FILE="$1"
    else
      ID_FILE="$1.pub"
    fi
    shift # and this should leave $1 as the target name
  fi
else
  if [ x$SSH_AUTH_SOCK != x ] ; then
    GET_ID="$GET_ID ssh-add -L"
  fi
fi

if [ -z "`eval $GET_ID`" ] && [ -r "${ID_FILE}" ] ; then
  GET_ID="cat ${ID_FILE}"
fi

if [ -z "`eval $GET_ID`" ]; then
  echo "$0: ERROR: No identities found" >&2
  exit 1
fi

if [ "$#" -lt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
  echo "Usage: $0 [-i [identity_file]] [user@]machine" >&2
  exit 1
fi

{ eval "$GET_ID" ; } | ssh $1 "umask 077; test -d ~/.ssh || mkdir ~/.ssh ; cat >> ~/.ssh/authorized_keys && (test -x /sbin/restorecon && /sbin/restorecon ~/.ssh ~/.ssh/authorized_keys >/dev/null 2>&1 || true)" || exit 1

cat <<EOF
Now try logging into the machine, with "ssh '$1'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

EOF

这样就能愉快的使用 ssh-copy-id了,运行后会发现本地登录服务器将不再需要键入密码,可以直接登录

本次讲述使用root用户,如果使用自定义的用户,记得给新的用户相关目录权限,或直接给予管理员最高权限,防止权限不足

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章