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

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

使用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!

安装完会创建一个 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 onSSLPROTOCOL all -SSLv2 -SSLv3SSLCertificateFile      /etc/ssl/domain.com/domain.com.crtSSLCertificateKeyFile	/etc/ssl/domain.com/domain.com.keySSLCertificateChainFile  /etc/ssl/domain.com/ca.crtSSLOptions +StrictRequire

Esc 并输入 :wq 保存退出。

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

root@guest:~# sudo service apache2 restart