在Ubuntu 20.04上使用Let's Encrypt保护Apache
Let’s Encrypt是由Internet安全研究小组(ISRG)创建的证书颁发机构。
它通过完全自动化的过程提供免费的SSL证书,该过程旨在消除手动创建,验证,安装和更新证书的过程。
Let's Encrypt颁发的证书自颁发之日起有效期为90天,并且今天已受到所有主要浏览器的信任。
本教程说明了如何在将Apache作为网络服务器运行的Ubuntu 20.04上安装免费的Let's Encrypt SSL证书。
我们还将展示如何配置Apache以使用SSL证书并启用HTTP/2.
准备工作
在继续之前,请确保满足以下准备工作:
- 以root或者具有sudo特权的用户身份登录。
- 我们要为其获取SSL证书的域必须指向公共服务器IP。我们将使用“ example.com”。
- 已安装Apache。
安装Certbot
我们将使用certbot获得证书。
这是一个命令行工具,可自动执行获取和续订“加密SSL”证书的任务。
certbot软件包包含在默认的Ubuntu存储库中。
使用以下命令更新软件包列表并安装certbot:
sudo apt updatesudo apt install certbot
产生强Dh(Diffie-Hellman)组
Diffie-Hellman密钥交换(DH)是一种在不安全的通信通道上安全地交换加密密钥的方法。
生成一组新的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请求,以验证请求的域是否解析为运行certbot的服务器。
为简化起见,我们将所有针对“ .well-known/acme-challenge”的HTTP请求映射到一个目录“/var/lib/letsencrypt”。
运行以下命令以创建目录并使该目录可用于Apache服务器。
sudo mkdir -p /var/lib/letsencrypt/.well-knownsudo chgrp www-data /var/lib/letsencryptsudo 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
SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1 SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384 SSLHonorCipherOrder off SSLSessionTickets off SSLUseStapling On SSLStaplingCache "shmcb:logs/ssl_stapling(32768)" SSLOpenSSLConfCmd DHParameters "/etc/ssl/certs/dhparam.pem" Header always set Strict-Transport-Security "max-age=63072000"
上面的代码片段使用了Mozilla推荐的削片程序,启用了OCSP装订,HTTP严格传输安全性(HSTS)并强制实施了一些以安全性为重点的HTTP标头。
在启用配置文件之前,请通过发出以下命令确保同时启用了“ mod_ssl”和“ mod_headers”:
sudo a2enmod sslsudo a2enmod headers
接下来,通过运行以下命令来启用SSL配置文件:
sudo a2enconf letsencryptsudo a2enconf ssl-params
启用HTTP/2模块,这将使更快,更健壮:
sudo a2enmod http2
重新加载Apache配置以使更改生效:
sudo systemctl reload apache2
现在,我们可以使用webroot插件运行Certbot工具并获取SSL证书文件:
sudo certbot certonly --agree-tos --email [email protected] --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 key file has been saved at: /etc/letsencrypt/live/example.com/privkey.pem Your cert will expire on 2017-10-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" - 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 Redirect permanent/https://example.com/ </VirtualHost> <VirtualHost *:443> ServerName 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 Labs服务器测试来测试域,则将获得A +等级,如下所示:
自动更新,Let’s Encrypt SSL证书
让我们的加密证书有效期为90天。
要在证书过期之前自动续订证书,certbot程序包会创建一个cronjob,该程序每天运行两次,并在证书过期前30天自动续订任何证书。
证书更新后,我们还必须重新加载Apache服务。
将'--renew-hook“ systemctl reload apache2”'添加到'/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'开关:
sudo certbot renew --dry-run
如果没有错误,则表示续订过程已成功。