介绍
Let’s Encrypt是一个自动化、开放的证书颁发机构,为公众提供免费的TLS/SSL证书。该服务由互联网安全研究小组(ISRG)提供。本教程展示了如何使用Certbot安装向导在带有Apache或Nginx的Ubuntu 20.04服务器上安装Let’s Encrypt SSL证书。完成本教程后,服务器将拥有有效的证书,并将所有HTTP请求重定向到HTTPS。
先决条件
本教程假设您已使用Apache或Nginx部署了Vultr Ubuntu服务器,有一个指向服务器IP地址的域名,并且您以root登录。
1.安装Certbot
Certbot的推荐安装方法是使用Snap。
- 验证快照是否为最新版本。
$ sudo snap install core; sudo snap refresh core
- 删除
certbot-auto
和任何Certbot OS软件包。$ sudo apt-get remove certbot
- 使用Snap安装Certbot。
$ sudo snap install --classic certbot
- 将Certbot链接到
/usr/bin
。$ sudo ln -s /snap/bin/certbot /usr/bin/certbot
2.安装证书
Run certbot
to install the certificate. Full examples are below, here are descriptions of the command line options:
- –apache:使用Apache Web服务器
- –nginx:使用nginx Web服务器
- –redirect:将所有HTTP请求重定向到HTTPS。
- -d example.com -d www.example.com:安装多域(SAN)证书。您可以使用多达100d的域条目。
- -m admin@example.com:此证书的通知电子邮件地址。
- –同意:同意服务条款。
使用certbot --help
了解更多信息。有关SAN证书的更多信息,请参阅Certbot常见问题解答。
示例:Apache
为Apache运行Certbot。
# certbot --apache --redirect -d example.com -d www.example.com -m admin@example.com --agree-tos
示例:Nginx
- 在运行Certbot之前,请确保正确设置了server_name。编辑您的Nginx配置:
# nano /etc/nginx/conf.d/default.conf
- 更新server_name以包含您的域名。
server { server_name example.com www.example.com;
- 保存并退出文件。
- 为Nginx运行Certbot。
# certbot --nginx --redirect -d example.com -d www.example.com -m admin@example.com --agree-tos
3.验证自动续订
Let’s Encrypt证书的有效期为90天。Certbot向导更新系统计时器和crontab,以自动续订您的证书。
- 验证计时器是否处于活动状态。
# systemctl list-timers | grep 'certbot\|ACTIVATES'
- 验证crontab条目是否存在。
# ls -l /etc/cron.d/certbot
- 验证续订过程是否可进行模拟运行。
# certbot renew --dry-run
摘要
使用Certbot安装免费的Let’s Encrypt证书很简单。有关更多信息,请参阅官方Certbot安装文档。