阿晨的运维笔记 | 一键HTTPS并开启自动更新

之前阿晨就分享过,阿晨的运维笔记 | 只要5分钟,给你的网站插上Https的翅膀,但是现在阿里云证书取消了免费证书的售卖,而且之前那种模式免不了一年后忘记重新配置导致网站HTTPS过期的问题。所以今天阿晨分享一个一劳永逸的方法!

开始之前,建议按照阿晨的另一篇Ubuntu一键部署Docker先部署上DockerDocker Compose,因为待会会用上。

方案简介

Let’s Encrypt和CertBot

我们申请和使用Let's Encrypt的免费HTTPS证书, 就需要一个证书申请和管理的工具, 然后certbot是官方推荐的申请工具, 我们使用这个工具申请和管理我们的证书

certbot支持大部分的Linux发行版

上面也提到,现在阿里云不售卖免费证书了,但是如果我们(实际上是公司)想白嫖怎么办呢?就得用到Let's Encrypt了。

下面阿晨就分享下如何一键部署CertBot并开启自动续期。

方案实施

准备

  • Docker以及Docker Compose环境
  • 域名DNS已经指向待部署的服务器,因为脚本校验证书所属权,需要访问域名

废话少说,这就开始!

1、克隆仓库

💡 提示:这一步必不可少,一定要按照仓库目录结构来执行,完成后,可以自行更改 nginx/conf.d 下的配置文件。

$ mkdir -p /data
$ cd /data
$ git clone https://ghproxy.com/https://github.com/gcdd1993/nginx-certbot
$ cd nginx-certbot
$ ls -l
drwxr-xr-x 4 root root 4096 Jun  8 22:01 ./
drwxr-xr-x 5 root root 4096 Jun  8 21:49 ../
drwxr-xr-x 4 root root 4096 Jun  8 21:53 data/
-rw-r--r-- 1 root root  660 Jun  8 21:49 docker-compose.yml
drwxr-xr-x 8 root root 4096 Jun  8 21:49 .git/
-rw-r--r-- 1 root root   14 Jun  8 21:49 .gitignore
-rwxr-xr-x 1 root root 2286 Jun  8 22:01 init-letsencrypt.sh*
-rw-r--r-- 1 root root 1074 Jun  8 21:49 LICENSE
-rw-r--r-- 1 root root 1376 Jun  8 21:49 README.md

2、修改邮箱

$ vim init-letsencrypt.sh
...
email="gcwm99@gmail.com"
...

3、修改操作域名

修改your_domain为你的域名(只能是单域名,不能是泛域名)

$ sed -i 's/example.org/your_domain/g' data/nginx/app.conf \
	&& sed -i 's/example.org/your_domain/g' init-letsencrypt.sh

4、执行脚本

出现以下内容,说明已经成功!

$ ./init-letsencrypt.sh
...
Requesting a certificate for your_domain

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/your_domain/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/your_domain/privkey.pem
This certificate expires on 2021-09-06.
These files will be updated when the certificate renews.

NEXT STEPS:
- The certificate will need to be renewed before it expires. Certbot can automatically renew the certificate in the background, but you may need to take steps to enable that functionality. See https://certbot.org/renewal-setup for instructions.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

5、修改配置

将配置更改为你自己网站的配置,下面给一个示例配置

$ mv app.conf app.conf.bak # 注释默认配置
$ cd /data/nginx-cert/data/nginx
$ vim nginx-example.conf # 编写自己的配置
upstream my.site {
    server localhost:8080;
}
server {
    server_name your_domain;

    proxy_read_timeout 600s;
    proxy_send_timeout 600s;

    location / {
        add_header X-Frame-Options deny;
        proxy_pass http://my.site;
    }

	# 以下内容保持不变即可,只需要修改your_domain为你的域名
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/your_domain/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your_domain/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    server_tokens off;
}
# 以下内容保持不变即可,只需要修改your_domain为你的域名
server {
    if ($host = your_domain) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    server_name your_domain;
    listen 80;
    return 404; # managed by Certbot
}

6、重启Nginx服务

$ cd /data/nginx-certbot
$ docker-compose restart

附录

多域名操作

步骤同上,先修改域名为待操作域名,然后执行 init-letsencrypt.sh

$ sed -i 's/your_domain/your_domain2/g' data/nginx/app.conf \
	&& sed -i 's/your_domain/your_domain2/g' init-letsencrypt.sh
$ ./init-letsencrypt.sh
...

更新证书

$ docker exec -it nginx-certbot_certbot_1 certbot renew
# ...
The following certificates are not due for renewal yet:
  /etc/letsencrypt/live/my.site/fullchain.pem expires on 2021-09-06 (skipped)
No renewals were attempted. # 还未到更新时间,证明证书还是有效的
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

修改更新间隔

修改docker-compose.yml里面的间隔即可。

$ cd /data/nginx-certbot
$ vim docker-compose.yml
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'" # 修改12h为你喜欢的值

# 修改完毕,重启
$ docker-compose restart

如果本篇博客对您有一定的帮助,大家记得留言+点赞+收藏哦。
image-20210723185904932
我是阿晨,在技术的道路上我们一起砥砺前行!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿晨聊技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值