使用acme.sh为Nextcloud申请Let’s Encrypt的ssl证书(非80端口)

1、安装acme.sh

root@guest:~# curl https://get.acme.sh | sh
.
.
.
[2023年 04月 18日 星期二 18:26:35 CST] Installing from online archive.
[2023年 04月 18日 星期二 18:26:35 CST] Downloading https://github.com/acmesh-official/acme.sh/archive/master.tar.gz
[2023年 04月 18日 星期二 18:26:37 CST] Extracting master.tar.gz
[2023年 04月 18日 星期二 18:26:39 CST] Installing to /root/.acme.sh
[2023年 04月 18日 星期二 18:26:39 CST] Installed to /root/.acme.sh/acme.sh
[2023年 04月 18日 星期二 18:26:40 CST] Installing alias to '/root/.bashrc'
[2023年 04月 18日 星期二 18:26:40 CST] OK, Close and reopen your terminal to start using acme.sh
[2023年 04月 18日 星期二 18:26:40 CST] Installing cron job
no crontab for root
no crontab for root
[2023年 04月 18日 星期二 18:26:40 CST] Good, bash is found, so change the shebang to use bash as preferred.
[2023年 04月 18日 星期二 18:26:47 CST] OK
[2023年 04月 18日 星期二 18:26:47 CST] Install success!

有的服务器执行上述命令报错:
在这里插入图片描述
为了安装acme.sh,可使用源码安装参考:
https://github.com/acmesh-official/acme.sh/wiki/Install-in-China
https://blog.csdn.net/weixin_57025326/article/details/136286399
https://github.com/acmesh-official/acme.sh/wiki/How-to-install#3-or-git-clone-and-install
而为了获取源码需要用到git命令,需要先使用以下命令安装git:
sudo apt install git

命令如下:
git clone --depth 1 https://github.com/acmesh-official/acme.sh.git
cd acme.sh
./acme.sh --install -m username@email.com
在这里插入图片描述
在这里插入图片描述
安装完会创建一个 cron 定时任务,可以使用 crontab -l 命令查看(通常分钟数是随机的)

root@guest:~# crontab -l
40 0 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null

上面这个表示在每天 0:40 执行一次。

注意安装后acme.sh默认位于/.acme.sh目录下,建议添加一条命令别名,方便后边执行:

root@guest:~# alias acme.sh=~/.acme.sh/acme.sh

开启acme.sh的自动升级:

root@guest:~# acme.sh --upgrade  --auto-upgrade

关闭自动更新:

root@guest:~# acme.sh --upgrade  --auto-upgrade  0

2、指定默认CA证书申请机构

root@guest:~# acme.sh --set-default-ca  --server  letsencrypt
[2023年 04月 18日 星期二 18:38:53 CST] Changed default CA to: https://acme-v02.api.letsencrypt.org/directory

3、生成证书

设置阿里云DNS的环境变量(替换为自己的阿里云账号key和密钥):

export Ali_Key="fsdfsgfddgdfghdfhg"
export Ali_Secret="hfhfhfafoeiridhfhdsifh"

执行以下命令生成证书:

root@guest:~# acme.sh --issue --dns dns_ali -d domain.com -d *.domain.com

