之前阿晨就分享過,阿晨的運維筆記 | 只要5分鐘,給你的網站插上Https的翅膀,但是現在阿里云證書取消了免費證書的售賣,而且之前那種模式免不了一年后忘記重新配置導致網站HTTPS過期的問題,所以今天阿晨分享一個一勞永逸的方法!
開始之前,建議按照阿晨的另一篇Ubuntu一鍵部署Docker先部署上Docker和Docker 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
如果本篇博客對您有一定的幫助,大家記得留言+點贊+收藏哦,

我是阿晨,在技術的道路上我們一起砥礪前行!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/290406.html
標籤:其他
上一篇:?? 「 插入排序 」 ??
