Let’s Encrypt 是由互联网安全研究小组 (ISRG) 创建的证书颁发机构。它通过完全自动化的流程提供免费的 SSL 证书,旨在消除手动证书创建、验证、安装和续订。
Let’s Encrypt 颁发的证书自颁发之日起 90 天内有效,并且受到当今所有主要浏览器的信任。
本教程将指导您完成在 Debian 9 上使用 certbot 工具获取免费 Let’s Encrypt 的过程。我们还将展示如何配置 Apache 以使用新的 SSL 证书并启用 HTTP/2。
先决条件#
在继续本教程之前,请确保您已满足以下先决条件:
登录身份为具有 sudo 权限的用户
.
有一个域名指向您的服务器公共服务器IP。我们将使用example.com
.
阿帕奇已安装 。您的域的 apache 虚拟主机。您可以关注这些说明 有关如何创建一个的详细信息。
安装证书机器人#
Certbot 是一款功能齐全且易于使用的工具,可以自动执行获取和续订 Let’s Encrypt SSL 证书的任务。 certbot 软件包包含在默认的 Debian 存储库中。
使用以下命令更新软件包列表并安装 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 分钟以上,具体取决于系统熵。
获取 Let's Encrypt SSL 证书#
要为我们的域获取 SSL 证书,我们将使用 Webroot 插件,该插件通过创建一个临时文件来验证所请求的域${webroot-path}/.well-known/acme-challenge
目录。 Let’s Encrypt 服务器向临时文件发出 HTTP 请求,以验证请求的域是否解析为 certbot 运行的服务器。
为了使其更简单,我们将映射所有 HTTP 请求.well-known/acme-challenge
到单个目录,/var/lib/letsencrypt
.
以下命令创建目录并使其可供 Apache 服务器写入。
sudo mkdir -p /var/lib/letsencrypt/.well-known
sudo chgrp www-data /var/lib/letsencrypt
sudo chmod g+s /var/lib/letsencrypt
为了避免重复代码,创建以下两个配置片段:
/etc/apache2/conf-available/letsencrypt.conf
Alias /.well-known/acme-challenge/ "/var/lib/letsencrypt/.well-known/acme-challenge/"
<Directory "/var/lib/letsencrypt/" >
AllowOverride None
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
Require method GET POST OPTIONS
</Directory>
/etc/apache2/conf-available/ssl-params.conf
SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH
SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
SSLHonorCipherOrder On
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
Header always set X-Frame-Options SAMEORIGIN
Header always set X-Content-Type-Options nosniff
# Requires Apache >= 2.4
SSLCompression off
SSLUseStapling on
SSLStaplingCache "shmcb:logs/stapling-cache(150000)"
# Requires Apache >= 2.4.11
SSLSessionTickets Off
SSLOpenSSLConfCmd DHParameters "/etc/ssl/certs/dhparam.pem"
上面的代码片段包括推荐的切片器,启用 OCSP 装订、HTTP 严格传输安全 (HSTS) 并强制执行一些以安全为中心的 HTTP 标头。
在启用配置文件之前,请确保mod_ssl
and mod_headers
通过发出以下命令启用:
sudo a2enmod ssl
sudo a2enmod headers
启用 HTTP/2 模块,这将使您的网站更快、更健壮:
sudo a2enmod http2
通过运行以下命令启用 SSL 配置文件:
sudo a2enconf letsencrypt
sudo a2enconf ssl-params
重新加载 Apache 配置以使更改生效:
sudo systemctl reload apache2
使用带有 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 cert will
expire on 2019-01-17. 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 admin@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
现在您已经拥有了证书文件,请编辑您的域虚拟主机配置,如下所示:
/etc/apache2/sites-available/example.com.conf
<VirtualHost *:80 >
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443 >
ServerName example.com
ServerAlias www.example.com
Protocols h2 http/1.1
<If "%{HTTP_HOST} == 'www.example.com'" >
Redirect permanent / https://example.com/
</If>
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
SSLEngine On
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
# Other Apache Configuration
</VirtualHost>
通过上面的配置,我们强制使用 HTTPS 并从 www 版本重定向到非 www 版本。可以根据您的需要自由调整配置。
重新加载 Apache 服务以使更改生效:
sudo systemctl reload apache2
使用打开您的网站https://
,您会注意到一个绿色的锁图标。
如果您使用以下命令测试您的域SSL 实验室服务器测试 ,您将获得 A+ 等级,如下所示:
自动续订 Let's Encrypt SSL 证书#
Let’s Encrypt 的证书有效期为 90 天。为了在证书过期之前自动续订证书,certbot 包创建了一个每天运行两次的 cronjob,并将在证书到期前 30 天自动续订任何证书。
更新证书后,我们还必须重新加载 Apache 服务。附加--renew-hook "systemctl reload apache2"
to the /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 apache2"
要测试续订过程,请使用 certbot--dry-run
switch:
sudo certbot renew --dry-run
如果没有错误,则表示更新过程成功。
结论#
在本教程中,您使用 Let’s Encrypt 客户端 certbot 来获取您的域的 SSL 证书。您还创建了 Apache 代码片段以避免重复代码,并将 Apache 配置为使用证书。在本教程结束时,您已经设置了一个用于自动证书续订的 cronjob。
如果您想了解有关 Certbot 脚本的更多信息,他们的文档 是一个很好的起点。
如果您有任何问题或反馈,请随时发表评论。