HTTPS(超文本传输安全协议)是一种互联网通信协议,可保护用户计算机与网站之间传输的数据的完整性和机密性。用户在访问网站时都希望获得安全私密的在线体验。
Lets Encrypt 是由 Internet Security Research Group (ISRG) 开发的免费开放的证书颁发机构。Lets Encrypt 颁发的证书如今几乎得到所有浏览器的信任。
rumenz.com
.Certbot 是一个易于使用的工具,可以自动执行获取和更新 Lets Encrypt SSL 证书以及配置 Web 服务器的任务。
要从 EPEL 存储库安装 certbot 包
yum install certbot
Diffie–Hellman (DH)密钥交换 是一种通过不安全的通信通道安全地交换加密密钥的方法。通过键入以下命令生成一组新的 2048 位 DH 参数:
> openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048
你可以将大小更改为 4096 位,但在这种情况下,生成可能需要 30 分钟以上,具体取决于系统熵。
要为我们的域获取 SSL 证书,我们将使用 Webroot 插件,该插件通过创建一个
${webroot-path}/.well-known/acme-challenge
临时文件来验证目录中请求的域。Lets Encrypt 服务器向临时文件发出 HTTP 请求,以验证请求的域是否解析为运行 certbot 的服务器。为了简单起见,我们将把.well-known/acme-challenge
的所有HTTP请求映射到/var/lib/letsencrypt
目录。创建目录并使其可用于 Nginx 服务器。
> mkdir -p /var/lib/letsencrypt/.well-known
> chgrp nginx /var/lib/letsencrypt
> chmod g+s /var/lib/letsencrypt
为了避免重复代码,创建以下两个片段,我们将把它们包含在我们所有的 Nginx 服务器块文件中:
> mkdir /etc/nginx/snippets
> cat /etc/nginx/snippets/letsencrypt.conf
location ^~ /.well-known/acme-challenge/ {
allow all;
root /var/lib/letsencrypt/;
default_type "text/plain";
try_files $uri =404;
}
> cat /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 TLSv2;
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 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推荐的配置 ,启用 OCSP 装订、HTTP 严格传输安全 (HSTS) 并强制执行少数以安全为中心的 HTTP 标头。
创建
letsencrypt.conf
代码段后,打开域服务器块并包含如下所示的代码段:
> cat /etc/nginx/conf.d/rumenz.com.conf
server {
listen 80;
server_name rumenz.com www.rumenz.com;
include snippets/letsencrypt.conf;
}
重新加载 Nginx 配置
> systemctl reload nginx
你现在可以使用 webroot 插件运行 Certbot 并通过发出以下命令为你的域获取 SSL 证书文件:
> certbot certonly --agree-tos --email admin@rumenz.com --webroot -w /var/lib/letsencrypt/ -d rumenz.com -d www.rumenz.com
如果成功获取 SSL 证书,certbot 将打印以下消息:
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/rumenz.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/rumenz.com/privkey.pem
Your cert will expire on 2018-06- 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
现在你有了证书文件,你可以 按如下方式编辑域服务器块:
> /etc/nginx/conf.d/rumenz.com.conf
server {
listen 80;
server_name www.rumenz.com rumenz.com;
include snippets/letsencrypt.conf;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name www.rumenz.com;
ssl_certificate /etc/letsencrypt/live/rumenz.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/rumenz.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/rumenz.com/chain.pem;
include snippets/ssl.conf;
include snippets/letsencrypt.conf;
return 301 https://rumenz.com$request_uri;
}
server {
listen 443 ssl http2;
server_name rumenz.com;
ssl_certificate /etc/letsencrypt/live/rumenz.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/rumenz.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/rumenz.com/chain.pem;
include snippets/ssl.conf;
include snippets/letsencrypt.conf;
# . . . other code
}
通过上面的配置,我们强制使用 HTTPS 并将 www 重定向到非www 。
重启 Nginx 服务
> systemctl reload nginx
https://rumenz.com
开启了
https
的网站,浏览器默认前面会加一把小锁.
Lets Encrypt 的证书有效期为 90 天。为了在证书到期前自动更新证书,我们将创建 一个每天运行两次的cronjob,并在证书到期前 30 天自动更新任何证书。
编辑crontab定时任务
> 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
开关:
> certbot renew --dry-run
相关文章