前提条件
My HTTP website is running Nginx on Debian 10(或者11)
时间:2024-3-28 16:25:52
你的网站部署在Debain 10(或者11)的 Nginx上
安装单域名证书(默认)(非泛域名)
1 进入自己的云服务器的宝塔面板(即云服务器控制台的外网连接)
2 进入终端标签页
安装snap
进入这个网址
https://snapcraft.io/docs/installing-snapd
选择自己的服务器环境
以 Debian 为例
在终端运行这些指令
snap can be installed directly from the command line:
sudo apt update
sudo apt install snapd
Either log out and back in again, or restart your system, to ensure snap’s paths are updated correctly.
After this, install the core
snap in order to get the latest snapd
.
sudo snap install core
To test your system, install the hello-world snap and make sure it runs correctly:
sudo snap install hello-world
hello-world
如果曾经安装过Certbot packages
则需要先卸载移除这些内容
例如
sudo apt-get remove certbot
sudo dnf remove certbot
sudo yum remove certbot
Install Certbot
sudo snap install --classic certbot
Prepare the Certbot command
Execute the following instruction on the command line on the machine to ensure that the certbot
command can be run.
sudo ln -s /snap/bin/certbot /usr/bin/certbot
Run this command to get a certificate and have Certbot edit your nginx configuration automatically to serve it, turning on HTTPS access in a single step.(小白推荐)
sudo certbot --nginx
注意,如果你的nginx不在默认目录/etc/nginx/nginx.conf
则需要修改这个命令,修改后如下
以/www/server/nginx/conf/nginx.conf为例
sudo certbot --nginx-server-root /www/server/nginx/conf
启用自动续订SSL证书
Test automatic renewal
The Certbot packages on your system come with a cron job or systemd timer that will renew your certificates automatically before they expire. You will not need to run Certbot again, unless you change your configuration. You can test automatic renewal for your certificates by running this command:
sudo certbot renew --dry-run
The command to renew certbot is installed in one of the following locations:
/etc/crontab/
/etc/cron.*/*
systemctl list-timers
3 Confirm that Certbot worked,重新进入自己的网站,看看是否变成了https协议
To confirm that your site is set up properly, visit https://yourwebsite.com/
in your browser and look for the lock icon in the URL bar.
完成
4 去宝塔网站标签页(从这里开始建议不必理会,可以先去看看其他大神)
5 选择自己的网站项目
这里以go项目为例
6 粘贴自己的证书内容(内容哪里找?看第7步)
7 去宝塔文件系统的这个目录/etc/letsencrypt/live/www.xxxxx.com
分别对应privkey.pem 和 fullchain.pem
附录
指定nginx目录,且指定域名
指定nginx目录,且指定域名,示例
sudo certbot --nginx --nginx-server-root /www/server/nginx/conf -d www.xxxxx.com
sudo certbot --nginx --nginx-server-root /www/server/nginx/conf -d www.xxxxx.com
强制续订所有证书
sudo certbot renew --force-renewal
续订完成之后回到步骤6 在宝塔面板更新一下证书即可
查看所有证书
sudo certbot certificates
这里可以查看所有的证书都放在什么位置,都还有多少时间过期。
比方说在这个路径:/etc/letsencrypt/live
删除不必要的证书
sudo rm /etc/letsencrypt/renewal/xxxxxxxx.conf
其实就是直接删除一个配置文件
关于阿里云上传SSL证书
上传SSL证书到阿里云时,要求同时上传证书文件、证书私钥和证书链,这是为了确保HTTPS连接的安全性和完整性。
文件对应关系
- 证书文件(Certificate):对应
cert.pem
- 证书私钥(Private Key):对应
privkey.pem
- 证书链(Certificate Chain):对应
chain.pem
或fullchain.pem
详细解释
-
证书文件(cert.pem):
- 内容:包含服务器的公钥和服务器的身份信息。直接使用
cert.pem
可能会破坏许多服务器配置,建议在阅读相关文档后再使用。 - 作用:用于加密数据和验证服务器的身份。
- 内容:包含服务器的公钥和服务器的身份信息。直接使用
-
证书私钥(privkey.pem):
- 内容:包含与证书文件对应的私钥。
- 作用:用于解密客户端发送的加密数据和签名数据,确保数据的完整性和来源的真实性。用于解密通过 SSL/TLS 加密的通信数据。私钥必须保密,不能泄露。
-
证书链(chain.pem 或 fullchain.pem):
- 内容:包含中间证书和根证书,用于建立完整的证书链。
- 作用:客户端在验证服务器证书时,需要通过证书链验证服务器证书的合法性。证书链确保客户端可以信任服务器证书。
chain.pem
用于 OCSP stapling(在线证书状态协议),特别是在 Nginx 1.3.7 及以上版本中使用。fullchain.pem
是大多数服务器软件使用的证书文件,确保客户端可以验证服务器证书的完整链。
为什么需要这些文件
- 证书文件:客户端需要服务器的公钥来加密数据,并验证服务器的身份。
- 证书私钥:服务器需要私钥来解密客户端发送的加密数据,并签名数据以确保数据的完整性和来源的真实性。
- 证书链:客户端需要通过证书链验证服务器证书的合法性,确保服务器证书是由受信任的证书颁发机构(CA)签发的。
服务器配置示例
以下是如何在不同的服务器软件中使用这些文件的示例:
Nginx 配置示例
在 Nginx 中使用 fullchain.pem
和 privkey.pem
:
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# 如果需要 OCSP stapling
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
ssl_stapling on;
ssl_stapling_verify on;
# 其他配置...
}
Apache 配置示例
在 Apache 中使用 fullchain.pem
和 privkey.pem
:
<VirtualHost *:443>
ServerName example.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
# 其他配置...
</VirtualHost>
阿里云 DNS API 自动续签 Let's Encrypt SSL 证书(Certbot)
使用 阿里云 RAM 访问控制 创建 API 访问凭据,并通过 certbot
自动获取和续签 Let's Encrypt SSL 证书,确保 HTTPS 站点长期安全运行。
第一部分:创建阿里云 API 访问凭据
为了让 Certbot 使用阿里云 DNS API,我们需要创建一个具有 AliyunDNSFullAccess 权限的用户。
1. 登录阿里云 RAM 访问控制
- 进入 阿里云 RAM 控制台
- 在左侧菜单找到 身份管理 → 用户
2. 创建 RAM 用户
- 点击「创建用户」
- 填写 用户名(例如
certbot
) - 选择 "OpenAPI 调用"(编程访问)
- 点击 「确定」
3. 绑定 AliyunDNSFullAccess 权限
- 选中刚刚创建的用户(
certbot
) - 进入 权限管理 → 点击 「添加权限」
- 搜索
AliyunDNSFullAccess
并勾选 - 点击 「确定」
4. 获取 API 访问密钥
- 在用户详情页面,进入 安全设置
- 创建 AccessKey
- 记下 AccessKey ID 和 AccessKey Secret
⚠️ 注意: 只会显示一次,请务必保存!
第二部分:服务器安装 Certbot 和插件
1. 安装 Certbot
Ubuntu/Debian
sudo apt update && sudo apt install certbot python3-certbot-dns-aliyun -y
CentOS
sudo yum install epel-release -y
sudo yum install certbot python3-certbot-dns-aliyun -y
使用 Snap
sudo snap install certbot --classic
sudo snap refresh certbot
2. 检查 Certbot 版本
certbot --version
确保版本大于 1.10.0
,否则更新 Certbot:
sudo apt update && sudo apt install certbot -y
第三部分:配置阿里云 API 访问凭据
1. 创建 API 认证文件
mkdir -p ~/.secrets/certbot
nano ~/.secrets/certbot/aliyun.ini
填写以下内容:
dns_aliyun_access_key = YOUR_ACCESS_KEY_ID
dns_aliyun_secret_key = YOUR_ACCESS_KEY_SECRET
dns_aliyun_access_key_id = YOUR_ACCESS_KEY_ID
dns_aliyun_access_key_secret = YOUR_ACCESS_KEY_SECRET
替换
YOUR_ACCESS_KEY_ID
和YOUR_ACCESS_KEY_SECRET
为你自己的值
2. 设置文件权限(重要!)
chmod 600 ~/.secrets/certbot/aliyun.ini
第四部分:获取 SSL 证书
使用 certbot
申请证书,示例中包含 XXX.XXX.com
和 www.XXX.com
:
sudo certbot certonly \
--authenticator dns-aliyun \
--dns-aliyun-credentials ~/.secrets/certbot/aliyun.ini \
--dns-aliyun-propagation-seconds 60 \
--force-renewal \
-d XXX.XXX.com -d www.XXX.com
执行成功后,你会看到:
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/www.XXX.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/XXX.XXX.com/privkey.pem
第六部分:自动续签 SSL 证书
Let's Encrypt 证书有效期 90 天,所以我们需要配置 自动续签任务。
1. 手动测试续签
sudo certbot renew --dry-run
如果没有错误,说明自动续签可用。
2. 配置定时任务(cron
)
打开 crontab
:
sudo crontab -e
添加:
0 3 * * * certbot renew --quiet --dns-aliyun --dns-aliyun-credentials ~/.secrets/certbot/aliyun.ini && systemctl reload nginx
每天凌晨 3 点 自动续签证书并重启 Nginx