Skip to content

按照提示输入你的阿里云访问密钥(AccessKey ID 和 AccessKey Secret)、默认的地域 ID 等信息,配置完成后就可以使用阿里云 CLI 工具来管理你的阿里云资源了。

如何获取阿里云访问密钥(AccessKey ID 和 AccessKey Secret)请参考官方文档:https:Profile|Credential|Valid|Region|Language---------|------------------|-------|----------------|--------default||Invalid||enAkProfile*|AK:***jAc|Valid|oss-cn-shanghai|en#目前存在两个配置文件,分别是 default 和 AkProfile。其中 AkProfile 后面带有 *号,这表明当前使用的默认配置文件是 AkProfile。

🍊 配置日志文件

3、申请证书

测试是否能正确申请:

警告

.onedayxyy.cn 请换成自己的主域名

sh
certbotcertonly-donedayxyy.cn-d*.onedayxyy.cn--manual--preferred-challengesdns--manual-auth-hook"alidns"--manual-cleanup-hook"alidns clean"--dry-run
sh
#输出[root@blog ~]# certbot certonly -d onedayxyy.cn -d *.onedayxyy.cn --manual --preferred-challenges dns --manual-auth-hook "alidns"--manual-cleanup-hook "alidns clean"--dry-runSavingdebuglogto/var/log/letsencrypt/letsencrypt.logSimulatingacertificaterequestfor*.onedayxyy.cnHook'--manual-auth-hook'foronedayxyy.cnranwithoutput:{"RecordId":"189619603129344","RequestId":"572026AD-B305831B4ABE"}Hook'--manual-cleanup-hook'foronedayxyy.cnranwithoutput:{"RecordId":"18961344","RequestId":"8B219E945642"}Thedryrunwassuccessful.#看到"The dry run was successful."模拟成功!

正式申请时去掉 --dry-run参数:

sh
certbotcertonly-donedayxyy.cn-d*.onedayxyy.cn--manual--preferred-challengesdns--manual-auth-hook"alidns"--manual-cleanup-hook"alidns clean"

输出:

bash
[root@blog ~]# certbot certonly -d onedayxyy.cn -d *.onedayxyy.cn --manual --preferred-challenges dns --manual-auth-hook "alidns"--manual-cleanup-hook "alidns clean"Savingdebuglogto/var/log/letsencrypt/letsencrypt.logRequestingacertificatefor*.onedayxyy.cnHook'--manual-auth-hook'foronedayxyy.cnranwithoutput:{"RecordId":"189619072","RequestId":"654E1FE97B193D5A"}Hook'--manual-cleanup-hook'foronedayxyy.cnranwithoutput:{"RecordId":"1896643072","RequestId":"492AB-75267001AAB7"}Successfullyreceivedcertificate.Certificateissavedat:/etc/letsencrypt/live/onedayxyy.cn-0001/fullchain.pemKeyissavedat:/etc/letsencrypt/live/onedayxyy.cn-0001/privkey.pemThiscertificateexpireson2025-05-31.Thesefileswillbeupdatedwhenthecertificaterenews.Certbothassetupascheduledtasktoautomaticallyrenewthiscertificateinthebackground.----------------------------------------IfyoulikeCertbot,pleaseconsidersupportingourworkby:*DonatingtoISRG/Let's Encrypt:https:*Donating to EFF:https:- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

参数说明

  • -d example.com:指定域名(可以指定多个域名)。
  • manual:手动模式。
  • preferred-challenges dns:使用 DNS 挑战。
  • manual-auth-hook “alidns”:在 DNS 挑战时调用 alidns 脚本。
  • manual-cleanup-hook “alidns clean”:在 DNS 挑战完成后调用 alidns clean 脚本。
  • —dry-run:模拟续期过程,不会实际续期证书。正式申请时去掉—dry-run参数:

查看下证书:

bash
Certificateissavedat:/etc/letsencrypt/live/onedayxyy.cn-0001/fullchain.pemKeyissavedat:/etc/letsencrypt/live/onedayxyy.cn-0001/privkey.pem[root@blog ~]# ll /etc/letsencrypt/live/onedayxyy.cn-0001/fullchain.pemlrwxrwxrwx1rootroot46Mar221:55/etc/letsencrypt/live/onedayxyy.cn-0001/fullchain.pem->../../archive/onedayxyy.cn-0001/fullchain1.pem[root@blog ~]# ll /etc/letsencrypt/live/onedayxyy.cn-0001/privkey.pemlrwxrwxrwx1rootroot44Mar221:55/etc/letsencrypt/live/onedayxyy.cn-0001/privkey.pem->../../archive/onedayxyy.cn-0001/privkey1.pem[root@blog ~]#

