生成 SSL 密钥 - Let's Encrypt
先决条件和假设¶
- 熟悉命令行
- 熟悉使用 SSL 证书保护网站是一个加分项
- 了解命令行文本编辑器(本示例使用 *vi*)
- 面向世界的 Web 服务器,在端口 80 (
http
) 上运行 - 熟悉 *SSH*(安全外壳)并能够使用 *SSH* 访问您的服务器
- 所有命令都假设您是 root 用户或使用
sudo
获得了 root 权限
简介¶
保护网站最流行的方式之一是使用 Let's Encrypt SSL 证书,它们也是免费的。
这些是真正的证书,而不是自签名或伪造证书,因此对于低预算的安全解决方案来说非常棒。本文档将引导您完成在 Rocky Linux Web 服务器上安装和使用 Let's Encrypt 证书的过程。
安装¶
要执行以下步骤,请使用 ssh
登录您的服务器。如果您的服务器的完全限定域名是
ssh -l root www.myhost.com
或者,如果您必须首先以非特权用户身份访问您的服务器。使用您的用户名
ssh -l username www.myhost.com
然后
sudo -s
在这种情况下,您将需要您的用户凭据才能以 root 身份访问系统。
Let's Encrypt 使用一个名为 *certbot* 的包,您可以使用 EPEL 仓库进行安装。首先添加它们
dnf install epel-release
安装相应的包,具体取决于您使用的是 Apache 还是 Nginx 作为您的 Web 服务器。对于 Apache,它是
dnf install certbot python3-certbot-apache
对于 Nginx,只需更换一个包
dnf install certbot python3-certbot-nginx
如果需要,您可以始终安装两个服务器模块。
注意
本指南的早期版本要求使用 *certbot* 的 snap 包版本,这在当时是必需的。RPM 版本最近重新测试过,现在可以正常工作。也就是说,Certbot 强烈建议使用 snap 安装程序。Rocky Linux 8 和 9 在 EPEL 中提供了 *certbot*,因此我们在此展示了该程序。如果您想使用 Certbot 建议的程序,只需按照该程序操作即可。
获取 Apache 服务器的 Let's Encrypt 证书¶
您可以通过两种方式检索 Let's Encrypt 证书:使用命令为您更改 http
配置文件,或者仅检索证书。如果您正在使用 Apache Web 服务器多站点设置 中为一个或多个站点建议的多站点设置程序,那么只需检索您的证书即可。
多站点设置是此处的假设,因此以下说明将仅检索证书。如果您正在运行带有默认配置的独立 Web 服务器,则可以一步检索证书并更改配置文件
certbot --apache
这确实是完成任务的最简单方法。但是,有时您可能希望采取更手动的方法来获取证书。要仅检索证书,请使用以下命令
certbot certonly --apache
这些命令会生成一组需要您回答的提示。第一个是提供一个电子邮件地址来获取重要信息
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): yourusername@youremaildomain.com
下一个要求您阅读并接受订户协议条款。阅读完协议后,回答“Y”继续
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.openssl.ac.cn/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o:
下一个是请求与电子前沿基金会共享您的电子邮件。根据您的喜好回答“Y”或“N”
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o:
下一个提示要求您标识要为其获取证书的域名。它可能会根据您运行的 Web 服务器在列表中显示一个域名。如果是,请输入您要获取证书的域名旁边的数字。在本例中,只存在一个选项(“1”)
Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: your-server-hostname
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel):
如果一切顺利,您将收到以下消息
Requesting a certificate for your-server-hostname
Performing the following challenges:
http-01 challenge for your-server-hostname
Waiting for verification...
Cleaning up challenges
Subscribe to the EFF mailing list (email: yourusername@youremaildomain.com).
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/your-server-hostname/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/your-server-hostname/privkey.pem
Your certificate will expire on 2021-07-01. 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.openssl.ac.cn/donate
Donating to EFF: https://eff.org/donate-le
站点配置 - https
¶
将配置文件应用于我们的站点与从其他提供商处购买的 SSL 证书所使用的程序几乎相同。
单个 PEM(隐私增强邮件)文件包含证书和证书链文件。这是目前所有证书文件通用的格式。尽管它在引用中包含“邮件”,但它只是一个证书文件类型。以下是配置文件的示例和对其操作的描述
信息
以前,此文档在配置中包含一行 SSLCertificateChainFile
指令。从 Apache 2.4.8 版本开始,该指令已弃用,因为 SSLCertificateFile
指令 现在扩展为包含中间 CA 文件。
<VirtualHost *:80>
ServerName your-server-hostname
ServerAdmin username@rockylinux.org
Redirect / https://your-server-hostname/
</VirtualHost>
<Virtual Host *:443>
ServerName your-server-hostname
ServerAdmin username@rockylinux.org
DocumentRoot /var/www/sub-domains/com.yourdomain.www/html
DirectoryIndex index.php index.htm index.html
Alias /icons/ /var/www/icons/
# ScriptAlias /cgi-bin/ /var/www/sub-domains/com.yourdomain.www/cgi-bin/
CustomLog "/var/log/httpd/com.yourdomain.www-access_log" combined
ErrorLog "/var/log/httpd/com.yourdomain.www-error_log"
SSLEngine on
SSLProtocol all -SSLv2 -SSLv3 -TLSv1
SSLHonorCipherOrder on
SSLCipherSuite EECDH+ECDSA+AESGCM:EECDH+aRSA+AESGCM:EECDH+ECDSA+SHA384:EECDH+ECDSA+SHA256:EECDH+aRSA+SHA384
:EECDH+aRSA+SHA256:EECDH+aRSA+RC4:EECDH:EDH+aRSA:RC4:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS
SSLCertificateFile /etc/letsencrypt/live/your-server-hostname/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/your-server-hostname/privkey.pem
<Directory /var/www/sub-domains/com.yourdomain.www/html>
Options -ExecCGI -Indexes
AllowOverride None
Order deny,allow
Deny from all
Allow from all
Satisfy all
</Directory>
</VirtualHost>
以下是发生的情况
- 即使端口 80(标准
http
)正在监听,您也会将所有流量重定向到端口 443(https
) - SSLEngine on - 表示使用 SSL
- SSLProtocol all -SSLv2 -SSLv3 -TLSv1 - 表示使用所有可用的协议,除了那些已发现存在漏洞的协议。您需要定期研究当前可接受使用的协议。
- SSLHonorCipherOrder on - 这与下一行关于密码套件的内容相关,表示按列出的顺序处理它们。这也是您需要定期审查您想要包含的密码套件的另一个领域
- SSLCertificateFile - 这是 PEM 文件,它包含站点证书 **和** 中间证书。
- SSLCertificateKeyFile - 私钥的 PEM 文件,使用 certbot 请求生成。
完成所有更改后,重新启动 httpd,如果它启动,请测试您的网站以确保您现在有一个有效的证书文件显示。如果是这样,您就可以继续执行下一步:自动化。
使用 certbot 与 Nginx¶
快速说明:使用 certbot 与 Nginx 与使用 Apache 几乎相同。以下是指南的简短版本
运行此命令开始
certbot --nginx
您需要输入您的电子邮件地址和您想要证书的网站。假设您至少配置了一个网站(域名指向服务器),您将看到一个列表
1. yourwebsite.com
2. subdomain.yourwebsite.com
如果您有多个网站,请按与您想要证书的网站相对应的数字。
其余文本与上面类似。结果会有所不同。如果您有一个像这样看起来的 Nginx 配置文件
server {
server_name yourwebsite.com;
listen 80;
listen [::]:80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
在 certbot 完成后,它将看起来像这样
server {
server*name yourwebsite.com;
listen 443 ssl; # managed by Certbot
listen [::]:443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/yourwebsite.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/yourwebsite.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
server {
if ($host = yourwebsite.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name yourwebsite.com;
return 404; # managed by Certbot
}
如果您使用 Nginx 作为反向代理,您可能需要更改新的配置文件来修复 certbot 本身无法完美处理的某些内容。
自动化 Let's Encrypt 证书续订¶
注意
在这些示例中,用实际的域名或主机名替换 "your-server-hostname"。
安装 certbot 的好处是 Let's Encrypt 证书将自动续订。您无需创建流程来执行此操作。您 *确实* 需要使用以下方法测试续订
certbot renew --dry-run
运行此命令时,您将获得一个显示续订过程的不错输出
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/your-server-hostname.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator apache, Installer apache
Account registered.
Simulating renewal of an existing certificate for your-server-hostname
Performing the following challenges:
http-01 challenge for your-server-hostname
Waiting for verification...
Cleaning up challenges
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed with reload of apache server; fullchain is
/etc/letsencrypt/live/your-server-hostname/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded:
/etc/letsencrypt/live/your-server-hostname/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
您可以通过以下几种方式续订 certbot 证书
- 列出
/etc/crontab/
的内容 - 列出
/etc/cron.*/*
的内容 - 运行
systemctl list-timers
使用 systemctl list-timers
方法,您可以看到 certbot 存在,并且它的安装使用 snap
过程
sudo systemctl list-timers
Sat 2021-04-03 07:12:00 UTC 14h left n/a n/a snap.certbot.renew.timer snap.certbot.renew.service
结论¶
Let's Encrypt SSL 证书是使用 SSL 保护您的网站的另一个选择。安装后,系统会提供证书的自动续订,并将加密到您网站的流量。
Let's Encrypt 证书适用于标准 DV(域名验证)证书。使用它们进行 OV(组织验证)或 EV(扩展验证)证书是不可能的。
作者:史蒂文·斯宾塞
贡献者:wsoyinka、Antoine Le Morvan、Ezequiel Bruni、Andrew Thiesen、Ganna Zhyrnova