ubuntu+nginx 下使用 letsencrypt 加密 https

455 阅读3分钟

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

本文基于 ubuntu16.04、nginx 环境

第一步:安装 Certbot

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

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

第二步: 获得 SSL 的证书

我们使用 WebRoot 这个插件。

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

vim /etc/nginx/sites-available/default

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

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

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

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

sudo nginx -t
sudo nginx -s reload

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

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,可以为别的域名。

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

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

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

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

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

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

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

内容如下:

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

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

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

内容如下:

# 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 的配置文件:

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

改成这样:

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

执行以下脚本:

sudo ufw allow 'Nginx Full'
sudo nginx -t
sudo nginx -s reload

第五步:自动更新 SSL 证书

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

执行命令:

sudo crontab -e

在最后加上这么一行:

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

完成!