Let‘s Encrypt 是一个认证机构(简称CA),使用它可以很方便的获取和安装免费的TLS/SSL证书,用来为网站服务器加密HTTPS。它提供一个客户断软件certbot来自动化要求的步骤。当前在Apache和Nginx上获取和安装SSL证书的过程已经自动化了。
在这个教程中,将使用certbot在Ubuntu上为Apache来获取一个免费的SSL证书。并且确保这个认证被设置为自动更新。
这个教程使用了一个单独的虚拟主机来设置这个需要使用Let’s Enctypt的SSL的网站。推荐为每一个网站单独创建一个虚拟主机文件以避免一些常见问题,同时保留了默认的配置作为备用。
云服务器推荐使用性价比高的搬瓦工VPS
优惠码:BWHCGLUKKB(购买时输入有折扣)
提前准备
为了完成这个教程你需要
- 在Ubuntu服务器上设置一个非root的用户,这个用户被赋予sudo管理权限。如果没有服务器可以通过上方的链接购买一个云服务器。
- 一个注册的域名,这个教程将使用your_domain作为例子,你可以在域名提供商处购买域名,例如namesilo 使用此链接,也可使用优惠码TYVKWQ。你也可以在freenom获取免费的域名。
- 为你的服务器设置下面的DNS记录
- 一个A记录your_domain 指向你服务器的IP地址
- 一个A记录www.your_domain指向你服务器的IP地址
- 已完整了Apache,如果未安装,可以看这个教程中安装Apache部分。Ubuntu上安装Apache,Mysql, PHP
安装Certbot
为了使用Let’s Encrypt来获取SSL证书,需要在服务器上安装Certbot软件。
首先更新本地包索引
sudo apt update
你需要两个包,certbot和python3-certbot-apache,后面这个是Certbot继承Apache的一个插件。通过它可以用一条命令来自动获取并为网站服务器配置HTTPS
sudo apt install certbot python3-certbot-apache
你将会被提示,确认安装,按Y之后回车。
Certbot已经安装,下一步来验证Apache的配置来确认虚拟主机设置正确。这个将会确保certbot脚本能够检测到你的域名,并能自动使用最新生成的SSL证书来重新配置你的网站服务器
检查Apache虚拟主机配置
为了给网站服务器自动获取和配置SSL,Certbot需要在Apache配置文件中找到正确的虚拟主机。你的服务器域名将会从虚拟主机VirtualHost配置块的ServerName和ServerAlias指令中获取。
如果你按照之前的安装Apache的教程,你应该为你的域名有一个虚拟主机块的配置,在这个文件/etc/apache2/sites-available/your_domain.conf中ServerName和ServerAlias已经配置好了。
为了确认设置好了,打开你的域名的虚拟主机配置文件,使用nano或者你喜欢的其他文本编辑器
sudo nano /etc/apache2/sites-available/your_domain.conf
找到ServerName和ServerAlias行,应该如下所示
...
ServerName your_domain
ServerAlias www.your_domain
...
如果ServerName和ServerAlias已配置好了,你可以退出文本编辑器并进行下一步。如果没有配置好,根据例子中配置。如果你使用nano,退出按CTRL和X,之后Y回车来确认改变。
下面运行下面命令来验证这些变更
sudo apache2ctl configtest
你应该收到Syntax OK作为回应。如果遇到错误,重新打开虚拟主机配置文件,检查是否拼写错误或遗漏字符。一旦配置文件语法正确,重启Apache使这些变更生效。
sudo systemctl reload apache2
基于这些变更,Certbot将能找到虚拟主机VirtualHost块并更新它
允许HTTPS穿过防火墙
如果你让UFW防火墙生效了。根据之前的教程中的推荐,你需要调整配置来允许HTTPS流量通过防火墙。在安装时,Apache注册了几个不同的UFW应用项。可以使用Apache FULL来将HTTP和HTTPS流量都允许。
为了验证当前哪种流量在服务器上被允许,检查一下状态
sudo ufw status
如果你按照之前安装Apache的教程,应该会看到类似如下的输出,意思是只有HTTP流量在80端口上被允许了。
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache (v6) ALLOW Anywhere (v6)
为了允许HTTPS流量,允许 “Apache Full”
sudo ufw allow 'Apache Full'
然后删除重复的Apache
sudo ufw delete allow 'Apache'
检查一下状态
sudo ufw status
将会显示如下
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache Full ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache Full (v6) ALLOW Anywhere (v6)
获取SSL证书
Certbot提供了通过插件可以获取SSL证书的多种方式。Apache插件将会处理重新配置Apache和重新加载配置文件。运行下面命令来使用这个插件
sudo certbot --apache
这个脚本会提示你回答一系列的问题来配置SSL证书。首先它会问你要一个有效的email地址,这个email用来接收更新通知和安全通知。
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): you@your_domain
提供了email地址后,按回车进入下一步。你将被询问确认是否同意 Let’s Encrypt的条款。你可以输入Y来确认,之后回车
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o:
下一步,你将会被问是否想要分享你的email地址给 Electronic Frontier Foundation来接受新闻和其他信息。如果你不想接受他们的信息,输入N,否则输入Y,按回车进入下一步
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing 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:
下一步将会询问你为哪些域名激活HTTPS。所列的域名是从虚拟主机配置中自动获取到的,所以在虚拟主机配置文件中的ServerName和ServerAlias一定要配置正确。如果你想所列的域名都激活HTTPS,那么什么都不输入直接按回车。否则输入你所选的域名前面的编号,多个域名时,使用逗号,分开,之后按回车。则只为这些域名激活HTTPS。
Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: your_domain
2: www.your_domain
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel):
这一步后,Certbot配置就完成了。将会给你展示最新的SSL证书的信息和哪里去找到这些生成的文件
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/your_domain/fullchain.pem
Key is saved at: /etc/letsencrypt/live/your_domain/privkey.pem
This certificate expires on 2022-07-10.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
Deploying certificate
Successfully deployed certificate for your_domain to /etc/apache2/sites-available/your_domain-le-ssl.conf
Successfully deployed certificate for www.your_domain.com to /etc/apache2/sites-available/your_domain-le-ssl.conf
Congratulations! You have successfully enabled HTTPS on https:/your_domain and https://www.your_domain.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
你的SSL证书现在已经安装并加载到了Apache配置文件,尝试使用https://来重新加载网站。并且注意浏览器的安全指示器,它应该会显示地址栏中有个锁,表示网站有适当的安全
下一步也是最后一部,你将测试Certbot的自动更新特性,使用它,你的SSL证书将在失效前自动更新。
验证Certbot自动更新
Let’s Encrypt的证书仅仅有效90天,这样鼓励用户自动化证书的更新过程。同时确保滥用的证书或者失窃的键值会很快失效。
你安装的certbot包通过到/etc/cron.d的一个更新脚本来处理更新,这个更新脚本由systemctl服务来管理,叫做certbot.timer。这个脚本一天运行两次,并且会自动更新在30天之内即将失效的任何证书。
为了检测这个服务的状态并确保它时运行着的,执行下面
sudo systemctl status certbot.timer
输出会类似于下面的
● certbot.timer - Run certbot twice daily
Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; vendor preset:>
Active: active (waiting) since Wed 2024-09-04 06:06:06 UTC; 6min 6s ago
Trigger: Wed 2024-09-04 00:06:06 UTC; 6h 0min left
Triggers: ● certbot.service
Sep 04 06:06:06 jammy-encrypt systemd[1]: Started Run certbot twice daily.
为了测试更新的过程,你可以用certbot运行一个模拟运行
sudo certbot renew --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/your_domain.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Account registered.
Simulating renewal of an existing certificate for your_domain and www.your_domain.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded:
/etc/letsencrypt/live/your_domain/fullchain.pem (success)
如果没有收到任何错误,那么就完成了。当需要更新时,Certbot会更新SSL证书并且重新加载Apache变更的配置。如果自动更新失败,Let’s Encrypt会向你提供的email地址发送一个通知,当SSL证书要过期时用来提醒你。
发表回复