4、配置证书

将上面获取到的证书配置到自己的nginx:(如果有cdn也需要配置到cdn里哦)

vim /etc/nginx/conf.d/wiki.onedayxyy.cn.conf

详细信息
bash
# cat /etc/nginx/conf.d/wiki.onedayxyy.cn.confserver{listen80;server_namewiki.onedayxyy.cn;#配置https重定向return301https:}server{listen443ssl;server_namewiki.onedayxyy.cn;root/root/rsync/rsync-vitepress/dist;location/{indexindex.htmlindex.htm;} location/images{alias/images;indexindex.html;valid_referersnoneblocked*.onedayxyy.cnonedayxyy.cn*.gitee.comgitee.comlocalhost127.0.0.1;if($invalid_referer ) {# 如不满足,指定访问如下资源rewrite^/https:return403;}}location/home{alias/root/home3.0/;indexindex.htmlindex.htm;}location/wiki{alias/root/rsync/rsync-docusaurus/build;indexindex.htmlindex.htm;}location/newyear{alias/root/rsync/rsync-qianduan-demo/qianduan-demo/newyear;indexindex.htmlindex.htm;}location/error{autoindexon;# 启用目录索引alias/FdangDaoLianImages;indexindex.html;# 默认显示index.html文件,如果没有则列出目录内容} ssl_certificate/etc/letsencrypt/live/onedayxyy.cn-0001/fullchain.pem;ssl_certificate_key/etc/letsencrypt/live/onedayxyy.cn-0001/privkey.pem;ssl_session_timeout5m;ssl_protocolsTLSv1.1TLSv1.2TLSv1.3;ssl_ciphersEECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;ssl_prefer_server_cipherson;add_headerStrict-Transport-Security"max-age=31536000";access_log/var/log/nginx/wiki.onedayxyy.cn.https.log;}
  • 重启nginx:
bash
nginx-sreload
  • 配置下cdn
bash
cat/etc/letsencrypt/live/onedayxyy.cn-0001/fullchain.pemcat/etc/letsencrypt/live/onedayxyy.cn-0001/privkey.pem

拷贝证书的公钥和私钥内容到cdn配置里:

image-20250302224620546

  • 浏览器验证下

可以看到自己的主域名和子域名的证书都是正常的。(完美😜)

image-20250305073708731

image-20250305073737633

5、证书续期

sh
certbotrenew--manual--preferred-challengesdns--manual-auth-hook"alidns"--manual-cleanup-hook"alidns clean"--dry-run

如果以上命令没有错误,把 --dry-run参数去掉。

sh
certbotrenew--manual--preferred-challengesdns--manual-auth-hook"alidns"--manual-cleanup-hook"alidns clean"

🍊 检查证书状态

运行以下命令查看证书的到期时间:

sh
certbotcertificates

输出:

bash
[root@blog ~]# certbot certificatesSavingdebuglogto/var/log/letsencrypt/letsencrypt.logRenewalconfigurationfile/etc/letsencrypt/renewal/onedayxyy.cn.confproducedanunexpectederror:expected/etc/letsencrypt/live/onedayxyy.cn/cert.pemtobeasymlink.Skipping.Renewalconfigurationfile/etc/letsencrypt/renewal/wiki.onedayxyy.cn.confproducedanunexpectederror:expected/etc/letsencrypt/live/wiki.onedayxyy.cn/cert.pemtobeasymlink.Skipping.----------------------------------------Foundthefollowingcerts:CertificateName:onedayxyy.cn-0001SerialNumber:3b19b47d3bf12688b69a9189e9da78cc117KeyType:ECDSADomains:*.onedayxyy.cnExpiryDate:2025-05-3112:57:22+00:00(VALID:89days)CertificatePath:/etc/letsencrypt/live/onedayxyy.cn-0001/fullchain.pemPrivateKeyPath:/etc/letsencrypt/live/onedayxyy.cn-0001/privkey.pemThefollowingrenewalconfigurationswereinvalid:/etc/letsencrypt/renewal/onedayxyy.cn.conf/etc/letsencrypt/renewal/wiki.onedayxyy.cn.conf----------------------------------------[root@blog ~]#

