Alibaba Cloud Linux 使用 Let’s Encrypt 设置 SSL 证书
Alibaba Cloud Linux 基于 Red Hat Enterprise Linux(RHEL),因此主要使用 YUM 包管理器。请根据实际系统和操作情况适当调整以下步骤。
1. 更新软件包索引
sudo yum update
2. 安装 Certbot
首先,你可能需要先安装 EPEL 仓库,但如果发现冲突或者已经有了,可以跳过这一步。
sudo yum install epel-release
接下来,安装 Certbot:
sudo yum install certbot
3. 获取 SSL 证书
执行以下命令,但记得替换其中的路径和域名为你的实际情况:
sudo certbot certonly --webroot -w /path/to/your/website -d your-domain.com -d www.your-domain.com
一旦域名验证成功,您的证书和私钥通常会保存在 /etc/letsencrypt/live/your-domain.com/ 目录中。
4. 设置 Nginx
- 在
nginx.conf
(可能存在/etc/nginx/nginx.conf)中找到 server 块并进行修改:
server {
listen 80;
listen [::]:80;
server_name xxxxxxx.top www.xxxxxxx.top;
root /usr/share/nginx/html;
...
}
- 重新加载 Nginx:
sudo nginx -s reload
- 使用 Certbot 的 nginx 插件获取和安装证书:
sudo certbot --nginx -d xxxxxxx.top -d www.xxxxxxx.top #-d表示 domain 域名的意思
5. 设置证书自动续期
你可以选择使用 crontab -e
命令编辑当前用户的 cron 表,或者直接编辑 /etc/crontab
文件(需要 root 权限)。
在编辑器里,添加以下行以设置自动续期:
echo "0 2 * * * root certbot renew --quiet" | sudo tee -a /etc/crontab # -a代表追加的意思,tee代表标准输出
这样,每天凌晨 2 点,系统会自动执行 Certbot 的 renew 命令来续期你的 SSL 证书。
注:安全组的443和80端口记得开。