mongodb操作

Created at 2016-10-07 Updated at 2016-10-17 Category 学习 Tag 后台

项目部署:典型的 nodejs 全栈项目

Google 的设计规范

  • materialup.com

申请 aliyun 服务器

国外还有 AWS 可以用。

登陆

1
ssh root@139.196.28.83

创建一个新的普通用户

1
adduser peter --ingroup sudo

切换到普通用户

1
su peter

退出登陆

Ctrl-D

绑定域名 DNS

http://godaddy.com/ 上可以买到域名。但是我们这里模拟一下本地域名

1
sudo vim /etc/hosts

填写

1
139.196.28.83 peter.com

从 github clone 代码到服务器

安装 git

1
sudo apt-get install git
1
git clone xxx.git

安 nodejs

使用 nvm 来安装

安装 curl

1
2
sudo apt-get update
sudo apt-get install -y curl

运行安装 nvm 的脚本

1
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash

重新加载 nvm

1
source .bashrc

列出所有可以安装的 node 版本

1
nvm ls-remote

执行安装命令

1
nvm install v6.8.0

列出所有系统上的 node 版本

1
nvm ls

开始部署 client

安装所有依赖包

1
2
cd digicity-express-api/client/
npm install # 使用 cnpm 会更快些

重新映射端口

1
npm run build # 得到 bundle.js

现在需要启动 server.js

1
2
3
4
mkdir public
cd public
mv ../dist/bundle.js .
mv ../index.html # 修改里面的 bundle.js 的路径

现在需要把 8080 端口,映射到 80(80端口是 http 服务的默认端口,通常都是默认打开的)

首先安装 nginx 服务器

1
sudo apt-get install -y nginx

先删除 default 页面

1
2
cd /etc/nginx/site_enabled/
sudo rm default

使用 vim 创建自己的配置文件

1
sudo vim client.conf

填写内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
listen 80 default;
server_name client.haoqicat.com;
location / {
proxy_pass http://localhost:8000;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_x_forwarded_host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 3m;
proxy_send_timeout 3m;
}
}

重启 nginx

1
sudo service nginx reload

现在 client 已经可以跑起来了,但是,如果我自己的笔记本的命令行一关闭,那么服务器就死掉了,这个怎么来解决呢?

所以说,现在我需要一种形式来维持服务器上的命令行不死。

使用 tmux

tmux 可以帮助我们维持远端的多个命令行,保持活跃状态。

安装

1
sudo apt-get install tmux

使用就是

1
tmux

然后在 tmux 之中打开的命令行,就不会因为我们自己的命令行关闭而死掉了。

再次登录的时候,执行

1
2
tmux a
# 退出 tmux 采用 ctrl-a-d/ctrl-b-d

就可以再次看到那个命令行了。参考:http://haoduoshipin.com/v/41

部署 server

现在登录到服务器,再来启动一个 tmux 的会话

ctrl-a-c/ctrl-b-c

然后在新会话中启动 server 代码

1
2
3
cd ~/digicity-express-api/server
cpm i
node index.js # 提前保证 mongodb 是启动的

现在来进行端口映射

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server{
listen 80;
server_name api.haoqicat.com;
location / {
proxy_pass http://localhost:3003;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_x_forwarded_host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 3m;
proxy_send_timeout 3m;
}
}

TIP: 在各个 tmux 会话之间跳转

ctrl-b-p/ctrl-b-n

这样,server 端就启动了

链接前后端

修改 settings.js 文件,最终内容如下

1
2
3
4
5
const Settings = {
host: 'http://api.haoqicat.com'
}
export default Settings;

重新打包 bundle.js 。

大功告成

这样,前后端就可以协同工作了。

Site by Mickey using Hexo & Random

Hide