banner
Lumos

Hi,Lumos

念念不忘,终有回响

ACME證書申請

Wiki#

安裝#

yum update ca-certificates # centos
apt install ca-certificates # debian

curl https://get.acme.sh | sh -s [email protected]
wget -O -  https://get.acme.sh | sh -s [email protected]

# 國內使用
# https://github.com/acmesh-official/acme.sh/wiki/Install-in-China
git clone https://gitee.com/neilpang/acme.sh.git
cd acme.sh
./acme.sh --install -m [email protected]

source ~/.bashrc
source ~/.zshrc

開啟自動更新

acme.sh --upgrade --auto-upgrade
acme.sh --upgrade --auto-upgrade  0 # 關閉自動更新

常見命令#

acme.sh --help # 幫助
acme.sh --list  # 列出證書
acme.sh --upgrade # 檢查更新
acme.sh --issue # 申請證書
acme.sh --renew # 更新證書
acme.sh --revoke # 吊銷證書
acme.sh --remove # 刪除證書
acme.sh --install-cert # 安裝證書

切換 CA#

# 切換為letsencrypt
acme.sh --set-default-ca --server letsencrypt
# 切換為zerossl
acme.sh --set-default-ca --server zerossl

apt-get install socat

image-20241203010111740

伺服器・acmesh-official/acme.sh Wiki・GitHub

證書申請#

sh acme.sh  --issue -d renalio.eu.org  --standalone
acme.sh --issue -d rsync.157077.xyz  --standalone

HTTP 認證#

使用獨立服務模式#

如果伺服器上沒有運行任何 Web 服務,80 端口是空閒的,那麼 acme.sh 還能假裝自己是一個 WebServer,臨時監聽 80 端口,完成驗證:

acme.sh --issue --standalone -d example.com -d www.example.com -d cp.example.com

直接簽發 (HTTP-01 驗證)#

需要你的伺服器上面已經部署了網站環境。(被申請的域名可以正常被打開)
Acme 自動在你的网站根目錄下放置一個文件,(這個文件可以被互聯網訪問)來驗證你的域名所有權,完成驗證。然後就可以生成證書了.

只需要指定域名,並指定域名所在的網站根目錄. acme.sh 會全自動的生成驗證文件,並放到網站的根目錄,驗證完成後會聰明的刪除驗證文件,整個過程沒有任何副作用。

acme.sh --issue -d mydomain.com -d www.mydomain.com --webroot /home/wwwroot/mydomain.com/

TLS-ALPN-01 驗證#

通過在伺服器上使用自定義的 TLS 握手來驗證域名的所有權。這種方式適用於沒有 Web 伺服器的情況。

如果您沒有網絡伺服器,可能您正在 smtp 或 ftp 伺服器上, 443 端口是空閒的。您可以使用獨立的 TLS ALPN 模式。acme.sh 內置了獨立的 TLS 網絡伺服器,它可以監聽 443 端口以頒發證書。

acme.sh --issue -d yourdomain.com --alpn
acme.sh  --issue  -d example.com  --alpn --tlsport 8443

使用 Nginx 模式#

如果你用的 Nginx 伺服器,或者反代,acme.sh 還可以智能的從 Nginx 的配置中自動完成驗證,你不需要指定網站根目錄:

acme.sh --issue --nginx -d example.com -d www.example.com -d cp.example.com

使用 Apache 模式#

如果你用的 Apache 伺服器,acme.sh 還可以智能的從 Apache 的配置中自動完成驗證,你不需要指定網站根目錄:

acme.sh --issue --apache -d example.com -d www.example.com -d cp.example.com

注意,無論是 Apache 還是 Nginx 模式,acme.sh 在完成驗證之後,會恢復到之前的狀態,都不會私自更改程序本身的配置。好處是你不用擔心配置被搞壞,也有一個缺點,你需要自己配置 SSL 項,否則只能成功生成證書,你的網站還是無法正常使用 HTTPS。

DNS 認證#

如果你沒有伺服器,沒有公網 IP,只需要 DNS 的解析記錄即可完成驗證。

手動驗證#

這需要你手動在域名上添加一條 TXT 解析記錄,驗證域名所有權。
注意,如果使用手動驗證,acme.sh 將無法自動更新證書,每次都需要手動添加解析來驗證域名所有權。如果有自動更新證書的需求,請使用自動驗證(DNS API)。

acme.sh --issue --dns -d example.com -d www.example.com -d cp.example.com