[2023年 04月 19日 星期三 09:28:00 CST] Using CA: https://acme-v02.api.letsencrypt.org/directory
[2023年 04月 19日 星期三 09:28:01 CST] Multi domain='DNS:domain.com,DNS:*.domain.com'
[2023年 04月 19日 星期三 09:28:01 CST] Getting domain auth token for each domain
[2023年 04月 19日 星期三 09:28:08 CST] Getting webroot for domain='domain.com'
[2023年 04月 19日 星期三 09:28:09 CST] Getting webroot for domain='*.domain.com'
[2023年 04月 19日 星期三 09:28:09 CST] Adding txt value: R9JwXRZ-7feihofhsdoirefhsdoiRehos for domain:  _acme-challenge.domain.com
[2023年 04月 19日 星期三 09:28:22 CST] The txt record is added: Success.
[2023年 04月 19日 星期三 09:28:23 CST] Adding txt value: KSEdfhBScerhoifsdREwfohsidfhisFShlfhoisofhkk for domain:  _acme-challenge.domain.com
[2023年 04月 19日 星期三 09:28:37 CST] The txt record is added: Success.
[2023年 04月 19日 星期三 09:28:37 CST] Let's check each DNS record now. Sleep 20 seconds first.
[2023年 04月 19日 星期三 09:29:31 CST] You can use '--dnssleep' to disable public dns checks.
[2023年 04月 19日 星期三 09:29:31 CST] See: https://github.com/acmesh-official/acme.sh/wiki/dnscheck
[2023年 04月 19日 星期三 09:29:31 CST] Checking domain.com for _acme-challenge.domain.com
[2023年 04月 19日 星期三 09:29:32 CST] Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: 35
[2023年 04月 19日 星期三 09:29:41 CST] Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: 28
[2023年 04月 19日 星期三 09:29:41 CST] Domain domain.com '_acme-challenge.domain.com' success.
[2023年 04月 19日 星期三 09:29:41 CST] Checking domain.com for _acme-challenge.domain.com
[2023年 04月 19日 星期三 09:29:41 CST] Domain domain.com '_acme-challenge.domain.com' success.
[2023年 04月 19日 星期三 09:29:41 CST] All success, let's return
[2023年 04月 19日 星期三 09:29:41 CST] Verifying: domain.com
[2023年 04月 19日 星期三 09:29:43 CST] Pending, The CA is processing your order, please just wait. (1/30)
[2023年 04月 19日 星期三 09:29:48 CST] Pending, The CA is processing your order, please just wait. (2/30)
[2023年 04月 19日 星期三 09:29:52 CST] Pending, The CA is processing your order, please just wait. (3/30)
[2023年 04月 19日 星期三 09:29:57 CST] Success
[2023年 04月 19日 星期三 09:29:57 CST] Verifying: *.domain.com
[2023年 04月 19日 星期三 09:29:59 CST] Pending, The CA is processing your order, please just wait. (1/30)
[2023年 04月 19日 星期三 09:30:04 CST] Pending, The CA is processing your order, please just wait. (2/30)
[2023年 04月 19日 星期三 09:30:08 CST] Pending, The CA is processing your order, please just wait. (3/30)
[2023年 04月 19日 星期三 09:30:13 CST] Success
[2023年 04月 19日 星期三 09:30:13 CST] Removing DNS records.
[2023年 04月 19日 星期三 09:30:13 CST] Removing txt: R9JwXRZ-7feihofhsdoirefhsdoiRehos for domain: _acme-challenge.domain.com
[2023年 04月 19日 星期三 09:30:31 CST] Removed: Success
[2023年 04月 19日 星期三 09:30:53 CST] Removing txt: KSEdfhBScerhoifsdREwfohsidfhisFShlfhoisofhkk for domain: _acme-challenge.domain.com
[2023年 04月 19日 星期三 09:31:11 CST] Removed: Success
[2023年 04月 19日 星期三 09:31:16 CST] Verify finished, start to sign.
[2023年 04月 19日 星期三 09:31:16 CST] Lets finalize the order.
[2023年 04月 19日 星期三 09:31:16 CST] Le_OrderFinalize='https://acme-v02.api.letsencrypt.org/acme/finalize/1043575/14568245'
[2023年 04月 19日 星期三 09:31:18 CST] Downloading cert.
[2023年 04月 19日 星期三 09:31:18 CST] Le_LinkCert='https://acme-v02.api.letsencrypt.org/acme/cert/05sdhfissiffhsdifh'
[2023年 04月 19日 星期三 09:31:20 CST] Cert success.
-----BEGIN CERTIFICATE-----
MtewtfdfhsdiofhweihriwefhdkhsfdoifhsdfiruweiohfshkfhogklaknvnwUA
.
.
.
xpR4EfIdQjigJhd3fsfsdfsdgfhfghgfjyewterytujgdhdd2WXBbw066szc2uMh
4eeLUuJ853AEN2ZrFDwdL5ecrWsTVFadHN64BZN9MWk4ljYTJFkUQVBpguruS0Fy
wd17yjxoppThVWp7qTHaW8f4gfdfgdhgfgretrehysgdfsgdsfgdfgdfgrtapEwB
Z0Fd5I9Eshopt1Ng39J9lw==
-----END CERTIFICATE-----
[2023年 04月 19日 星期三 09:31:20 CST] Your cert is in: /root/.acme.sh/domain.com_ecc/domain.com.cer
[2023年 04月 19日 星期三 09:31:20 CST] Your cert key is in: /root/.acme.sh/domain.com_ecc/domain.com.key
[2023年 04月 19日 星期三 09:31:20 CST] The intermediate CA cert is in: /root/.acme.sh/domain.com_ecc/ca.cer
[2023年 04月 19日 星期三 09:31:20 CST] And the full chain certs is there: /root/.acme.sh/domain.com_ecc/fullchain.cer

