服务器购买 & 配置
打开阿里云, 选择购买云服务器 ECS, 这里可以选择一键购买进行快速配置, 操作系统选择 CentOS 7.2 64 位, 其他默认或根据实际需求来, 若选择自定义购买请自行搜索;
购买成功设置账号密码后, 就可以通过 ftp 工具 (我用的是 FileZilla) 或者 Git 连接我们的服务器了, 这个时候我们也可以打开阿里云的控制台 / 云服务器 ECS 查看购买的服务器;
打开阿里云的控制台 / 云服务器 ECS / 网络和安全 / 安全组, 在安全组列表点击配置规则, 点击快速创建规则, 就可以暴露端口了. 比如暴露 80 端口, 选择 HTTP(80), 授权对象填 0.0.0.0/0, 其他默认就可以了. 暴露其他端口你就在自定义端口选择, 比如暴露 7001 端口, 你就在自定义端口选择 TCP, 输入 7001/7001 即可;
开启 node 性能平台, 点击创建新应用按照操作提示来就行, 成功开启后在项目配置 (具体配置看下文)) 就可以监控数据了.
连接服务器
1.Git 连接
# SSH remote_username[@remote_ip](/user/remote_ip) 然后输入密码即可
如果 SSH 不存在, 执行以下命令即可
# yum install openssh-client 下载客户端 SSH
2.ftp 工具连接(这里以 FileZilla 为例), 下载 filezilla 后, 点击新建站点, 输入主机 ip, 选择 sftp 协议, 选择登录类型为正常, 输入账号密码即可.
部署 node 环境
1. 部署 node 环境
- # SSH remote_username[@remote_ip](/user/remote_ip) 连接服务器
- # wget https://nodejs.org/dist/v6.9.5/node-v6.9.5-linux-x64.tar.xz 下载 node 压缩文件
- # tar xvf node-v6.9.5-Linux-x64.tar.xz 解压
- # ln -s /root/node-v6.9.5-Linux-x64/bin/node /usr/local/bin/node 创建软连接
- # ln -s /root/node-v6.9.5-Linux-x64/bin/NPM /usr/local/bin/NPM 创建软连接
- # node -v 查看 node 版本
- # NPM -v 查看 NPM 版本
2. 其他
# yum install VIM 下载 VIM
nginx 安装 & 配置
1.nginx 安装
- # yum install epel-release 下载 epel-release
- # yun install nginx 下载 nginx
- # cd /etc/nginx
- # VIM nginx.conf 用 VIM 打开 nginx.conf
2. 修改 nginx.conf
修改 user 为 root
修改 server 如下, 这里 admin 是指向管理后台项目, App 是指向移动端项目, wx 是指向代理另一个 node 微信公众号项目(运行在 7002 端口, 但微信公众号配置 http 只允许 80 端口, 所以设置代理, 我们的 elm 接口运行在 7001 不用代理)
- server {
- listen 80 default_server;
- listen [::]:80 default_server;
- server_name _;
- root /root/www/;
- # Load configuration files for the default server block.
- include /etc/nginx/default.d/*.conf;
- location /admin/ {
- # 为空即可, 前端打包文件放在 / root/www/admin 里面就可访问到, 注意默认是 index.html
- }
- location /App/ {
- }
- location /wx/ {
- proxy_pass http://127.0.0.1:7002/;
- }
- error_page 404 /404.HTML;
- location = /40x.HTML {
- }
- error_page 500 502 503 504 /50x.HTML;
- location = /50x.HTML {
- }
- }
3. 启动 nginx
nginx -t 测试 nginx 语法是否有误
nginx 启动 nginx
nginx -s reload 重启 nginx, 修改 nginx.conf 后记得重启
4. 其他命令
ps -ef | grep nginx 显示 nginx 进程
nginx -s stop 停止 nginx
nginx -v 查看 nginx 版本
部署 MongoDB
1. 安装 MongoDB
- # SSH remote_username[@remote_ip](/user/remote_ip) 连接服务器
- # curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz 下载
- # tar -zxvf MongoDB-Linux-x86_64-3.0.6.tgz 解压
- # mkdir data 创建数据库文件夹
- # touch MongoDB.log 创建日志文件
- # cd /usr/local/MongoDB/bin
- # ./mongod -dbpath=/usr/local/MongoDB/data -logpath=/usr/local/MongoDB/MongoDB.log -logappend -port=27017 -fork 注意 fork 是后台启动, 避免又要再开窗口重新连接服务器再能进行其他操作
- # ./mongo 连接 MongoDB
2. 配置随 Linux 启动
在 / etc/rc.local 添加如下即可:
- # rm /usr/local/MongoDB/data/mongod.lock 停止可能在运行的 mongo
- # /.../bin/mongod -dbpath /usr/local/MongoDB/data -logpath /usr/local/MongoDB/MongoDB.log -logappend -fork -port 27017
3. 设置权限
- # cd /usr/local/MongoDB/bin
- # ./mongod -dbpath=/usr/local/MongoDB/data -logpath=/usr/local/MongoDB/MongoDB.log -logappend -port=27017 -fork
- # ./mongo
- > use admin
- > db.createUser(
- > {
- > user: "root",
- > pwd: "123456",
- > roles: [ {
- role: "root", db: "admin"
- } ]
- >
- }
- > )
- > db.shutdownServer();
- # ./mongod -dbpath=/usr/local/MongoDB/data -logpath=/usr/local/MongoDB/MongoDB.log -logappend -port=27017 -fork --auth
- # db.auth("root","123456")
4. 项目中连接 MongoDB(这里以 koa 框架 egg 项目为例, 其他 node 请自行查找)
- # cnpm i egg-mongoose -S
- // config/plugin.JS
- exports.mongoose = {
- enable: true,
- package: 'egg-mongoose',
- }
- // config/config.default.JS
- config.mongoose = {
- url: 'mongodb://127.0.0.1/eggadmin',
- options: {
- // 如果设置了密码
- // auth: { "authSource": "admin" },
- // user: "root",
- // pass: "123456",
- }
- }
部署 node 项目
部署环境 阿里云 CentOS 7.2 64 位
1. 本地项目根目录(删除 node_modules, 建议依赖在服务器下载)
- # tar -zcvf ../file_name.tgz . 打包
- # scp ../file_name.tgz remote_username[@remote_ip](/user/remote_ip):/root/www/server 上传到服务器
2. 服务器
- # SSH remote_username[@remote_ip](/user/remote_ip) 连接服务器
- # cd /root/www
- # mkdir server 这里创建 server 文件夹放 node 项目代码
- # cd server
- # tar -zxvf file_name.tgz . 解压
- # cnpm install --production 安装生产环境依赖
1. koa 项目(express 项目类似)
- # cnpm i -g pm2 下载 pm2
- # pm2 start bin/www 守护进程启动
- # pm2 restart app_name|app_id 重启
- # pm2 stop app_name|app_id 停止
- # pm2 list 查看进程状态
- # pm2 stop all 停止所有应用
- # pm2 start ./bin/www --watch 监听更改自动重启
2. egg 项目
- # NPM start 运行
- # NPM stop 停止
3. 阿里 node 性能平台监控
1)koa 项目(express 项目类似)
- # wget -O- https://raw.githubusercontent.com/aliyun-node/tnvm/master/install.sh | bash 安装版本管理工具 tnvm
- # source ~/.bashrc
- # tnvm ls-remote alinode 查看需要的版本
- # tnvm install alinode-v3.11.4 安装需要的版本
- # tnvm use alinode-v3.11. 使用需要的版本
新建 config.JSON 文件如下, 从 node 性能平台获取对应的接入参数
- {
- "appid": "",
- "secret": ""
- }
- # cnpm install [@alicloud](/user/alicloud)/agenthub -g 安装 agenthub
- # agenthub start config.JSON 启动 agenthub
- # agenthub list 查看 agenthub 列表
- # ENABLE_NODE_LOG=YES pm2 start bin/www 使用 pm2 管理的应用
2)egg 项目
- # cnpm i nodeinstall -g
- # nodeinstall --install-alinode ^3
- # cnpm i egg-alinode --save
- # NPM start
- // config/plugin.JS
- exports.alinode = {
- enable: true,
- package: 'egg-alinode',
- };
- // config/config.default.JS
- config.alinode = {
- appid: '',
- secret: '',
- };
部署前端项目(这里以 vue 为例)
在项目根目录 NPM run build, 然后把 dist 文件夹里的内容传到服务器, 这里我们把两个项目分部传到 / root/www/App 和 / root/www/admin, 记得提前创建 App 和 admin 文件夹.
来源: http://server.51cto.com/sOS-590518.htm