1. 程式人生 > 實用技巧 >acme 快速生成證書且部署nginx (檔案驗證)

acme 快速生成證書且部署nginx (檔案驗證)

acme 有兩種方式驗證域名釋出ssl證書,dns和檔案校驗

本文使用比較方便的檔案驗證方式,需要nginx 配置了servername 或者其他方式,埠需要開放全球80,會在預設網頁路徑生成txt檔案,校驗網址的url,例http://baidu.com/ssl/qwertyy

更多資訊可以參考
https://github.com/acmesh-official/acme.sh
https://github.com/acmesh-official/acme.sh/wiki/說明

三種方式安裝 acme指令碼

git clone https://github.com/acmesh-official/acme.sh.git
cd ./acme.sh
./acme.sh --install
# 或者
curl https://get.acme.sh | sh
# 或者
wget -O -  https://get.acme.sh | sh

nginx conf模板

server {
        listen  80;
        server_name    baidu.com;
        root /usr/local/nginx/html;
        location / {
                return 404;
        }
        location ^~ /.well-known/acme-challenge/ {
                default_type "text/plain";

        }

        location = /.well-known/acme-challenge/ {
                return 404;
        }

快捷生成指令碼和nginx配置檔案的指令碼,
需要先配置好demo.com.conf.b 模板,根據實際需求來配置

#!/bin/bash
# 指令碼可命名為 update_ssl.sh
if [ -z "$1" ];then
            exit
fi
# acme.sh 可能需要配置絕對路徑
acme.sh --issue -d $1 -d www.$1 -w /usr/local/nginx/html
sed "s/demo.com/$1/g" /usr/local/nginx/conf/conf.d/demo.com.conf.b > /usr/local/nginx/conf/conf.d/$1.conf
/usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx -s reload

如果你有大量域名需要配置,可以寫for迴圈 去生成配置

for i in `cat domain.txt`
do
update_ssl.sh $i
done