安裝certbot
mkdir -p /data/certbot #創建certbot工作目錄
cd /data/certbot
python -m venv venv #在certbot目錄創建python工作環境
source venv/bin/activate #執行python工作環境下的activate命令,source只是Linux執行命令的一種方式,和直接用 ./venv/bin/activate執行時一樣的
用source執行腳本文件,執行過程不另開進程,腳本文件中設定的變量在當前shell中可以看到;
用sh執行腳本文件,是在當前進程另開子進程來執行腳本命令,腳本文件中設定的變量在當前shell中不能看到。
pip install certbot certbot-nginx certbot-dns-aliyun #在當前python工作環境中安裝 certbot和阿里雲dns插件
ln -sf /data/certbot/venv/bin/certbot /usr/bin/certbot #創建軟鏈,以便可以直接執行certbot
如果執行certbot 提示: ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with OpenSSL 1.0.2k-fips 26 Jan 2017. See: https://github.com/urllib3/urllib3/issues/2168
說明urllib3版本太高,可以降低urllib3版本:
pip uninstall urllib3
pip install 'urllib3<2.0'
然后重新安裝
pip uninstall certbot certbot-nginx certbot-dns-aliyun
pip install certbot certbot-nginx certbot-dns-aliyun
為nginx添加證書並自動修改nginx配置
certbot --nginx
如果Nginx配置文件不在/etc/nginx/nginx.conf目錄,則需要指定目錄
certbot --nginx --nginx-server-root=/usr/local/nginx/conf (等號右邊填寫nginx配置文件的目錄)
為nginx添加證書
certbot certonly --nginx
測試證書更新
certbot renew --dry-run
證書更新
certbot renew
阿里雲泛域名證書
Certbot 支持自動申請 LetsEncrypt 的泛域名證書,但是官方插件不支持阿里雲,在 GitHub 搜索發現已經有人寫好了阿里雲 DNS 插件,下面只需要進行簡單的配置即可免費申請一個泛域名證書並自動續訂。
申請並配置阿里雲 DNS 訪問密鑰
前往 https://ram.console.aliyun.com 申請阿里雲子賬號並授予 AliyunDNSFullAccess 權限。然后為子賬號創建 AccessKey 並記錄。
創建 certbot-dns-aliyun 配置文件:
cat > /data/certbot/credentials.ini <<EOF
certbot_dns_aliyun:dns_aliyun_access_key = 12345678
certbot_dns_aliyun:dns_aliyun_access_key_secret = 1234567890abcdef1234567890abcdef
EOF
修改文件權限
chmod 600 /data/certbot/credentials.ini
創建證書
/data/certbot/venv/bin/certbot \
-a certbot-dns-aliyun:dns-aliyun \
--certbot-dns-aliyun:dns-aliyun-credentials \
/data/certbot/credentials.ini \
-d xxx.com \
-d "*.xxx.com" \
--nginx-server-root=/usr/local/nginx/conf
如果提示:certbot: error: unrecognized arguments: --certbot-dns-aliyun:dns-aliyun-credentials /data/certbot/credentials.ini
cat > /data/certbot/credentials.ini <<EOF
dns_aliyun_access_key = 12345678
dns_aliyun_access_key_secret = 1234567890abcdef1234567890abcdef
EOF
/data/certbot/venv/bin/certbot \
-a dns-aliyun \
--dns-aliyun-credentials \
/data/certbot/credentials.ini \
-d xxx.com \
-d "*.xxx.com" \
--nginx-server-root=/usr/local/nginx/conf
配置crontab自動更新
43 0 * * * certbot renew -q > /dev/null
非阿里雲,直接使用certbot-nginx
mkdir -p /data/certbot #創建certbot工作目錄
cd /data/certbot
python -m venv venv #在certbot目錄創建python工作環境
source venv/bin/activate #執行python工作環境下的activate命令,source只是Linux執行命令的一種方式,和直接用 ./venv/bin/activate執行時一樣的
pip install certbot certbot-nginx
ln -sf /data/certbot/venv/bin/certbot /usr/bin/certbot #創建軟鏈,以便可以直接執行certbot
申請和安裝證書
certbot --nginx --nginx-server-root=/usr/local/nginx/conf
配置crontab自動更新
43 0 * * * certbot renew -q > /dev/null