1. 程式人生 > >Let's Encrypt 安裝配置教程,免費的 SSL 證書

Let's Encrypt 安裝配置教程,免費的 SSL 證書

官網:https://letsencrypt.org/

安裝Let’s Encrypt

安裝非常簡單直接克隆就可以了

git clone https://github.com/letsencrypt/letsencrypt

生成萬用字元證書

期間需要根據提示設定DNS TXT記錄,用作你對判斷你是否擁有域名使用權

 cd letsencrypt
./certbot-auto certonly  -d *.you.cn --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory

其中"you.cn"換成你的一級域名即可

引數 說明
certonly 表示安裝模式,Certbot 有安裝模式和驗證模式兩種型別的外掛。
–manual 表示手動安裝外掛,Certbot 有很多外掛,不同的外掛都可以申請證書,使用者可以根據需要自行選擇
-d 為那些主機申請證書,如果是萬用字元,輸入 *.you.cn(可以替換為你自己的一級域名)
–preferred-challenges dns 使用 DNS 方式校驗域名所有權
–server Let’s Encrypt ACME v2 版本使用的伺服器不同於 v1 版本,需要顯示指定。

接著可以看到下面介面結果:

Dependency Installed:
  dwz.x86_64 0:0.11-3.el7             perl-srpm-macros.noarch 0:1-8.el7             tcl.x86_64 1:8.5.13-8.el7             tix.x86_64 1:8.4.3-12.el7             tk.x86_64 1:8.5.13-6.el7             tkinter.x86_64 0:2.7.5-69.el7_5            

Complete!
Creating virtual environment...
Installing Python packages...
Installation succeeded.
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): 
[email protected]

接下來需要輸入些指令

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: N
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for kuaichuangkeji.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.

Are you OK with your IP being logged?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

這裡有幾個需互動的提示

  • 是否同意 Let’s Encrypt 協議要求=>需要同意
  • 是否分享你的郵箱
  • 詢問是否對域名和機器(IP)進行繫結=>需要同意

需要注意的地方:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.you.cn with the following value:

RYtObhDvEcXewZckknNQkBKIkvwIlbb4PNRel74LNwU

Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue
Waiting for verification...
Cleaning up challenges

要求配置 DNS TXT 記錄,從而校驗域名所有權,也就是判斷證書申請者是否有域名的所有權。

上面輸出要求給 _acme-challenge.you.cn 配置一條 TXT 記錄,在沒有確認 TXT 記錄生效之前不要回車執行。

我用的是阿里雲的域名伺服器,控制檯具體操作如下圖所示:

伺服器域名dns配置
確認生效後,回車執行,輸出如下

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/you.cn/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/you.cn/privkey.pem
   Your cert will expire on 2019-02-27. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again. To non-interactively renew *all* of your certificates, run
   "certbot-auto renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

看到這個介面內容的話,恭喜你,證書安裝成功了。

證書續簽

注:證書在到期前30天才會續簽成功,但為了確保證書在執行過程中不過期,官方建議每天自動執行續簽兩次;
使用crontab自動續期

crontab -e // 編輯定時任務
0 */12 * * * certbot renew --quiet --renew-hook "/etc/init.d/nginx reload"
  • 需要注意"/etc/init.d/nginx reload"為nginx重啟命令,需要根據自己伺服器的重啟命令重啟即可;這裡建議使用reload,不推薦使用restart,因為這樣萬一配錯了,也不會影響伺服器其他專案的執行

證書儲存的路徑[配置nginx需要用到的]

/etc/letsencrypt/live/you.cn/fullchain.pem
/etc/letsencrypt/live/you.cn/privkey.pem

nginx 開啟 https

證書生成完成後可以到 /etc/letsencrypt/live/ 目錄下檢視對應域名的證書檔案。編輯 nginx 配置檔案監聽 443 埠,啟用 SSL,並配置 SSL 的公鑰、私鑰證書路徑:

server {
   listen   443 ssl;
   server_name  you.cn;
   root /home/www/you;
   index  index.html index.htm index.php;

    ssl on;
    ssl_certificate /etc/letsencrypt/live/you.cn/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/you.cn/privkey.pem;
    ...
}

新增 HTTP 自動跳轉到 HTTPS:

server {
    listen 80;
	server_name you.cn;
	location / {
		rewrite ^(.*)$  https://$host$1 permanent;
	}
}
配置好nginx後,重啟nginx
證書安裝成功後,該域名下的所有二級域名都可以使用噢

取消證書

可以使用一下命令取消剛剛生成的密匙,也就是以上的反操作:

certbot revoke --cert-path /etc/letsencrypt/live/you.cn/cert.pem
certbot delete --cert-name you.cn
參考的網址有