为了加速https的普及,chrome浏览器将对所有的http网站打上红叉,因此为自己的网站加上一把安全锁,则变得越来越重要。

安装cerbot

1
2
3
4
5
6
7
8

# 下载
wget https://dl.eff.org/certbot-auto
# 给与执行权限
chmod a+x certbot-auto
# 设置软连接
ln -s certbot-auto /usr/local/bin/cerbot


上证书

停止nginx

1
nginx -s stop

生成证书

1
2
3
4
cerbot certonly --standalone --email <邮箱地址> -d <域名>
 
# 如果需要一次性生成多个网站
cerbot certonly --standalone --email <邮箱地址> -d <域名> -d <域名>

查看生成的证书

1
2
3
4
# 如果没安装tree,请先安装
yum install tree

tree /etc/letsencrypt/live/

将证书配置到网页上

在nginx相关的配置文件中增加

 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

server {
  # 更改开放端口
  listen 443 ssl;
  server_name gogs.jiangyixin.top;
  # 填上证书的路径
  ssl_certificate /etc/letsencrypt/live/<域名>/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/<域名>/privkey.pem;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_dhparam /etc/ssl/certs/dhparam.pem;
  ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
  ssl_session_timeout 1d;
  ssl_session_cache shared:SSL:50m;
  ssl_stapling on;
  ssl_stapling_verify on;
  add_header Strict-Transport-Security max-age=15768000;
  location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

检查配置文件

1
nginx -t

重新起用nginx

1
nginx

开放443端口

1
firewall-cmd --zone=public --add-prot=443/tcp --permanent

证书续签

Let’s Encrypt 生成的免费证书为3个月时间,但是我们可以无限次续签证书

1
2
3
4
5
crontab -e
# 每月一日凌晨3点更新证书
0 3 1 * * /usr/local/bin/cerbot renew >> /var/log/cerbot-renew.log
# 每月一日凌晨4点重启nginx
0 4 1 * * /usr/local/bin/nginx -s reopen

异常处理

如果在nginx -t出现unknown directive ssl_certificate 则nginx中没把SSL模块给编译进去,所以需要重弄下编译

重新编译nginx

首先安装必要的依赖

1
yum -y install openssl openssl-devel

进入当初下载nginx压缩包的解压目录

1
2
3
4
5
# 重新添加ssl模块
./configure --with-http_ssl_module

# 执行make(不需要执行make install)
make

执行后吧先备份原先的nginx,然后用新的nginx覆盖

1
2
3
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

cp objs/nginx /usr/local/nginx/sbin/nginx

最后,检查模块是否成功安装

1
nginx -V