Linux中安装SSL证书,并且自动续订

1. 安装 Certbot

不同 Linux 发行版安装 Certbot 的方法略有不同:

Debian/Ubuntu

sudo apt update
sudo apt install certbot python3-certbot-nginx -y  # 如果使用Nginx
sudo apt install certbot python3-certbot-apache -y # 如果使用Apache

CentOS/RHEL (使用 EPEL)

sudo dnf install epel-release -y
sudo dnf install certbot python3-certbot-nginx -y  # 如果使用Nginx
sudo dnf install certbot python3-certbot-apache -y # 如果使用Apache

Rocky Linux/AlmaLinux

sudo dnf install epel-release -y
sudo dnf install certbot python3-certbot-nginx -y

2. 生成 SSL 证书

Certbot 主要支持以下方式申请 SSL 证书:

方法 1:使用 Web 服务器自动配置(推荐)

Nginx 自动申请

sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

Apache 自动申请

sudo certbot --apache -d yourdomain.com -d www.yourdomain.com

说明: Certbot 会自动检测服务器并修改 Nginx/Apache 配置文件,同时申请证书。


方法 2:手动 DNS 验证(适用于无 Web 服务器或通配符证书)

使用 DNS TXT 记录进行域名验证:

sudo certbot certonly --manual --preferred-challenges dns --email [email protected] -d "*.yourdomain.com" -d yourdomain.com

Certbot 会提示你添加一条 _acme-challenge.yourdomain.com 的 TXT 记录,添加后等待 DNS 解析生效再继续。


3. 配置自动续订

Let’s Encrypt 证书有效期为 90 天,推荐每 60 天自动更新。

检查 Certbot 是否安装定时任务

Certbot 默认会安装 systemd 计划任务,检查:

sudo systemctl list-timers | grep certbot

如果没有自动任务,可以手动添加 cronjob

sudo crontab -e

添加:

0 3 * * * certbot renew --quiet

表示每天凌晨 3 点检查并更新 SSL 证书。

或者使用 systemd 定时任务:

sudo systemctl enable certbot.timer
sudo systemctl start certbot.timer

手动测试续订:

sudo certbot renew --dry-run

4. Certbot 常用命令

命令 说明
certbot --nginx -d yourdomain.com 自动为 Nginx 申请证书
certbot --apache -d yourdomain.com 自动为 Apache 申请证书
certbot certonly --standalone -d yourdomain.com 申请证书但不修改 Web 配置(需要停掉 Web 服务器)
certbot certonly --manual -d yourdomain.com 手动申请 SSL 证书
certbot renew 续订所有证书
certbot certificates 查看已安装的 SSL 证书
certbot revoke --cert-name yourdomain.com 撤销证书
certbot delete --cert-name yourdomain.com 删除证书

5. 配置 HTTPS(Nginx 示例)

如果 Certbot 没有自动修改 Nginx 配置,可以手动添加:

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name yourdomain.com www.yourdomain.com;

    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;

    location / {
        root /var/www/html;
        index index.html index.php;
    }
}

然后重启 Nginx:

sudo systemctl restart nginx

6. 验证 SSL 证书

确认证书生效:

sudo openssl x509 -in /etc/letsencrypt/live/yourdomain.com/fullchain.pem -text -noout

访问 https://yourdomain.com 检查是否成功启用了 HTTPS。

 

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