6、自动续期

添加定时任务 crontab。

sh
crontab-e

然后添加以下配置:

sh
11*/1**rootcertbotrenew--manual--preferred-challengesdns--manual-auth-hook"alidns"--manual-cleanup-hook"alidns clean"--deploy-hook"nginx -s reload"

上面脚本中的 --deploy-hook "nginx -s reload"表示在续期成功后自动重启 nginx。

FAQ

案例:certbot-renew.log 没有日志输出

没有日志输出,如果 /var/log/certbot-renew.log 没有日志输出,可能是以下原因:权限问题:确保日志文件可写:

警告

这里需要执行下这2行命令的,否则没这个日志文件;

sh
sudotouch/var/log/certbot-renew.logsudochmod644/var/log/certbot-renew.log

案例:cron 任务未执行

检查 cron 日志:

sh
grepCRON/var/log/syslog

案例:证书未到期

如果证书未到期,Certbot 会跳过续期。可以通过以下命令强制续期:

sh
certbotrenew--force-renewal--preferred-challengesdns--manual-auth-hook"alidns"--manual-cleanup-hook"alidns clean"

案例:如何获取阿里云访问密钥及Region Id

(1)如何获取阿里云访问密钥(AccessKey ID 和 AccessKey Secret)请参考官方文档:https:{"Regions":{"Region":[{"LocalName":"华北1(青岛)","RegionEndpoint":"ecs.cn-qingdao.aliyuncs.com","RegionId":"cn-qingdao"},{"LocalName":"华北2(北京)","RegionEndpoint":"ecs.cn-beijing.aliyuncs.com","RegionId":"cn-beijing"},{"LocalName":"华北3(张家口)","RegionEndpoint":"ecs.cn-zhangjiakou.aliyuncs.com","RegionId":"cn-zhangjiakou"},{"LocalName":"华北5(呼和浩特)","RegionEndpoint":"ecs.cn-huhehaote.aliyuncs.com","RegionId":"cn-huhehaote"},{"LocalName":"华北6(乌兰察布)","RegionEndpoint":"ecs.cn-wulanchabu.aliyuncs.com","RegionId":"cn-wulanchabu"},{"LocalName":"华东1(杭州)","RegionEndpoint":"ecs.aliyuncs.com","RegionId":"cn-hangzhou"},{"LocalName":"华东2(上海)","RegionEndpoint":"ecs.cn-shanghai.aliyuncs.com","RegionId":"cn-shanghai"},{"LocalName":"华东5(南京-本地地域)","RegionEndpoint":"ecs.cn-nanjing.aliyuncs.com","RegionId":"cn-nanjing"},{"LocalName":"华南1(深圳)","RegionEndpoint":"ecs.cn-shenzhen.aliyuncs.com","RegionId":"cn-shenzhen"},{"LocalName":"华南2(河源)","RegionEndpoint":"ecs.cn-heyuan.aliyuncs.com","RegionId":"cn-heyuan"},{"LocalName":"华南3(广州)","RegionEndpoint":"ecs.cn-guangzhou.aliyuncs.com","RegionId":"cn-guangzhou"},{"LocalName":"华东6(福州-本地地域)","RegionEndpoint":"ecs.cn-fuzhou.aliyuncs.com","RegionId":"cn-fuzhou"},{"LocalName":"华中1(武汉-本地地域)","RegionEndpoint":"ecs.cn-wuhan-lr.aliyuncs.com","RegionId":"cn-wuhan-lr"},{"LocalName":"西南1(成都)","RegionEndpoint":"ecs.cn-chengdu.aliyuncs.com","RegionId":"cn-chengdu"},{"LocalName":"中国香港","RegionEndpoint":"ecs.cn-hongkong.aliyuncs.com","RegionId":"cn-hongkong"},{"LocalName":"日本(东京)","RegionEndpoint":"ecs.ap-northeast-1.aliyuncs.com","RegionId":"ap-northeast-1"},{"LocalName":"韩国(首尔)","RegionEndpoint":"ecs.ap-northeast-2.aliyuncs.com","RegionId":"ap-northeast-2"},{"LocalName":"新加坡","RegionEndpoint":"ecs.ap-southeast-1.aliyuncs.com","RegionId":"ap-southeast-1"},{"LocalName":"马来西亚(吉隆坡)","RegionEndpoint":"ecs.ap-southeast-3.aliyuncs.com","RegionId":"ap-southeast-3"},{"LocalName":"菲律宾(马尼拉)","RegionEndpoint":"ecs.ap-southeast-6.aliyuncs.com","RegionId":"ap-southeast-6"},{"LocalName":"印度尼西亚(雅加达)","RegionEndpoint":"ecs.ap-southeast-5.aliyuncs.com","RegionId":"ap-southeast-5"},{"LocalName":"泰国(曼谷)","RegionEndpoint":"ecs.ap-southeast-7.aliyuncs.com","RegionId":"ap-southeast-7"},{"LocalName":"美国(弗吉尼亚)","RegionEndpoint":"ecs.us-east-1.aliyuncs.com","RegionId":"us-east-1"},{"LocalName":"美国(硅谷)","RegionEndpoint":"ecs.us-west-1.aliyuncs.com","RegionId":"us-west-1"},{"LocalName":"墨西哥","RegionEndpoint":"ecs.na-south-1.aliyuncs.com","RegionId":"na-south-1"},{"LocalName":"英国(伦敦)","RegionEndpoint":"ecs.eu-west-1.aliyuncs.com","RegionId":"eu-west-1"},{"LocalName":"阿联酋(迪拜)","RegionEndpoint":"ecs.me-east-1.aliyuncs.com","RegionId":"me-east-1"},{"LocalName":"德国(法兰克福)","RegionEndpoint":"ecs.eu-central-1.aliyuncs.com","RegionId":"eu-central-1"}]},"RequestId":"CB0F1AC0-5E28-54EA-A2A9-5F583B2D7A0D"}[root@blog ~]#

