Linux CentOS 7 使用certbot管理nginx证书

1、安装Certbot

yum install certbot python2-certbot-nginx

2、配置nginx

vim /etc/nginx/nginx.conf

内容如下:


user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
   worker_connections  1024;
}


http {
   include       /etc/nginx/mime.types;
   default_type  application/octet-stream;

   log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for"';

   access_log  /var/log/nginx/access.log  main;

   sendfile        on;
   #tcp_nopush     on;

   keepalive_timeout  65;

   #gzip  on;

   include /etc/nginx/conf.d/*.conf;
}

其中有一行

 include /etc/nginx/conf.d/*.conf;

可见我们只要在这个目录下新建一个xxx.conf即可被引用。

在上述目录下添加我们需要配置的文件homePage.conf
内容如下:

server {
    listen 80;
    server_name www.xxxxxx.com;
}

3、自动配置下载证书

先检查一下nginx的配置文件是否有错误

nginx -t

运行如下:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

自动配置:

certbot --nginx 

运行如下:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): #此处需要输入你的邮箱地址
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y #此处选Y

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y 此处选Y
Account registered.

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: www.xxxxx.com  #此处会列出你配置文件中包含的域名
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1 #选择域名对应的序号
Requesting a certificate for www.xxxxx.com
Performing the following challenges:
http-01 challenge for www.xxxxx.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/homePage.conf
Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/homePage.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://www.xxxxx.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Subscribe to the EFF mailing list (email: 你的邮箱地址).
Starting new HTTPS connection (1): supporters.eff.org
An unexpected error occurred:
TypeError: __str__ returned non-string (type Error)
Please see the logfiles in /var/log/letsencrypt for more details.

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/www.xxxxx.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/www.xxxxx.com/privkey.pem
   Your certificate will expire on 2022-03-17. To obtain a new or
   tweaked version of this certificate in the future, simply run
   certbot again with the "certonly" option. To non-interactively
   renew *all* of your certificates, run "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le


相关证书会生成在如下文件中:

/etc/letsencrypt/renewal/www.xxxxx.com.conf
/etc/letsencrypt/archive/www.xxxxx.com
/etc/letsencrypt/live/www.xxxxx.com

此时打开/etc/nginx/conf.d/homePage.conf;
内容如下:

server {
    server_name www.xxxxx.com;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/www.xxxxx.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/www.xxxxx.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
    if ($host = www.xxxxx.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name www.xxxxx.com;
    return 404; # managed by Certbot
}

可以看到帮我们自动生成了相关的配置文件

添加我们的跳转后,内容如下:

server {
    server_name www.xxxxx.com;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/www.xxxxx.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/www.xxxxx.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    location / {
        root   /homePage;
        index index.html;
    }
}
server {
    if ($host = www.xxxxx.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name www.xxxxx.com;
    return 404; # managed by Certbot
}


4、重启/启动nginx,访问域名查看是否配置成功

#启动
nginx -c /etc/nginx/nginx.conf
#重启
nginx -s reload

5、证书的有效期只有三个月,所以需要定时续期

以下部分摘抄整理来自:https://www.liaosam.com/use-cron-service-and-certbot-for-renewal-of-letsencrypt-ssl-certificates.html

5.1、检查 Cron 服务状态

service crond status

运行如下:


● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 五 2021-12-17 14:45:07 CST; 5h 5min ago
 Main PID: 14167 (crond)
   CGroup: /system.slice/crond.service
           └─14167 /usr/sbin/crond -n

12月 17 14:45:07 xxxxxxxxxxx systemd[1]: Stopped Command Scheduler.
12月 17 14:45:07 xxxxxxxxxxx systemd[1]: Started Command Scheduler.
12月 17 14:45:07 xxxxxxxxxxx crond[14167]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 59% if used.)
12月 17 14:45:07 xxxxxxxxxxx crond[14167]: (CRON) INFO (running with inotify support)
12月 17 14:45:07 xxxxxxxxxxx crond[14167]: (CRON) INFO (@reboot jobs will be run at computer's startup.)

如果执行后提示:crond (pid xxxxx) is running… 代表正常运行中,则可以跳过下面5.2、5.3两步 。

如果提示错误,不识别的服务,则先按照5.2和5.3执行安装和启动。


5.2、安装 cron 服务

依次输入以下 2 条命令并回车执行

yum -y install vixie-cron
yum -y install crontabs

成功安装 Cron 之后,启动 cron 服务。


5.3、启动 Cron 服务

service crond start

执行后会出现:Starting crond: [ OK ] 的提示,表明启动成功。

继续执行开机启动服务命令,把 Cron 加入开机启动的服务列表中:

chkconfig --level 345 crond on

安装完检查一下 Cron 服务状态

service crond status

如果提示:crond (pid xxxxx) is running… 代表正常运行中。


5.4、搜索 cron 文件所在位置

输入命令:

find / -name "cron"

找到如下结果:

/var/spool/cron
/var/log/cron
/etc/selinux/targeted/active/modules/100/cron

/var/log/cron 这个是日志文件位置,不管它
/var/spool/cron 这里是所有的自动执行任务的 cron 文件存放位置

打开 /var/spool/cron,看看 cron 目录下有没有文件。

  • 如果没有,创建 cron 文件,按照步骤5.5。
  • 如果有,跳过步骤5.5。

5.5、创建 Cron 文件

输入以下命令:

crontab -e

此时会创建一个新文件同时打开了vim

输入以下内容:

0 3 */7 * * /usr/bin/certbot renew --renew-hook "/usr/sbin/nginx -s reload"

上面这个/usr/bin/certbot和/usr/sbin/nginx 各自需要写成各自的路径可以用which certbot和which nginx查询
按住 shift+分号(打出冒号来),然后输入 wq,回车。退出编辑文件状态。

以上含义是:每隔 7 天,夜里 3 点整自动执行检查续期命令一次。续期完成后,重启 nginx 服务。


5.6、重启 Cron 服务,使之生效

service crond restart

5.7、手动尝试 Certbot 证书更新

/bin/certbot renew

6.可能遇到的问题

https://blog.csdn.net/sinat_39595180/article/details/88120604

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,273评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,349评论 3 398
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,709评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,520评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,515评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,158评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,755评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,660评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,203评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,287评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,427评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,122评论 5 349
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,801评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,272评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,393评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,808评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,440评论 2 359

推荐阅读更多精彩内容