Let's Encrypt是由互联网安全研究组(ISRG)开发的免费开放认证机构。 Let's Encrypt颁发的证书现在几乎所有浏览器都信任。
在本教程中,我们将逐步提供关于如何使用CentOS Linux 7.5上的certbot工具使用Let's Encrypt来保护Nginx的说明。
先决条件
在继续本教程之前,请确保您已满足以下先决条件:
- 你有一个指向你的公共服务器IP的域名。 在本教程中,我们将使用linuxidc.com。
- 您已经通过以下在CentOS 7上如何安装Nginx来启用EPEL存储库并安装了Nginx。 https://www.linuxidc.com/Linux/2018-05/152399.htm
安装Certbot
要从EPEL存储库安装certbot软件包,请运行:
sudo yum install certbot
生成强大的Dh(Diffie-Hellman)组
我们将生成一组新的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服务器。
sudo mkdir -p /var/lib/letsencrypt/.well-known sudo chgrp nginx /var/lib/letsencrypt sudo chmod g s /var/lib/letsencrypt
为避免重复代码,请创建以下两个片段,我们将在所有Nginx服务器块文件中使用这两个片段:
sudo mkdir /etc/nginx/snippets
/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/conf.d/linuxidc.com.conf
server { listen 80; server_name linuxidc.com www.linuxidc.com;
include snippets/letsencrypt.conf; }
重新加载Nginx配置以使更改生效:
sudo systemctl reload nginx
您现在可以运行带有webroot插件的Certbot,并通过发出以下命令获取SSL证书文件:
sudo certbot certonly --agree-tos --email admin@linuxidc.com --webroot -w /var/lib/letsencrypt/ -d linuxidc.com -d www.linuxidc.com
如果SSL证书成功获取,certbot将打印以下消息:
IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/linuxidc.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/linuxidc.com/privkey.pem Your cert will expire on 2018-07-15. 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 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
现在您已拥有证书文件,您可以按如下方式编辑域服务器块:
server { listen 80; server_name www.linuxidc.com linuxize.com;
include snippets/letsencrypt.conf; return 301 https://hostrequest_uri;}
server { listen 443 ssl http2; server_name www.linuxidc.com;
ssl_certificate /etc/letsencrypt/live/linuxidc.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/linuxidc.com/privkey.pem; ssl_trusted_certificate /etc/letsencrypt/live/linuxidc.com/chain.pem; include snippets/ssl.conf;
return 301 https://linuxidc.com$request_uri; }
server { listen 443 ssl http2; server_name linuxidc.com;
# . . . other code
ssl_certificate /etc/letsencrypt/live/linuxidc.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/linuxidc.com/privkey.pem; ssl_trusted_certificate /etc/letsencrypt/live/linuxidc.com/chain.pem; include snippets/ssl.conf;
# . . . other code }
通过上面的配置,我们强制HTTPS并从www重定向到非www版本。
最后,重新加载Nginx服务以使更改生效:
sudo systemctl reload nginx
SSL证书自动续订
让我们加密的证书有效期为90天。 要在证书过期前自动续订证书,我们将创建一个每天运行两次的cronjob,并在证书到期前30天自动更新证书。
运行crontab命令来创建一个新的cronjob:
sudo crontab -e
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
如果没有错误,则意味着更新过程成功。