案例:http获取证书方式不支持泛域名证书生成

警告

Let's Encrypt 对通配符域名的验证要求使用 DNS 验证dns-01),而不是 HTTP 验证(http-01) ;(即http获取证书方式不支持泛域名证书生成)

因此,推荐使用dns方式来生成泛域名证书;

案例:cdn加速主域名后是默认不会对子域名也加速的

问题:cdn上我配置的是对主域名onedayxyy.cn加速,请问这个对其子域名wiki.onedayxyy.cn也会加速吗?-2024.7.24(已解决)

回答:

警告

CDN(内容分发网络)的配置通常是针对具体的域名进行的。因此,如果您的 CDN 配置是针对主域名 onedayxyy.cn进行加速,那么它并不会自动对子域名 wiki.onedayxyy.cn进行加速。子域名需要单独配置才能享受 CDN 加速服务。

案例:切记申请泛域名证书的命令方式

🍊问题:

我利用certbot申请的*.onedayxyy.cn泛域名证书,给子域名wiki.onedayxyy.cn都能正常使用,但是主域名onedayxyy.cn依然报不安全的证书,这个是为什么呢?

https:listen 443ssl;server_name onedayxyy.cn;ssl_certificate /etc/letsencrypt/live/onedayxyy.cn/fullchain.pem;ssl_certificate_key /etc/letsencrypt/live/onedayxyy.cn/privkey.pem;# 其他配置...}server{listen 443ssl;server_name wiki.onedayxyy.cn;ssl_certificate /etc/letsencrypt/live/onedayxyy.cn/fullchain.pem;ssl_certificate_key /etc/letsencrypt/live/onedayxyy.cn/privkey.pem;# 其他配置...}

  • 验证和重新加载 Nginx: 在修改配置文件后,使用以下命令测试配置文件的语法是否正确:

    如果测试通过,使用以下命令重新加载 Nginx 配置:

  • 通过以上步骤,你应该能够确保主域名 onedayxyy.cn和泛域名 *.onedayxyy.cn都能使用安全的证书。

    🍊自己重新再测试:

    之前生成的:

    image-20240803095424151

    先删除目录,再进行测试:

    image-20240803100103524

    再次把这2个证书进行替换,测试:

    image-20240803100317552

    重启nginx:

    nginx -s reload

    🍊验证:(完美)

    https:t="1711730357270" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4392" width="16" height="16" >打赏支持