Let's Encrypt是由互联网安全研究组(ISRG)开发的免费开放认证机构。 Let's Encrypt颁发的证书现在几乎所有浏览器都信任。
在本教程中,我们将逐步提供关于如何使用Ubuntu 18.04上的certbot工具使用Let's Encrypt保护Nginx的说明。
先决条件
在继续本教程之前,请确保您已满足以下先决条件:
- 你有一个指向你的公共服务器IP的域名。 在本教程中,我们将使用example.com。
- 通过这里的介绍安装Nginx https://www.linuxidc.com/Linux/2018-05/152257.htm
- 你的域名有一个服务器模块。 你可以按照这个介绍了解如何创建一个。 https://www.linuxidc.com/Linux/2018-05/152258.htm
安装Certbot
更新软件包列表并安装certbot软件包:
sudo apt update sudo apt install certbot
生成强大的Dh(Diffie-Hellman)组
Diffie-Hellman密钥交换(DH)是通过不安全的通信信道安全地交换加密密钥的方法。 我们将生成一组新的2048位DH参数以加强安全性:
sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048
如果你喜欢,你可以改变大小到4096位,但在这种情况下,生成可能需要超过30分钟,这取决于系统熵。
获取SSL证书
要获取我们域的SSL证书,我们将使用Webroot插件,该插件通过在${webroot-path}/.well-known/acme-challenge目录中创建所请求域的临时文件以及Let's Encrypt 验证服务器会使HTTP请求验证所请求域的DNS是否解析到运行certbot的服务器。
为了使它更简单,我们将把.well-known/acme-challenge的所有HTTP请求映射到单个目录/var/lib/letsencrypt。 以下命令将创建该目录并将其设置为可写入Nginx服务器。
mkdir -p /var/lib/letsencrypt/.well-known chgrp www-data /var/lib/letsencrypt chmod g s /var/lib/letsencrypt
为避免重复代码,请创建以下两个片段,我们将在所有Nginx服务器块文件中使用这两个片段:
/etc/nginx/snippets/letsencrypt.conf
location ^~ /.well-known/acme-challenge/ { allow all; root /var/lib/letsencrypt/; default_type "text/plain"; try_files $uri =404; }
/etc/nginx/snippets/ssl.conf
ssl_dhparam /etc/ssl/certs/dhparam.pem;
ssl_session_timeout 1d; ssl_session_cache shared:SSL:50m; ssl_session_tickets off;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS'; ssl_prefer_server_ciphers on;
ssl_stapling on; ssl_stapling_verify on; resolver 8.8.8.8 8.8.4.4 valid=300s; resolver_timeout 30s;
add_header Strict-Transport-Security "max-age=15768000; includeSubdomains; preload"; add_header X-Frame-Options SAMEORIGIN; add_header X-Content-Type-Options nosniff;
上面的代码片段使用了Mozilla的chippers recomendend,支持OCSP Stapling,HTTP严格传输安全性(HSTS),并强制实施少量以安全为重点的HTTP标头。 一旦创建了片段,打开域服务器块并包含letsencrypt.confsnippet,如下所示:
/etc/nginx/sites-available/example.com
server { listen 80; server_name example.com www.example.com;
include snippets/letsencrypt.conf; }
重新加载Nginx配置以使更改生效:
sudo systemctl reload nginx
您现在可以运行带有webroot插件的Certbot,并通过发出以下命令获取SSL证书文件:
sudo certbot certonly --agree-tos --email admin@example.com --webroot -w /var/lib/letsencrypt/ -d example.com -d www.example.com
如果SSL证书成功获取,certbot将打印以下消息:
IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/example.com/privkey.pem Your cert will expire on 2018-07-28. 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" - 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
现在您已拥有证书文件,您可以按如下方式编辑域服务器块:
/etc/nginx/sites-available/example.com
server { listen 80; server_name www.example.com example.com;
include snippets/letsencrypt.conf; return 301 https://hostrequest_uri;}
server { listen 443 ssl http2; server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem; include snippets/ssl.conf;
return 301 https://example.com$request_uri; }
server { listen 443 ssl http2; server_name example.com;
# . . . other code
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem; include snippets/ssl.conf;
# . . . other code }
通过上面的配置,我们强制HTTPS并从www重定向到非www版本。
最后,重新加载Nginx服务以使更改生效:
sudo systemctl reload nginx
SSL证书自动续订
让我们加密的证书有效期为90天。 要在证书过期之前自动续订证书,certbot软件包将创建一个每天运行两次的cronjob,并且将在证书到期前30天自动续订任何证书。
由于我们在证书更新后使用certbot webroot插件,我们还必须重新加载nginx服务。 将--renew-hook "systemctl reload reload"添加到/etc/cron.d/certbot文件中,使其看起来像这样:
/etc/cron.d/certbot
0 */12 * * * root test -x /usr/bin/certbot -a ! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew --renew-hook "systemctl reload nginx"
要测试续订过程,您可以使用certbot --dry-run开关:
sudo certbot renew --dry-run
如果没有错误,则意味着更新过程成功。
就这样! 如果您遇到任何问题,请随时发表评论。