Let’s Encrypt 是一个免费的,自动的,开放证书供应商。它由提供免费 SSL 证书的 Internet Security Research Group(ISRG)开发。
由 Let’s Encrypt 签发的证书,被大部分浏览器信任,并且从签发当日起 90 天内有效。
这篇指南讲解在运行着 Apache 网站服务器的 CentOS 8 上如何安装一个免费的 Let’s Encrypt SSl 证书。我们将会使用 certbot 来获取并且刷新证书。
请先确保以下前提条件,再继续后面的步骤:
example.com
。安装下面的软件包,这些是配置一个 SSL 加密网页服务器所必须的:
sudo dnf install mod_ssl openssl
当 mod_ssl package 安装后,它应该已经给本地主机创建了一个自签名的 key 和证书。如果这些文件没有自动被创建,你可以使用openssl
命令创建它们。
sudo openssl req -newkey rsa:4096 -x509 -sha256 -days 3650 -nodes \
-out /etc/pki/tls/certs/localhost.crt \
-keyout /etc/pki/tls/private/localhost.key
Certbot 是一个终端命令工具,它简化了获取和刷新 Let’s Encrypt SSL 证书的流程,并且可以为你的服务器自动启用 HTTPS。
certbot 软件包没有包含在标准的 CentOS 8 软件源仓库,但是它可以从供应商的网站下载。
以 root 用户或者其他有 sudo 权限用户身份运行下面的wget
命令,将一个 certbot 脚本下载到/usr/local/bin
目录:
sudo wget -P /usr/local/bin https://dl.eff.org/certbot-auto
一旦下载完成,赋予该文件可执行权限:
sudo chmod +x /usr/local/bin/certbot-auto
Diffie–Hellman key(DH)是一个在不安全通讯频道的安全交换密码的方案。产生一个 2048 位的 DH 因子来加强安全性:
sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048
你可以将大小提高到 4096 位,但是依赖系统熵,产生过程可能会超过 30 分钟。
想要为域名获得一个 SSL 证书,我们必须先使用 WeBroot 插件创建一个临时文件,用来验证${webroot-path}/.well-known/acme-challenge
目录下的域名。 Let’s Encrypt 服务器请求这个临时文件,来验证域名。
想要使得步骤变得简单,我们将所有.well-known/acme-challenge
的 HTTP 请求放进一个简单文件夹,/var/lib/letsencrypt
。
运行下面的命令创建文件夹,并且使得它对于 Apache 服务器来说可写。
sudo mkdir -p /var/lib/letsencrypt/.well-known
sudo chgrp apache /var/lib/letsencrypt
sudo chmod g+s /var/lib/letsencrypt
想要避免重复代码,并且使得配置可维护,创建以下两个配置代码片段:
/etc/httpd/conf.d/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/httpd/conf.d/ssl-params.conf
SSLCipherSuite EECDH+AESGCM:EDH+AESGCM
# Requires Apache 2.4.36 & OpenSSL 1.1.1
SSLProtocol -all +TLSv1.3 +TLSv1.2
SSLOpenSSLConfCmd Curves X25519:secp521r1:secp384r1:prime256v1
# Older versions
# 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 DENY
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"
上面的代码段使用了Cipherli.st推荐的加密配置。它启用了 OCSP,HTTP Strict Transport Security (HSTS), Dh key, 并且强制加上几个聚焦安全的 HTTP headers。
重新加载 Apache 配置,使得修改生效:
sudo systemctl reload httpd
现在你可以运行 certbot 脚本,配合 webroot 插件,用来获取 SSL 证书文件:
sudo /usr/local/bin/certbot-auto certonly --agree-tos --email admin@example.com --webroot -w /var/lib/letsencrypt/ -d example.com -d www.example.com
一旦成功,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 2020-01-26. To obtain a new or tweaked
version of this certificate in the future, simply run certbot-auto
again. To non-interactively renew *all* of your certificates, run
"certbot-auto 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/httpd/conf.d/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 /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/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 强制转向 non-www 版本。它同时启用了 HTTP/2,可以让你的网站变得更快,更强壮。你可以按照你的需求随意修改配置文件。
重启 Apache 服务:
sudo systemctl restart httpd
你现在可以使用 https://
打开你的网站,你将看到一个绿色的锁图标。
如果你使用SSL Labs Server Test测试你的域名,你将会获得一个 A+等级,就像下面一样:
Let’s Encrypt 的证书只有 90 天有效期。想要在过期之前自动刷新证书,我们需要创建一个 cronjob,它将会一天运行两次,并且在证书过期前 30 天左右刷新证书。
运行下面的命令创建一个新的 cronjob,它将会刷新证书,并且重启 Apache:
echo "0 0,12 * * * root python3 -c 'import random; import time; time.sleep(random.random() * 3600)' && /usr/local/bin/certbot-auto -q renew --renew-hook \"systemctl reload httpd\"" | sudo tee -a /etc/crontab > /dev/null
想要测试刷新过程,使用 certbot 命令加上--dry-run
选项:
sudo /usr/local/bin/certbot-auto renew --dry-run
如果没有错误提示,那就意味着刷新过程是成功的。
在这个指南中,我们讨论了如何在 CentOS 上使用 Let’s Encrypt 客户端 certbot 去获得域名的 SSL 证书。你也了解了如何使用配置 Apache,使用证书,并且建立一个 cronjob 定期任务去刷新证书。
想要了解更多关于 Certbot 脚本,浏览:Certbot 官方文档。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有