首页主机资讯Ubuntu Nginx如何配置自动续期SSL证书

Ubuntu Nginx如何配置自动续期SSL证书

时间2026-01-22 08:41:05发布访客分类主机资讯浏览710
导读:在Ubuntu上使用Nginx配置自动续期SSL证书,通常会结合Let’s Encrypt的Certbot工具来实现。Certbot提供了自动续期功能,并且可以与Nginx无缝集成。以下是详细步骤: 1. 安装Certbot和Nginx 首...

在Ubuntu上使用Nginx配置自动续期SSL证书,通常会结合Let’s Encrypt的Certbot工具来实现。Certbot提供了自动续期功能,并且可以与Nginx无缝集成。以下是详细步骤:

1. 安装Certbot和Nginx

首先,确保你已经安装了Nginx和Certbot。如果没有安装,可以使用以下命令进行安装:

sudo apt update
sudo apt install nginx certbot python3-certbot-nginx

2. 获取SSL证书

使用Certbot获取SSL证书。假设你的域名是example.com,运行以下命令:

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

Certbot会自动配置Nginx并重启Nginx服务以应用新的SSL证书。

3. 配置自动续期

Certbot会在安装时创建一个cron任务或systemd定时器来自动续期证书。你可以检查这些任务以确保它们已正确设置。

检查Cron任务

运行以下命令查看Certbot的cron任务:

crontab -l | grep certbot

你应该会看到类似以下的条目:

0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system &
    &
     perl -e 'sleep int(rand(43200))' &
    &
     certbot -q renew &
    &
     systemctl reload nginx

这个cron任务每12小时运行一次,检查证书是否需要续期,并在需要时自动续期并重启Nginx。

检查Systemd定时器

如果你使用的是systemd,可以检查Certbot的定时器:

systemctl list-timers | grep certbot

你应该会看到类似以下的条目:

certbot.service - Let's Encrypt client
   Loaded: loaded (/lib/systemd/system/certbot.service;
     enabled;
     vendor preset: enabled)
   Active: inactive (dead)

以及一个定时器:

certbot.timer - Run let's encrypt certificates
   Loaded: loaded (/lib/systemd/system/certbot.timer;
     enabled;
 vendor preset: enabled)
   Active: inactive (dead)

这个定时器通常每12小时运行一次。

4. 手动测试续期

你可以手动测试证书续期过程,确保一切正常:

sudo certbot renew --dry-run

如果没有错误,可以实际续期:

sudo certbot renew

5. 配置Nginx以使用SSL

确保你的Nginx配置文件中正确配置了SSL。以下是一个基本的Nginx SSL配置示例:

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

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    
    include /etc/letsencrypt/options-ssl-nginx.conf;
    
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;


    location / {
    
        root /var/www/html;
    
        index index.html index.htm;

    }

}
    

6. 重启Nginx

每次修改Nginx配置后,记得重启Nginx服务:

sudo systemctl restart nginx

通过以上步骤,你可以在Ubuntu上使用Nginx和Certbot实现SSL证书的自动续期。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Ubuntu Nginx如何配置自动续期SSL证书
本文地址: https://pptw.com/jishu/789790.html
Debian中Laravel项目如何配置数据库 C++程序在Ubuntu上如何进行网络编程

游客 回复需填写必要信息