注意:执行 acme.sh --issue --dns dns_ali -d domain.com -d *.domain.com 命令前,先关闭梯子,因为开着梯子就是国外的IP,访问国内的域名会失败,导致证书生成失败。

4、更新证书

证书在60天数后会自动更新,你无需任何操作。而且这个天数有可能会变,不过都是自动的,你不用关心。

也可手动更新证书:

root@guest:~# acme.sh --renew -d domain.com --force

如果是ECC证书,需要加--ecc参数

root@guest:~# acme.sh --renew -d domain.com --force --ecc

这些配置完后,就不用再管了。

提示:acme.sh 官方对申请证书的频率有限制,子域名好像每周最多4次。所以不要频繁乱申请,否则IP会被拉黑!

5、吊销证书

如果证书文件泄露了,使用以下命令吊销证书:

root@guest:~# acme.sh --revoke -d domain.com

如果是ECC证书,则需要加--ecc参数:

root@guest:~# acme.sh --revoke -d domain.com --ecc

6、安装部署证书

将安装的证书存放在 etc/ssl/domain.com 目录下,先创建目录:

root@guest:~# mkdir /etc/ssl/domain.com

然后,安装证书到该目录下(注意以下命令是一行命令,只是有换行):

root@guest:~# acme.sh --install-cert -d domain.com \
> --cert-file /etc/ssl/domain.com/domain.com.crt  \
> --key-file /etc/ssl/domain.com/domain.com.key  \
> --ca-file /etc/ssl/domain.com/ca.crt \
> --reloadcmd "service apache2 force-reload"

[2023年 04月 19日 星期三 10:54:27 CST] The domain 'domain.com' seems to have a ECC cert already, lets use ecc cert.
[2023年 04月 19日 星期三 10:54:28 CST] Installing cert to: /etc/ssl/domain.com/domain.com.crt
[2023年 04月 19日 星期三 10:54:28 CST] Installing CA to: /etc/ssl/domain.com/ca.crt
[2023年 04月 19日 星期三 10:54:28 CST] Installing key to: /etc/ssl/domain.com/domain.com.key
[2023年 04月 19日 星期三 10:54:28 CST] Run reload cmd: service apache2 force-reload
 * Reloading Apache httpd web server apache2                                  
 *
[2023年 04月 19日 星期三 10:54:29 CST] Reload success

请一定使用以上语法 acme.sh --install-cert 进行拷贝, 这样证书才能保证在新的位置也能自动更新。

部署证书,编辑apache配置文件:

root@guest:~# vim /etc/apache2/sites-available/default-ssl.conf

