ubuntu+nginx下使用letsencrypt加密https

因为服务器太乱,我清理了一下服务器并且重新使用letsencrypt加密了https,现在将我的经验分享出来。

本文基于ubuntu16.04、nginx环境

第一步:安装 Certbot

第一步是安装letsencrypt提供的certbot工具

1
2
3
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install certbot

第二步: 获得SSL的证书

我们使用WebRoot这个插件。

这里以nginx的default的site作为示例:

1
vim /etc/nginx/sites-available/default

在server的块中,加入以下内容

1
2
3
location ~ /.well-known {
allow all;
}

确认root是你网站的根目录,比如默认情况下是/var/www/html

保存退出之后,测试并重启你的nginx:

1
2
sudo nginx -t
sudo nginx -s reload

然后我们获取到相关的SSL证书:

1
sudo certbot certonly --webroot --webroot-path=/var/www/html -d example.com -d www.example.com -d third.another.com

记得把上面的/var/www/html改成你自己的网站根目录。如果需要同时对多个域名进行认证的话只要同时使用多个-d就可以了,并且这些域名并不一定都需要为example.com,可以为别的域名。

然后根据提示,输入对应的信息,如果完成后应该会看到类似的信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/example.com/fullchain.pem. Your cert
will expire on 2017-07-26. To obtain a new or tweaked version of
this certificate in the future, simply run certbot again. To
non-interactively renew *all* of your certificates, run "certbot
renew"
- If you lose your account credentials, you can recover through
e-mails sent to sammy@example.com.
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- If you like Certbot, please consider supporting our work by:

Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le

认证成功后,我们来生成一下更强的dhparam

1
sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048

这一步应该会消耗一定的时间

第三步:在nginx上设置TLS/SSL

我们先创建一个新的脚本:

1
sudo vim /etc/nginx/snippets/ssl-example.com.conf

内容如下:

1
2
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

保存退出后,再创建一个脚本用来设置ssl的参数:

1
sudo vim /etc/nginx/snippets/ssl-params.conf

内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# from https://cipherli.st/
# and https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
ssl_ecdh_curve secp384r1;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# disable HSTS header for now
#add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;

ssl_dhparam /etc/ssl/certs/dhparam.pem;

保存退出。

然后修改一下site的配置文件:

1
sudo vim /etc/nginx/sites-available/default

改成这样:

1
2
3
4
5
6
7
8
9
10
11
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;

server_name example.com www.example.com;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;

. . .

保存退出。

第四步:在防火墙中Allow Nginx

执行以下脚本:

1
2
3
sudo ufw allow 'Nginx Full'
sudo nginx -t
sudo nginx -s reload

第五步:自动更新SSL证书

因为letsencrypt提供的证书是有期限的,所以我们需要设置自动更新证书。

执行命令:

1
sudo crontab -e

在最后加上这么一行:

1
30 0 * * 1 /usr/bin/certbot renew --quiet --renew-hook "/bin/systemctl reload nginx"

完成!