Debian安装Certbot获取SSL证书

安装snapd

# 安装snapd
apt-get update
apt-get install snapd

# 安装 snapd core
snap install core
snap refresh core

通过snapd安装certbot

snap install --classic certbot

# 软链接
ln -s /snap/bin/certbot /usr/bin/certbot

如果遇到无法下载,可能需要翻墙,请给snap设置代理:

sudo snap set system proxy.http=http://127.0.0.1:1081
sudo snap set system proxy.https=http://127.0.0.1:1081

获取SSL证书

执行certbot certonly

certbot certonly -d "*.youdomain.com" --manual --preferred-challenges dns-01  --server https://acme-v02.api.letsencrypt.org/directory
root@docker00:/usr/bin# certbot certonly -d "*.youdomain.com" --manual --preferred-challenges dns-01  --server https://acme-v02.api.letsencrypt.org/directory
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): xxx@xxx.com

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

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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: N
Account registered.
Requesting a certificate for *.youdomain.com
Performing the following challenges:
dns-01 challenge for youdomain.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.youdomain.com with the following value:

<your_dns_txt_value>

Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/youdomain.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/youdomain.com/privkey.pem
   Your certificate will expire on 2021-07-01. To obtain a new or
   tweaked version of this certificate in the future, simply run
   certbot again. 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

添加DNS TXT记录

上面会通过获取DNS TXT验证域名是否是本人所有,进入域名管理中添加该记录

主机记录:_acme-challenge

记录类型:TXT

记录值:<your_dns_txt_value>(此处根据实际值进行替换)

image-20210402092759560

手工验证DNS TXT记录

工具网站:https://mxtoolbox.com/TXTLookup.aspx

填写:_acme-challenge.youdomain.com

image-20210402093308991

拷贝证书

手工方式

进入生成的证书目录:

cd /etc/letsencrypt/live/youdomain.com/
root@docker00:/usr/bin# cd /etc/letsencrypt/live/youdomain.com/
root@docker00:/etc/letsencrypt/live/youdomain.com# ll
total 4
lrwxrwxrwx 1 root root  34 Apr  2 09:23 cert.pem -> ../../archive/youdomain.com/cert1.pem
lrwxrwxrwx 1 root root  35 Apr  2 09:23 chain.pem -> ../../archive/youdomain.com/chain1.pem
lrwxrwxrwx 1 root root  39 Apr  2 09:23 fullchain.pem -> ../../archive/youdomain.com/fullchain1.pem
lrwxrwxrwx 1 root root  37 Apr  2 09:23 privkey.pem -> ../../archive/youdomain.com/privkey1.pem
-rw-r--r-- 1 root root 692 Apr  2 09:23 README

拷贝并重命名

cp fullchain.pem /root/youdomain.com.cert
cp privkey.pem /root/youdomain.com.key

# 设置权限
chmod 644 /root/youdomain.com.cert /root/youdomain.com.key

# 将证书复制至目标服务器
cd /root
scp -P <port> youdomain.com.cert youdomain.com.key <user>@<host>:/tmp/

接着通过ssh登录目标服务替换新的ssl证书并生效即可。

使用Shell脚本

在生成证书机器执行:

#!/bin/bash

sourceFolder='/etc/letsencrypt/live/youdomain.com'
sourceCertFile='fullchain.pem'
sourceKeyFile='privkey.pem'

targetFolder='/tmp'
targetCertFile='youdomain.com.cert'
targetKeyFile='youdomain.com.key'

# copy cert and key files
scp -P <port> ${sourceFolder}/${sourceCertFile} <user>@<host>:${targetFolder}/${targetCertFile}
scp -P <port> ${sourceFolder}/${sourceKeyFile} <user>@<host>:${targetFolder}/${targetKeyFile}

# 可选:直接调用服务器脚本执行,如果不方便,可以直接连上服务器端进行执行脚本
# ssh -p <port> <user>@<host> "<script_folder>/letsencrypt_update.sh"

