文中的操作都是在CentOS Stream release 9下執(zhí)行的,使用的是root用戶。
1. 安裝docker
# 卸載原有的docker
yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
# 安裝依賴
yum install -y yum-utils device-mapper-persistent-data lvm2
# 配置docker-ce源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 安裝docker
yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
# 安裝docker-compose
wget https://github.com/docker/compose/releases/download/v2.17.2/docker-compose-linux-x86_64
chmod +x docker-compose-linux-x86_64 && mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose && ldconfig
2. Let`s Encrypt及Certbot介紹
關(guān)于Let`s Encrypt可以參見這里。
certbot安裝使用參加這里。
3. Docker運(yùn)行Certbot獲取證書
為了方便維護(hù)、升級(jí),同時(shí)也避免破壞本地的開發(fā)環(huán)境,我這里使用docker方式來運(yùn)行certbot。整個(gè)過程分為兩步:首次申請(qǐng)證書和證書更新。
3.1 首次申請(qǐng)證書
因?yàn)槲业奈恼露际峭ㄟ^jekyll運(yùn)行的靜態(tài)網(wǎng)站,之后會(huì)通過nginx來運(yùn)行,所以這里就以nginx為例來配置網(wǎng)站的tls證書。
- 創(chuàng)建nginx配置文件
default.conf
:
server {
listen 80;
server_name example.com www.example.com;
# 高優(yōu)先級(jí),僅用于更新證書
location ~ /.well-known/acme-challenge {
allow all;
root /data/letsencrypt;
}
}
- docker-compose文件:
version: '3.3'
services:
nginx:
image: nginx:1.23.4-alpine
container_name: frontend
volumes:
- ./default.conf:/etc/nginx/conf.d/default.conf
- ./frontend:/usr/share/nginx/html
ports:
- 80:80
- 啟動(dòng)web服務(wù):
docker-compse up -d
- 啟動(dòng)
certbot
申請(qǐng)證書:
docker run --rm -it -v ./certbot/etc/letsencrypt:/etc/letsencrypt -v ./certbot/var/log/letsencrpt:/var/log/letsencrypt -v ./frontend:/data/letsencrypt certbot/certbot:latest certonly --webroot --email your@eamil.com --agree-tos --no-eff-email --webroot-path=/data/letsencrypt -d example.com -d example.com
運(yùn)行結(jié)束后可以在./certbot/etc/letsencrypt/live
目錄下找到example.com
文件夾,其中包含證書文件fullchain.pem
和私鑰文件privkey.pem
。
- 停止web服務(wù):
docker-compose down
- 更新compose文件:
version: '3.3'
services:
nginx:
image: nginx:1.23.4-alpine
container_name: frontend
volumes:
- ./default.conf:/etc/nginx/conf.d/default.conf
# - ./frontend:/usr/share/nginx/html
- ./certbot/etc/letsencrypt/live:/letsencrypt/live # 當(dāng)前證書目錄
- ./certbot/etc/letsencrypt/archive:/letsencrypt/archive # 歷史證書目錄
- ./dhparam-2048.pem:/letsencrypt/dhparam-2048.pem # 使用2048位DH(Diffie-Hellman)參數(shù)
ports:
- 80:80
- 443:443
2048為DH參數(shù)生成命令:openssl dhparam -out ./dhparam-2048.pem 2048
文章來源:http://www.zghlxwxcb.cn/news/detail-427807.html
- 更新nginx配置文件
# 處理http請(qǐng)求
server {
listen 80;
server_name example.com www.example.com;
# 重定向到https
location / {
rewrite ^ https://$host$request_uri? permanent;
}
# 高優(yōu)先級(jí),僅用于更新證書
location ~ /.well-known/acme-challenge {
allow all;
root /data/letsencrypt;
}
}
# 處理https請(qǐng)求
server {
listen 443 ssl http2;
server_name example.com www.example.com;
server_tokens off;
ssl_certificate /letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /letsencrypt/live/example.com/privkey.pem;
ssl_buffer_size 8k;
ssl_dhparam /letsencrypt/dhparam-2048.pem; # 使用2048位DH參數(shù),加強(qiáng)安全
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;
ssl_ecdh_curve secp384r1;
ssl_session_tickets off;
# OCSP stapling
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8;
root /usr/share/nginx/html;
index index.html;
}
- 重新啟動(dòng)web服務(wù):
docker-compose up -d
3.2 證書更新
- 通過以下腳本可以實(shí)現(xiàn)證書更新:
#!/bin/bash
docker run -it --rm \
-v ./certbot/etc/letsencrypt:/etc/letsencrypt \
-v ./certbot/var/lib/letsencrypt:/var/lib/letsencrypt \
-v ./certbot/var/log/letsencrypt:/var/log/letsencrypt \
-v ./site:/data/letsencrypt \
certbot/certbot \
renew --webroot -w /data/letsencrypt --quiet && docker kill --signal=HUP frontend
- crontab -e新增一條定時(shí)任務(wù),每月1號(hào)00:00更新一次證書:
0 0 1 * * {{YOURPATH}}/renew.sh
聲明:本作品采用署名-非商業(yè)性使用-相同方式共享 4.0 國(guó)際 (CC BY-NC-SA 4.0)進(jìn)行許可,使用時(shí)請(qǐng)注明出處。
Author: mengbin
blog: mengbin
Github: mengbin92
cnblogs: 戀水無意文章來源地址http://www.zghlxwxcb.cn/news/detail-427807.html
到了這里,關(guān)于Docker獲取Let`s Encrypt SSL 證書的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!