配置 Vue-Yarn-PM2 工具环境
说明 本地环境是 Mac , 服务器环境是 Centos7.x, 开始之前,请自带翻墙
本地环境
nvm node git pm2 yarn
复制代码
首先安装 brew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
复制代码
安装 Nvm
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
复制代码
然后 使用
command -v nvm
复制代码
重新启动 终端 查看版本
nvm -v
复制代码
查看可装的 Node 版本
nvm ls-remote
复制代码
安装最新版本的 Node
nvm install v10.6.0
nvm alias default v10.6.0
复制代码
安装 git
去这个网站下载, 然后无脑下一步即可
https://git-scm.com/
复制代码
安装完成 git 先进行配置
git config --global user.name "mhkz"
git config --global user.email '[email protected]'
复制代码
生成 ssh-key
ssh-keygen -t rsa -b 4096 -C "[email protected]"
复制代码
生成key的时候,会提示创建密码,不用管,一路回车,回车三次即可
然后把生成的 public key 添加到 github ,防止每次提交都得重新输入密码 利用
cat ~/.ssh/id_rsa.pub
复制代码
将得到的字符串整体复制下来, 然后粘贴到 Settings下的 SSH and GKG keys 中,标题随便写,自己能够区别就好
在 github 上新建仓库,并且复制仓库地址
本地生成新的仓库
git init
git add README.md
git commit -m "initial commit"
git remote add origin https://github.com/mhkz/project.git
git push -u origin master
复制代码
ok 本地项目已经生成并且上传到 github 上
安装 pm2 部署工具
npm install pm2 -g
复制代码
查看开启的 Node 服务
pm2 list
复制代码
服务器 环境
服务器环境比较复杂,不仅需要上述的所有步骤,还需要 Nginx 反向代理,以及 SSH 免密登陆, 不然我们每次部署都需要输入密码,那就太麻烦了
yarn nvm node pm2 git nginx
复制代码
其中前四种安装步骤跟 Mac 下安装方法大同小异,不再详叙
现在重点说说 Nginx 的安装和代理的设置
Nginx
Nginx 在 Centos7 下有两种安装方式,第一种源码安装,第二种 yum 安装,这里采用第一种,源码安装‘
- 下载并且解压
wget http://nginx.org/download/nginx-1.10.1.tar.gz
tar -zxvf nginx-1.10.1.tar.gz
复制代码
- 进入 解压后的文件夹进行编译
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
复制代码
- 编译
make && make install
复制代码
- 启动
cd /usr/local/nginx/sbin
./nginx
复制代码
- 关闭
./nginx -s stop
复制代码
nginx 端口代理与域名指向
进入conf 目录,在该目录下创建include 文件。进入 conf/include 目录,创建 nginx.node.conf 文件,在里面输入如下代码:
upstream nodejs {
server 127.0.0.1:3000;
keepalive 64;
}
复制代码
server {
listen 80;
server_name p.iquanku.com 47.95.7.29;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
proxy_pass http://nodejs;
}
}
复制代码
进入conf ,打开nginx.conf, 在http 里面添加 。
include ./include/*
复制代码
重启nginx , 输入
nginx -c conf/nginx.conf
复制代码
- 利用 Node 创建新项目并且启动 在服务器任何地方 创建 server.js 文件
vim server.js
复制代码
把 Node 官网事例粘贴进去
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('您好, 我是 大白--------');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
复制代码
利用 pm2 启动
pm2 server.js start
复制代码
在浏览器中输入 p.iquanku.com 或者服务器域名, 便是能看到返回的 “您好, 我是 大白--------”这写内容
上面所有内容告一段落,接下来配置自动化仓库来部署项目
复制代码
首先把服务器 所有 Node 项目停掉
pm2 kill
复制代码
pm2 list
复制代码
配置服务器免密登陆
进入本地终端
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
复制代码
重启控制台 不用输入密码,便可以利用直接连到服务器
ssh [email protected]
复制代码
在本地最开始初始化的项目中创建两个文件 一个是 ecosystem.json文件, 一个是 server.js文件
ecosystem.json 内容
{
"apps": [{
"name": "new-doc",
"script": "server.js",
//"instances": 2 服务器核心数配置
"env": {
"COMMON_VARIABLE": "true"
},
"env_production": {
"NODE_ENV": "production"
}
}],
"deploy": {
"production": {
"user": "root",
"host": ["47.95.7.29"],
"port": "22",
"ref": "origin/master",
"repo": "https://github.com/mhkz/project.git",
"path": "/www/doc/production",
"ssh_options": "StrictHostKeyChecking=no",
// "post-deploy": "npm install"
"pre-deploy-local": "echo 'Deploy Done'",
"env": {
"NODE_ENV": "production"
}
}
}
}
复制代码
server.js 内容
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('您好, 我是 大白--------');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
复制代码
然后把项目内容提交到 github 上
git add .
git commit -m "deploy project test"
git push origin master
复制代码
- 一健初始化部署发布项目
pm2 deploy ecosystem.json production setup
复制代码
- 发布项目
pm2 deploy ecosystem.json production
复制代码
打开浏览器输入 p.iquanku.com 看到了输出内容
- 修改本地文件内容
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('为什么路飞的橡胶果实不惧怕闪电');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
复制代码
- 再次发布 内容变化
pm2 deploy ecosystem.json production
复制代码
这样,每次修改完成项目提交后,不需要服务器做任何操作,把代码合并到主分支,测试完成后,直接一健部署即可。
- pm2 开机自启
pm2 startup centos
复制代码
别的命令用到自行 Google ,这里不再累述
个人公众号