原文地址: https://www.jianshu.com/p/3ffd27b64847
1. HTTPS基本介紹
現在各大廠商都在推行HTTPS,比如谷歌要求多個頂級域名要用HTTPS來加密,蘋果要求開發者全部采用HTTPS等等。那什么是HTTPS呢?其實HTTPS只是HTTP的一個拓展,是在HTTP的基礎上利用SSL/TLS來加密數據包的。工作流程如下:

圖片來自IBM Knowledge Center: An overview of the SSL or TLS handshake
注意第(2)步Server給Client發送了一個Server certificates,這個里面包含有Server的一些信息,如域名、公司信息、序列號和簽名信息組成等,這個證書可以個人生成,也可以由權威機構簽發,當然個人的就不受大眾信任,而權威機構簽發的證書則會被信任。
具體的可以參考:細說 CA 和證書
2. Let’s Encrypt
CA的證書提供商有許多個,有收費的有免費的,而Let’s Encrypt就是其中之一的免費提供商。
2.1 如何獲取Let's Encript的證書呢?
要從Let's Encript獲取某個域名的證書,需要證明那你對該域名擁有控制權,對於該證明你可以使用某個使用ACME協議的軟件來實現,而Certbot就是官方出的一個ACME客戶端。
3. Certbot介紹
先介紹一些Certbot相關概念。
3.1 Authenticators和Installers
Certbot支持兩種類型的plugin,一種是用來獲取證書的,稱為Authenticators;另外一種是用來安裝證書的,稱為Installers。有的plugin支持一種,有的兩種都支持,如nginx。
安裝證書:自動修改配置文件,如修改nginx的某個
.conf
文件
Authenticators plugin使用certonly
命令來獲取證書,而Installers plugin使用install
命令來安裝證書。
3.2 plugin說明
下面列舉幾個常用的plugin作簡要說明:
- Webroot:本地有運行webserver並且有能力修改其配置,就可以用該種方式(創建隱藏文件
.well-known
),獲取證書時無需暫停webserver的運行。 - Standalone:服務器未運行webserver可以使用該方式,要保持80或443端口開放。
- Nginx:自動獲取和安裝證書(自動修改配置文件)。
3.3 Certbot使用流程
Certbot的使用包含以下幾個部分:
- 安裝Certbot
- 生成證書
- 配置Web Server
- 更新證書
3.3.1 Certbot安裝
安裝Certbot參考:Certbot,直接選擇軟件和操作系統即可。
3.3.2 獲取證書
使用certbot certonly
命令然后根據提示操作即可。
[root@efd140d6210b /]# certbot certonly Saving debug log to /var/log/letsencrypt/letsencrypt.log How would you like to authenticate with the ACME CA? ------------------------------------------------------------------------------- 1: Spin up a temporary webserver (standalone) 2: Place files in webroot directory (webroot) ------------------------------------------------------------------------------- Select the appropriate number [1-2] then [enter] (press 'c' to cancel):
對於nginx可以使用
certbot --nginx
來獲取和安裝證書。
獲取完之后可以通過certbot certificates
命令查看證書:
root@node01:~# certbot certificates Saving debug log to /var/log/letsencrypt/letsencrypt.log ------------------------------------------------------------------------------- Found the following certs: Certificate Name: www.youdomain.com Domains: www.youdomain.com Expiry Date: 2018-09-03 02:08:54+00:00 (VALID: 89 days) Certificate Path: /etc/letsencrypt/live/www.youdomain.com/fullchain.pem Private Key Path: /etc/letsencrypt/live/www.youdomain.com/privkey.pem -------------------------------------------------------------------------------
3.3.3 配置Web Server
不同Web Server的配置方式不同,這里以Nginx為例,在配置文件youdomain.conf
中添加:
server { listen [::]:80; root /var/www/youdomain; index index.html index.htm; server_name www.yourdomain.com; charset utf-8; #................. listen 443 ssl; ssl_certificate /etc/letsencrypt/live/www.yourdomain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/www.yourdomain.com/privkey.pem; }
需要了解Nginx的使用
配置完之后更新配置即可(nginx -s reload
),到這里證書配置就完成了,正常情況下該域名HTTPS就可以訪問了。
3.3.4 更新證書
由於Let’s Encrypt頒發的證書的有效期只有90天,這就需要更新證書。
certbot renew
如果使用了nginx plugin,則更新時需要使用
certbot renew --quiet --installer node
,否則會自動安裝證書導致錯誤。
3.4 Certbot常用命令
// perform a test run certbot certonly --dry-run certbot renew --dry-run // 顯示證書信息 certbot certificates // 撤銷證書 certbot revoke --cert-path /etc/letsencrypt/live/CERTNAME/cert.pem // 刪除證書(撤銷之后使用) certbot delete --cert-name example.com
如果證書是測試的如
--staging
,撤銷時也要加上。
3.5 Certbort其他功能
3.5.1 泛域名支持
可以使用DNS Plugins來實現。
Reference
作者:keith666
鏈接:https://www.jianshu.com/p/3ffd27b64847
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。