首次申請
下載安裝 CertBot
# 下載 CertBot 腳本到當前目錄,假設當前文件夾為 ~ 目錄
wget https://dl.eff.org/certbot-auto
# 為 CertBot 腳本增加執行權限,# a 為 all 簡寫,x 為 execute 簡寫,a+x 表示所有用戶及群組的可執行權限
chmod a+x ./certbot-auto
配置 pip 國內源
注:若之前已配置,請跳過此步驟;此步驟的目的是加速 CertBot 下載 python 模塊的速度
# 新建 .pip 文件夾并進入
mkdir .pip && cd .pip
# 創建 pip.conf 文件
vi pip.conf
# 在 pip.conf 文件中輸入以下內容
[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
# 保存退出
運行腳本,安裝依賴
./certbot-auto --help
配置 nginx
目的:獲取域名證書過程中, Let's Encrypt 會對域名發起訪問,以確認申請者對域名的所有權;故需要配置 nginx,以便能夠對 Let's Encrypt 的訪問返回正確的響應;
# 創建文件夾,用于 Let's Encrypt 訪問時返回響應內容
mkdir /home/letsencrypt
# 打開 nginx 配置文件進行編輯,此處假設 nginx 的配置文件在以下路徑:/usr/local/nginx/conf/nginx.conf,如不是,則相應修改路徑
vi /usr/local/nginx/conf/nginx.conf
// 在 nginx 配置文件中,找到 http 下監聽 80 端口的 server
http {
//...(略)...
server {
listen 80;
// 添加如下內容,此處假設申請域名為 www.helloworld.com,請修改為實際申請的域名
server_name www.helloworld.com;
location ^~ /.well-known/acme-challenge/ {
defaulf_type "text/plain";
root /home/letsencrypt/;
}
// ......以下略......
重啟 nginx
# 此處假設 nginx 可執行文件在路徑 /usr/local/nginx/sbin 下面,如不是則相應修改路徑
# 先使用 -t 參數測試配置文件格式是否正確
/usr/local/nginx/sbin/nginx -t
# 若正確,屏幕上將顯示以下字樣
> nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
# 重啟 Nginx
/usr/local/nginx/sbin/nginx -s reload
運行腳本,申請證書
在申請證書前,記得先將域名的 DNS 解析指向當前的服務器 IP,這樣 letsencrypt 機構在向域名發起連接請求的時候,才能路由到當前設置的機器
# 此處為域名 www.helloworld.com 申請一張證書,其中的 youremail.com 請替換為你自己的郵箱地址
./certbot-auto certonly --email youremail.com --webroot -w /home/letsencrypt -d www.helloworld.com
# 如果要為多個子域名(如 api/test/www) 申請一張證書,則相應修改命令如下
./certbot-auto certonly --email youremail.com --webroot -w /home/letsencrypt -d api.helloworld.com -d test.helloworld.com -d www.helloworld.com
申請成功后,界面下會有如下的成功提示:
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/helloworld.com/fullchain.pem. Your cert
will expire on 2019-08-26. To obtain a new version of the
certificate in the future, simply run Let's Encrypt again......
配置 nginx,啟用證書
// 在 nginx 配置文件中,找到 http 下監聽 443 端口的 server
http {
//...(略)...
server {
listen 443 ssl;
// 修改 server_name、ssl_certificate、ssl_certificate_key 三個字段的值
// 此處假設申請域名為 www.helloworld.com,請修改為實際申請的域名
server_name www.helloworld.com;
ssl_certificate /etc/letsencrypt/live/www.helloworld.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.helloworld.com/privkey.pem;
// ......以下略......
當用戶訪問非加密的 80 端口時,如果需要讓服務器自動跳轉到 443 端口使用證書的 https 訪問,則可以在 http 下 80 端口的 server 中增加如下內容:
// 在 nginx 配置文件中,找到 http 下監聽 80 端口的 server
http {
//...(略)...
server {
listen 80;
server_name www.helloworld.com;
// 添加如下內容,實現自動跳轉
return 301 https://$server_name$request_uri
// ......以下略......
重啟 Nginx,讓配置生效
# 此處假設 nginx 可執行文件在路徑 /usr/local/nginx/sbin 下面,如不是則相應修改路徑
/usr/local/nginx/sbin/nginx -s reload
測試自動更新
# 使用 --dry-run 選項表示測試,非真正執行更新
./certbot-auto renew --dry-run
若顯示如下字樣,則表示自動更新功能測試成功
Congratulations, all renewals succeeded. The following certs have been renewed:
/etc/letsencrypt/live/www.helloworld.com/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates above have not been saved.)
到期更新
由于 Let's Encrypt 頒發的證書只有 90 天有效期,因此需要定期進行證書更新
# 手動更新
./certbot-auto renew -v