指定对应的证书文件,重点关注以下配置:

		SSLEngine on
		SSLPROTOCOL all -SSLv2 -SSLv3
		SSLCertificateFile      /etc/ssl/domain.com/domain.com.crt
		SSLCertificateKeyFile	/etc/ssl/domain.com/domain.com.key
		SSLCertificateChainFile  /etc/ssl/domain.com/ca.crt
		SSLOptions +StrictRequire

Esc 并输入 :wq 保存退出。

重启apache,这个时候不出意外https已经不会显示不安全的提示了。

root@guest:~# sudo service apache2 restart

7、手动更新证书

按照前述步骤设置之后,不知是何原因,自动更新证书存在问题,证书三个月后没有自动更新,可直接使用以下命令手动更新证书(之前的任何配置都不动):

acme.sh --renew -d domain.com --force --ecc
<think>好的,我现在需要帮助用户了解如何使用acme.sh来设置Let's Encrypt或其他不需要认证或注册的证书颁发机构。用户之前询问过如何将Nginx添加为服务,现在可能是在部署网站时需要配置SSL证书。 首先,用户的问题中提到“不需要认证或注册的颁发机构”,这有点矛盾,因为Let's Encrypt是需要验证域名的所有权的,但可能用户指的是不需要用户注册账户或提供额外信息的CA。Let's Encrypt确实不需要用户注册,只需要验证域名控制权。其他可能的CA如ZeroSSL可能需要邮箱注册,但acme.sh默认使用Let's Encrypt,也可能用户想用其他类似的CA。 接下来,我需要回顾acme.sh的基本用法,包括安装、签发证书的几种验证方式(HTTP、DNS等),以及自动续期配置。用户可能需要的是快速生成证书,不需要复杂步骤,所以重点应放在HTTP验证和DNS API集成上,尤其是自动化的部分。 需要注意的是,用户可能对ACME协议和不同的CA不太了解,所以需要解释清楚Let's Encrypt和ZeroSSL的区别,以及如何切换CA。同时,用户可能关心如何免交互或自动化,比如使用--register-account -m email参数自动注册,或者使用STAGE环境变量避免生产限速。 另外,用户可能遇到常见问题,比如证书签发失败的原因,权限问题,或者cron任务未正确设置导致续期失败。需要给出检查日志、验证配置和测试续期的建议。 最后,要确保步骤清晰,适合不同环境的用户,比如使用Nginx或Apache的,或者需要手动验证的情况。可能需要提醒用户关于路径配置和权限设置,尤其是acme.sh默认安装到用户目录下的问题,以及如何配置系统服务或cron任务。 总结步骤: 1. 安装acme.sh,解释不同安装方式。 2. 选择CA,默认是Let's Encrypt,切换到ZeroSSL或其他可能需要邮箱。 3. 签发证书的各种方法,强调DNS API的自动化。 4. 安装证书到指定位置,并配置web服务器。 5. 自动续期的配置和测试。 6. 常见问题处理和小贴士。 需要确保语言简洁,步骤明确,特别是命令行部分要正确无误。同时,要避免技术术语过多,用用户容易理解的方式解释,比如用示例说明DNS API的配置,或如何修改Nginx配置使用证书。</think>以下是使用 **acme.sh** 配置 **Let's Encrypt**(或其他 ACME 兼容证书颁发机构)自动签发 SSL 证书的详细指南,支持无需人工交互的自动化操作。 --- ### **1. 安装 acme.sh** #### **一键安装脚本**(推荐): ```bash curl https://get.acme.sh | sh -s email=your-email@example.com ``` - **说明**: - `email` 用于证书颁发机构(如 Let's Encrypt)的账户注册(可替换为任意邮箱)。 - 脚本会将 `acme.sh` 安装到 `~/.acme.sh/` 目录,并自动添加计划任务(cron job)处理续期。 --- ### **2. 选择证书颁发机构(CA)** acme.sh 默认使用 **Let's Encrypt**,但支持其他 ACME 兼容的 CA(如 ZeroSSL): ```bash # 切换为 ZeroSSL acme.sh --set-default-ca --server zerossl ``` #### **常用 CA 服务器地址**: | CA | 命令参数 | 是否需要注册 | |--|--|--| | Let's Encrypt(默认) | `--server letsencrypt` | 无需注册(自动生成账户) | | ZeroSSL | `--server zerossl` | 需要邮箱注册(首次需运行 `acme.sh --register-account -email@example.com`) | | Buypass | `--server buypass` | 需要邮箱注册 | -- ### **3. 签发证书的验证方式** #### **方式 1:HTTP 文件验证**(需开放 80 端口) ```bash acme.sh --issue -d example.com -d www.example.com --webroot /var/www/html ``` - **参数说明**: - `-d`: 指定域名(支持多个域名)。 - `--webroot`: 指定 Web 根目录,acme.sh 会在该目录下创建 `.well-known/acme-challenge/` 临时文件用于验证。 #### **方式 2:DNS API 验证**(无需开放端口,推荐自动化) 以 **Cloudflare** 为例: 1. **获取 Cloudflare API Token**: 登录 Cloudflare 控制台,生成一个具备 **Zone:Read** 和 **DNS:Edit** 权限的 API Token。 2. **配置环境变量**: ```bash export CF_Token="your-cloudflare-api-token" export CF_Account_ID="your-cloudflare-account-id" ``` 3. **签发证书**: ```bash acme.sh --issue --dns dns_cf -d *.example.com ``` - **其他 DNS 服务商**: acme.sh 支持 100+ 种 DNS 提供商的 API(如阿里云、GoDaddy、AWS Route53),具体参数参考 [官方文档](https://github.com/acmesh-official/acme.sh/wiki/dnsapi)。 --- ### **4. 安装证书到指定位置** 证书签发后默认存储在 `~/.acme.sh/example.com/`,需手动复制到 Web 服务器目录: ```bash acme.sh --install-cert -d example.com \ --key-file /etc/nginx/ssl/example.com.key \ --fullchain-file /etc/nginx/ssl/example.com.crt \ --reloadcmd "systemctl reload nginx" ``` - **参数说明**: - `--reloadcmd`: 证书续期后自动执行的命令(如重启 Nginx/Apache)。 - 证书文件会自动更新,续期过程无需人工干预。 --- ### **5. 自动续期配置** acme.sh 安装时已自动添加 **cron 任务**,每日检查证书有效期(到期前 30 天自动续期)。 - **手动测试续期**: ```bash acme.sh --renew -d example.com --force ``` --- ### **6. 其他实用命令** | 命令 | 说明 | |--| | `acme.sh --list` | 查看已签发的证书 | | `acme.sh --revoke -d example.com` | 吊销证书 | | `acme.sh --remove -d example.com` | 删除证书 | | `acme.sh --upgrade` | 升级 acme.sh | --- ### **7. 常见问题** #### **问题 1:证书签发失败** - **原因**: - DNS 解析未生效或 API Token 权限不足。 - HTTP 验证时 80 端口被占用或无法访问。 - **解决**: ```bash # 查看详细日志 acme.sh --issue -d example.com --debug ``` #### **问题 2:cron 任务未执行** - **检查 cron 日志**: ```bash grep acme.sh /var/log/cron ``` --- ### **8. 高级配置** #### **使用 Let's Encrypt 测试环境**(避免生产限速): ```bash acme.sh --staging --issue -d example.com --webroot /var/www/html ``` #### **自定义证书有效期**(仅部分 CA 支持): ```bash acme.sh --issue -d example.com --days 90 ``` --- 通过以上步骤,你可以快速为域名签发免费的 SSL 证书,并通过自动化脚本实现长期维护。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值