然後,acme.sh 會生成相應的解析記錄顯示出來,你只需要在你的域名管理面板中添加這條 TXT 記錄即可。
等待解析完成之後,執行以下命令重新生成證書:

acme.sh --renew -d mydomain.com

注意這裡現在用的是 --renew 參數

自動驗證 (DNS API)(DNS-01 驗證)#

DNS 方式的真正強大之處在於可以使用域名解析商提供的 API 自動添加 TXT 記錄,且在完成驗證後刪除對應的記錄。
acme.sh 目前支持超過一百家的 DNS API。
以 DNSPod.cn 為例,你需要先登錄到 DNSPod.cn,拿到你的 DNSPod API Key 和 ID 並設置:

export DP_Id="1234"
export DP_Key="sADDsdasdgdsf"

現在我們可以簽發通配符證書了:

acme.sh --issue --dns dns_dp -d example.com -d *.example.com

DP_IdDP_Key 將保存在 ~/.acme.sh/account.conf 中,並在需要時自動獲取,無需手動再設置。
更詳細的 DNS API 用法: https://github.com/acmesh-official/acme.sh/wiki/dnsapi

支持的 DNS API 包括但不限於:

  • dns_cf:Cloudflare
  • dns_dp:DNSPod
  • dns_cx:CloudXNS
  • dns_ali:阿里雲 DNS
  • dns_aws:AWS Route 53
  • dns_gd:GoDaddy
  • dns_linode:Linode
  • dns_nsupdate:BIND DNS
  • dns_ovh:OVH
  • dns_pdns:PowerDNS
  • dns_gcore:G-Core Labs

Cloudflare 示例#

請稍候…
dnsapi · acmesh-official/acme.sh Wiki · GitHub

使用限制性令牌#

多個區域 DNS
image.png

export CF_Account_ID="763eac4f1bcebd8b5c95e9fc50d010b4"
export CF_Token="Y_jpG9AnfQmuX5Ss9M_qaNab6SQwme3HWXNDzRWs"
使用全局 API 密鑰#

image.png

export CF_Key="763eac4f1bcebd8b5c95e9fc50d010b4"
export CF_Email="[email protected]"

查看已安裝證書信息#

acme.sh --info -d example.com

安裝證書#

證書生成好以後,我們需要把證書複製給對應的 Apache、Nginx 或其他伺服器去使用。
必須使用 --install-cert 命令來把證書複製到目標文件,請勿直接使用 ~/.acme.sh/ 目錄下的證書文件,這裡面的文件都是內部使用,而且目錄結構將來可能會變化。

Nginx 示例#

acme.sh --install-cert -d example.com \
	--key-file       /path/to/keyfile/in/nginx/key.pem  \
	--fullchain-file /path/to/fullchain/nginx/cert.pem \
	--reloadcmd     "service nginx reload"

對應的參數:

  • --key-file :私鑰文件安裝地址
  • --cert-file :證書文件安裝地址
  • --fullchain-file :證書鏈文件安裝地址
  • --reloadcmd :重啟命令內容
server {
    listen 80;
    listen [::]:80;
    server_name www.exmaple.com;
    rewrite ^(.*)$ https://$host$1 permanent;
}

server {
    charset utf-8;
    listen       443 ssl;
    listen  [::]:443;
    server_name  www.exmaple.com;

    ssl_certificate    /etc/nginx/ssl/www.exmaple.com/fullchain.pem;
    ssl_certificate_key   /etc/nginx/ssl/www.exmaple.com/key.pem;

    location / {
        # rewrite ^/api-server/(.*)$ /$1 break;
        proxy_pass http://172.24.64.224:19999/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    # location / {
    #     root   /usr/share/nginx/html;
    #     index  index.html index.htm;
    # }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

Apache 示例#

acme.sh --install-cert -d example.com \
	--cert-file      /path/to/certfile/in/apache/cert.pem  \
	--key-file       /path/to/keyfile/in/apache/key.pem  \
	--fullchain-file /path/to/fullchain/certfile/apache/fullchain.pem \
	--reloadcmd     "service apache2 force-reload"

更新證書#

目前證書每 60 天自動更新,你無需任何操作。
但是你也可以強制續簽證書:

acme.sh --renew -d example.com --force

吊銷證書#

acme.sh --revoke -d dnomd343.top

刪除證書#

acme.sh --remove -d dnomd343.top

連結🔗#

FreeSSL#

FreeSSL.cn - 一個提供免費 HTTPS 證書申請的網站

專案地址#

GitHub - acmesh-official/acme.sh: A pure Unix shell script implementing ACME client protocol

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。