跳至内容

生成 SSL 密钥 - Let's Encrypt

先决条件和假设

  • 熟悉命令行操作
  • 熟悉使用SSL证书保护网站是一个加分项
  • 了解命令行文本编辑器(本示例使用vi
  • 一个面向公众开放且在80端口(http)上运行的Web服务器
  • 熟悉SSH(安全Shell)并能够通过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(Privacy Enhanced Mail)文件包含证书和链文件。这现在是所有证书文件的常见格式。即使它在名称中带有“Mail”,它也只是一种证书文件。配置文件的示例和说明如下

信息

以前,此文档在配置中包含SSLCertificateChainFile指令。自Apache 2.4.8版本以来,该指令已弃用,因为SSLCertificateFile指令现在已扩展为包含中间CA文件

<VirtualHost *:80>
        ServerName your-server-hostname
        ServerAdmin username@rockylinux.org
        Redirect / https://your-server-hostname/
</VirtualHost>
<VirtualHost *: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证书是保护您的网站安全的又一种选择。安装后,系统将自动续订证书,并加密到您网站的流量。

Let's Encrypt证书适用于标准的DV(域验证)证书。不能将其用于OV(组织验证)或EV(扩展验证)证书。

作者:Steven Spencer

贡献者:wsoyinka, Antoine Le Morvan, Ezequiel Bruni, Andrew Thiesen, Ganna Zhyrnova