ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

Ubuntu 24.04 Certbot 自动化SSL证书配置指南

Ubuntu 24.04 Certbot 自动化SSL证书配置指南 Ubuntu 24.04 Certbot 自动化SSL证书配置指南简介Let’s Encrypt是一个免费、开放、自动化的证书颁发机构CA由非营利组织 Internet Security Research GroupISRG运营。它提供有效期 90 天的免费 SSL/TLS 证书任何人都可以申请使用。Certbot是 Let’s Encrypt 官方推荐的 ACME 客户端用于自动申请、续期和管理 SSL 证书。它支持多种验证方式如--webroot、--nginx、--standalone可灵活适配不同的服务器环境。环境说明系统Ubuntu 24.04Web 服务器Nginx域名certbot.xxxx.com证书工具Certbot Let’s Encrypt一、安装certbotsudoaptupdatesudoaptinstallcertbot-y安装完成后验证certbot--version二、申请证书sudocertbot certonly--webroot\-w/usr/local/nginx-1.26/html\-dcertbot.xxxx.com\--agree-tos\--email123456789qq.com参数说明certonly只获取证书不修改 Nginx 配置--webroot通过网站根目录验证域名所有权-w /usr/local/nginx-1.26/html换成你的网站根目录路径-d指定要申请证书的域名--agree-tos同意 Let’s Encrypt 的服务条款--email用于注册 ACME 账号和接收证书到期提醒等待过后我们可以看到证书已经成功申请到了路径是/etc/letsencrypt/live/certbot.xxxx.com/fullchain.pem /etc/letsencrypt/live/certbot.xxxx.com/privkey.pem三、添加自动续期定时任务配置自动续期任务让证书在到期前自动续签并重载 Nginx无需手动干预。1、编辑cron任务sudocrontab-e在文件末尾添加一行03* * * /usr/bin/certbot renew--quiet--renew-hook/usr/local/nginx-1.26/sbin/nginx -t /usr/local/nginx-1.26/sbin/nginx -s reload参数说明0 3 * * *每天凌晨 3 点执行certbot renew检查证书是否需要续期只有到期前 30 天内才会真正续期--renew-hook续期成功后先检查 Nginx 配置语法再重载防止配置错误导致服务中断2、验证cron任务已添加sudocrontab-l3、测试续期流程是否正常sudocertbot renew --dry-run看到以上信息即表示续期流程正常工作。四、配置 Nginx配置 Nginx 启用 HTTPS使证书生效编辑你的 Nginx 配置文件我的路径为/usr/local/nginx-1.26/conf/nginx.conf写入以下测试内容worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name certbot.xxxx.com; return 301 https://$server_name$request_uri; } server { listen 443 ssl; server_name certbot.xxxx.com; ssl_certificate /etc/letsencrypt/live/certbot.xxxx.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/certbot.xxxx.com/privkey.pem; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location /50x.html { root html; } } }然后执行/usr/local/nginx-1.26/sbin/nginx-t/usr/local/nginx-1.26/sbin/nginx-sreload五、验证HTTPS生效我们来验证一下 HTTPS证书 是否真的生效了在浏览器里打开https://certbot.xxxx.com能看到小锁图标就说明 HTTPS 正常工作了。如需验证自动续期流程是否真正跑通可以强制续期一次sudocertbot renew --force-renewal --renew-hook/usr/local/nginx-1.26/sbin/nginx -t /usr/local/nginx-1.26/sbin/nginx -s reload续期后查看证书信息序列号和到期日期已更新说明续期流程正常sudocertbot certificates
返回列表