部署环境 Node + mongodb + nginx

images

序言

把你亲亲苦苦码出来的 “孩子” 买个家
还要给你的 “家” 购买个 “名字”
但是 “家” 啥都没,是时候为你的 “家” 做一次构建行动了
这次我是用ubuntu系统

IPtables && Fail2Ban : 给你的家做点安全措施

修改ssh port

进入sshd配置文件中:
sudo vim /etc/ssh/sshd_config

修改以下内容

1
2
3
4
5
6
7
8
# 修改端口 ssh 只能到这个端口进来,不能用0 ~ 1024, 可用范围 1024 ~ 65535
Port *****

# 把root登录关闭
PermitRootLogin no

# 这里修改你登录的用户
AllowUsers <你登录的用户>

重启你的ssh
sudo service ssh restart

IPtables 设置

这一步需要细心点,建议打开多个连接

清除预设表filter中的所有规则链的规则
sudo iptables -F

新建文件
sudo vim /etc/iptables.up.rules

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
*filter

# allow all connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# allow out traffic
-A OUTPUT -j ACCEPT

# allow http https
-A INPUT -p tcp --dport 443 -j ACCEPT
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 8081 -j ACCEPT

# allow ssh port login, ***** 是你通过ssh进入服务器的端口
-A INPUT -p tcp -m state --state NEW --dport ***** -j ACCEPT

# ping
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

# mongodb connect
-A INPUT -s 127.0.0.1 -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
-A OUTPUT -d 127.0.0.1 -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT

# log denied calls
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied:" --log-level 7

#防止DDOS攻击
-A INPUT -p tcp --syn -m limit --limit 12/s --limit-burst 24 -j ACCEPT
-A FORWARD -p tcp --syn -m limit --limit 1/s -j ACCEPT
#防CC攻击
#单个IP最大并发连接数为30
-I INPUT -p tcp --dport 80 -m connlimit --connlimit-above 30 -j REJECT
#单个IP在60秒内允许建立的连接数为60个
-A INPUT -p tcp --dport 80 -m recent --name BAD_HTTP_ACCESS --update --seconds 60 --hitcount 60 -j REJECT


# reject all other inbound
-A INPUT -j REJECT
-A FORWARD -j REJECT

COMMIT

确定无误后,执行
sudo iptables-restore < /etc/iptables.up.rules

启动防火墙
sudo ufw enable

让防火墙自动开启:
sudo vim /etc/network/if-up.d/iptables

fail2ban:

fail2ban防御性的动作库,通过监控系统的日志文件,根据检查到的任何可疑的行为触发不同的动作,例如给可疑的ip进行ip锁定。

  1. 安装fail2ban
    sudo apt-get install fail2ban

  2. 打开配置文件:
    sudo vim /etc/fail2ban/jail.conf

  3. 修改

    1
    2
    3
    bantime=3600
    destemail=<自己的邮箱>
    action=%(action_mw)s
  4. 查看fail2ban是否有运行:sudo service fail2ban status

安装 nvm (node版本管理)

nvm官方

  1. 执行

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
or
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash

  1. 打开新终端登录进去

  2. 输入

    1
    2
    nvm --version
    0.33.8

安装成功!

  1. 安装最新的node tls版本
    nvm install -v 8.9.4
    nvm use v8.9.4

  2. 设置node默认版本
    nvm alias default v8.9.4

修改npm国内镜像源并安装npm

  1. 安装
    npm --registry=https://registry.npm.taobao.org install -g npm

  2. 查看版本
    npm -v

  3. 修改sysctl

fs.inotify.max_user_watches:表示同一用户同时可以添加的watch数目(watch一般是针对目录,决定了同时同一用户可以监控的目录数量)

echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

  1. (可选)用cnpm替代npm
    npm --registry=https://registry.npm.taobao.org install -g cnpm

一般用npm网络不是太慢时,还是建议用npm。

如果是时用npm太慢时,可以用cnpm进行同步下载。
cnpm sync <backage-name>

SSL : 让每个人带着白帽子进来

Git 私有仓库 : 托儿所

使用 PM2 管理 node 服务进程

使用 PM2

  1. 新建文件app.js
    mkdir -p ~/webroot/app
    cd ~/webroot/app
    vim app.js
1
2
3
4
5
6
7
8
9
// app.js
const http = require('http')

http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'})
res.end('hello wrold')
}).listen(8081)

console.log('server runing successful!!')
  1. 启动pm2
    pm2 start app.js

  2. 查看pm2当前运行列表
    pm2 list

  3. 查看运行中的应用详情信息
    pm2 show <app-name>

  4. 查看日志
    pm2 logs

安装 nginx

解决当前问题,让外网能够访问80端口到我们的网站。
把nginx来自80端口的流量分配给node服务的端口。

  1. 因为在某云上可能会预装了apache,所以我们来将他删除掉
    update-rc.d -f apache2 remove
    sudo apt-get remove apache2

  2. 安装 nginx
    sudo apt-get install nginx

  3. 查看 nginx 版本
    nginx -v

  4. 在/etc/nginx/conf.d文件中新增配置文件
    sudo vim /etc/nginx/conf.d/node-app-8081.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
upstream app {
server 127.0.0.1:8081;
}

server {
listen 80;
server_name localhost;

location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;

proxy_pass http://app;
proxy_redirect off;
}
}
  1. 修改 /etc/nginx/nginx.conf 文件
    sudo vim /etc/nginx/nginx.conf

将下面的#去掉,意思是 去掉server的版本信息加载conf.d文件下的所有.conf后缀的配置文件

1
2
3
#server_tokens off;
#include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;

  1. 使用 nginx -t 查看刚刚配置的文件是否有误
    sudo nginx -t

显示一下证明没问题

1
2
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启nginx
sudo nginx -s reload
or
sudo service nginx reload

安装 mongodb

mongodb官网

ubtuntu install mongodb

根据官方提供的命令行进行下载和安装。

启动mongod
sudo service mongod start

停止mongod
sudo service mongod stop

重启mongod
sudo service mongod restart

修改端口

  1. 进入conf配置
    sudo vim /etc/mongod.conf

  2. 修改port端口
    port: 27017 => port: 19999

  3. 修改防火墙

  4. 重新载入防火墙

  5. 指定端口进入mongodb
    mongo --port 19999

  6. 数据库导入
    mongorestore -d
    : 数据库名字
    : 数据库备份目录

  7. 进入mongodb
    mongo

use <db-name>

show tables