關(guān)鍵字: DSM ssl? 證書
起因
群暉下自建服務(wù)(alist3)和系統(tǒng)服務(wù)在外部網(wǎng)絡(luò)訪問需要加ssl安全證書來實(shí)現(xiàn)基礎(chǔ)的傳輸保護(hù)。
申請證書和續(xù)期手動操作都還好,不算太麻煩,但是每個(gè)應(yīng)用單獨(dú)證書需要復(fù)制和重啟,再配合服務(wù)重啟一套下來就比較麻煩了,于是有了這篇文章和末尾的ssl自動續(xù)期和證書自動復(fù)制及服務(wù)自動重啟腳本。
方案
免費(fèi)ssl證書的申請,一般是let's encrypt 或者 Trust Asia ,泛域名三個(gè)月續(xù)期。手動操作比較繁瑣,且三個(gè)月操作一次更加的繁瑣。
自動化ssl續(xù)期/申請工具有:
- acme.sh (基于linux原生shell,不需要額外的安裝其他東西,但是可能一些非常規(guī)linux 系統(tǒng)下會有問題,比如群暉內(nèi)linux)
- certbot? (基于python,可以用acme代理認(rèn)證或者直接域名服務(wù)商dns更新,大部分云廠商都有插件支持)
- keyManager (Windows下,可以自動部署到服務(wù)器)
- ......
本來打算直接用acme.sh,但是不知道為什么我這邊群暉下(ssh 登錄群暉終端) acme.sh 總是openssl報(bào)錯,懶得去動系統(tǒng)內(nèi)的依賴(openssl動了可能影響很多東西),搜了下發(fā)現(xiàn)certbot也完整實(shí)現(xiàn)了acme協(xié)議,基于python實(shí)現(xiàn)的。
安裝 certbot(直接pip 安裝就行) 以及 ssl續(xù)期 網(wǎng)上有很多教程,就不詳細(xì)介紹了,腳本中域名和路徑、accesskey等自行更改。
?
# ssh
ssh 192.168.1.100
# root權(quán)限
sudo su -
# 插件按需安裝就行
pip3 install certbot certbot-dns-aliyun certbot-dns-cloudflare certbot-auth-dnspod
# 這里用阿里云演示
### 配置文件
mkdir -p /etc/letsencrypt/
cat > /etc/letsencrypt/credentials.ini <<EOF
dns_aliyun_access_key = 12345678
dns_aliyun_access_key_secret = 1234567890abcdef1234567890abcdef
EOF
chmod 644 /etc/letsencrypt/credentials.ini
# 申請命令
certbot certonly \
--authenticator=dns-aliyun \
--dns-aliyun-credentials=' /etc/letsencrypt/credentials.ini' \
-d example.com\
-d "*.example.com"
# 手動續(xù)期
cerbot renew -q
流程及腳本
這里來講一下本文的主要目的:
1. 自動續(xù)期腳本
2. 復(fù)制證書并重啟alist
3. 復(fù)制證書并重啟 nginx
具體路徑自己更改
#! /bin/bash
is_updated="false"
check_file_md5_and_replace(){
file1="$1"
file2="$2"
# 檢查文件是否存在
if [ ! -f "$file1" ] || [ ! -f "$file2" ]; then
echo "Error: Both files must exist."
return 1
fi
# 計(jì)算文件的MD5摘要
md5_file1=$(md5sum "$file1" | awk '{print $1}')
md5_file2=$(md5sum "$file2" | awk '{print $1}')
# 比較MD5摘要
if [ "$md5_file1" != "$md5_file2" ]; then
echo "MD5 mismatch. Copying $file1 to $file2."
cp "$file1" "$file2"
is_updated="true"
echo "Copy complete."
else
echo "MD5 match. No action needed."
fi
}
# 需要續(xù)期的域名,自行修改
domain=example.com
ssl_cert=/etc/letsencrypt/live/${domain}/cert.pem
ssl_chain=/etc/letsencrypt/live/${domain}/chain.pem
ssl_fullchain=/etc/letsencrypt/live/${domain}/fullchain.pem
ssl_privkey=/etc/letsencrypt/live/${domain}/privkey.pem
# renew ssl certs
/usr/bin/certbot renew -q
# update alist3 certs
target_ssl_fullchain=/volume1/@appdata/alist3/cert.crt
target_ssl_privkey=/volume1/@appdata/alist3/pri.key
check_file_md5_and_replace ${ssl_fullchain} ${target_ssl_fullchain}
if [ "$is_updated" = "true" ]; then
synopkg restart alist3
is_updated="false"
fi
check_file_md5_and_replace ${ssl_privkey} ${target_ssl_privkey}
# update syno default certs
target_path=/usr/syno/etc/certificate/_archive/VpBstU
target_ssl_cert=${target_path}/cert.pem
target_ssl_chain=${target_path}/chain.pem
target_ssl_fullchain=${target_path}/fullchain.pem
target_ssl_privkey=${target_path}/privkey.pem
check_file_md5_and_replace ${ssl_cert} ${target_ssl_cert}
if [ "$is_updated" = "true" ]; then
systemctl restart nginx
fi
check_file_md5_and_replace ${ssl_chain} ${target_ssl_chain}
check_file_md5_and_replace ${ssl_fullchain} ${target_ssl_fullchain}
check_file_md5_and_replace ${ssl_privkey} ${target_ssl_privkey}
# 查看證書信息
# openssl x509 -in VpBstU/fullchain.pem -text -noout|grep DNS
群暉下套件采用 synopkg 管理
系統(tǒng)服務(wù)使用 systemctl 管理
參考來源
使用 Certbot 自動申請并續(xù)訂阿里云 DNS 免費(fèi)泛域名證書_tabsp的博客-CSDN博客
Certbot免費(fèi)證書的安裝·使用·自動續(xù)期_certbot renew_BlackRockZero的博客-CSDN博客文章來源:http://www.zghlxwxcb.cn/news/detail-683170.html
查看 PEM DER 格式證書命令_centos查看pem證書命令_maimang09的博客-CSDN博客文章來源地址http://www.zghlxwxcb.cn/news/detail-683170.html
到了這里,關(guān)于群暉DSM下套件及系統(tǒng)網(wǎng)頁服務(wù)器ssl證書自動更新的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!