服务器端脚本:

#!/bin/bash

targetFolder='/tmp'

# 证书 youdomain.com
# certFile='youdomain.com.cert'
# keyFile='youdomain.com.key'
targetCertFile='youdomain.com.cert'
targetKeyFile='youdomain.com.key'
sslCertFile='/data/soft/nginx/conf/cert/youdomain.com/youdomain.com.cert'
sslKeyFile='/data/soft/nginx/conf/cert/youdomain.com/youdomain.com.key'

if [ -f "${targetFolder}/${targetCertFile}" -a -f "${targetFolder}/${targetKeyFile}" ]; then
    cat ${targetFolder}/${targetCertFile} > ${sslCertFile}
    cat ${targetFolder}/${targetKeyFile} > ${sslKeyFile}

    rm -f ${targetFolder}/${targetCertFile}
    rm -f ${targetFolder}/${targetKeyFile}
fi

# 多个证书可以类似添加
# 证书 frp.youdomain.com
# certFile='frp.youdomain.com.cert'
# keyFile='frp.youdomain.com.key'
targetCertFile='frp.youdomain.com.cert'
targetKeyFile='frp.youdomain.com.key'
sslCertFile='/data/soft/nginx/conf/cert/frp.youdomain.com/frp.youdomain.com.cert'
sslKeyFile='/data/soft/nginx/conf/cert/frp.youdomain.com/frp.youdomain.com.key'

if [ -f "${targetFolder}/${targetCertFile}" -a -f "${targetFolder}/${targetKeyFile}" ]; then
    cat ${targetFolder}/${targetCertFile} > ${sslCertFile}
    cat ${targetFolder}/${targetKeyFile} > ${sslKeyFile}

    rm -f ${targetFolder}/${targetCertFile}
    rm -f ${targetFolder}/${targetKeyFile}
fi

# 重启nginx使ssl证书生效
/data/soft/nginx/sbin/nginx -s reload

标签: linux, tools, ssl

已有 14 条评论

  1. 收藏,好好学习

  2. 想想你的文章写的特别好

  3. 不错不错,我喜欢看 https://www.237fa.com/

  4. 文章的确不错啊https://www.cscnn.com/

  5. 你的文章内容非常卖力,让人点赞。http://www.gahaiqinaili.com

  6. 《美人为馅》国产剧高清在线免费观看:https://www.jgz518.com/xingkong/70897.html

  7. 《美人为馅》国产剧高清在线免费观看:https://www.jgz518.com/xingkong/70897.html

  8. 《新品上市便利餐厅》日韩综艺高清在线免费观看:https://www.jgz518.com/xingkong/100917.html

  9. 你的文章让我感受到了生活的美好,谢谢! https://www.yonboz.com/video/98380.html

  10. 《皇家飞凤》动作片高清在线免费观看:https://www.jgz518.com/xingkong/23567.html

  11. 《不要追究过去》韩国剧高清在线免费观看:https://www.jgz518.com/xingkong/121949.html

  12. ?学术类评语?

  13. 新盘 上车集合 留下 我要发发 立马进裙coinsrore.com

  14. 2025年10月新盘 做第一批吃螃蟹的人coinsrore.com
    新车新盘 嘎嘎稳 嘎嘎靠谱coinsrore.com
    新车首发,新的一年,只带想赚米的人coinsrore.com
    新盘 上车集合 留下 我要发发 立马进裙coinsrore.com
    做了几十年的项目 我总结了最好的一个盘(纯干货)coinsrore.com
    新车上路,只带前10个人coinsrore.com
    新盘首开 新盘首开 征召客户!!!coinsrore.com
    新项目准备上线,寻找志同道合 的合作伙伴coinsrore.com
    新车即将上线 真正的项目,期待你的参与coinsrore.com
    新盘新项目,不再等待,现在就是最佳上车机会!coinsrore.com
    新盘新盘 这个月刚上新盘 新车第一个吃螃蟹!coinsrore.com

添加新评论