概述
HTTP 是一個網絡協議,大部分網站都是通過該協議來傳輸 WEB 頁面的,內容包括圖片、CSS 樣式、JS 腳本等。但 HTTP 協議是明文的,傳輸的內容容易被偷窺和篡改。到了上世紀 90 年代,由網景公司發明了 SSL 協議,“Secure Sockets Layer” 的縮寫,中文叫做“安全套接層”。到了 1999 年,SSL 因爲應用廣泛,已經成爲互聯網上的事實標準。IETF 就在那年把 SSL 標準化。標準化之後的名稱改爲 TLS(是“Transport Layer Security”的縮寫),中文叫做“傳輸層安全協議”。
HTTP 與 HTTPS
很多相關的文章都把這兩者並列稱呼(SSL/TLS),因爲這兩者可以視作同一個東西的不同階段。
HTTP 協議和“SSL/TLS”協議的組合,可以把 HTTPS 大致理解爲——“HTTP over SSL”或“HTTP over TLS”。
超文本傳輸協議 HTTP 協議被用於在 Web 瀏覽器和網站服務器之間傳遞信息。HTTP 協議以明文方式發送內容,不提供任何方式的數據加密,如果攻擊者截取了 Web 瀏覽器和網站服務器之間的傳輸報文,就可以直接讀懂其中的信息,因此 HTTP 協議不適合傳輸一些敏感信息,比如信用卡號、密碼等。
爲了解決 HTTP 協議的這一缺陷,需要使用另一種協議:安全套接字層超文本傳輸協議 HTTPS。爲了數據傳輸的安全,HTTPS 在 HTTP 的基礎上加入了 SSL 協議, SSL 依靠證書來驗證服務器的身份,併爲瀏覽器和服務器之間的通信加密。
HTTPS 協議需要到 ca 申請證書,一般免費證書很少,需要交費。證書獲取途徑:
通過 open ssl 生成自定義的簽名證書,此證書並未獲得全球信任機構(CA)的認可和記錄;
向全球信任機構證書品牌廠商(letsencrypt)申請免費證書;
向全球信任機構證書品牌廠商(symantec、geotrust、trustasia)購買證書;
下面介紹如果通過 letsencrypt 獲取免費的 ssl 證書。
原理
letsencrypt 提供了 2 種認證方式,HTTP 和 DNS。DNS 方式解決了內網不能被 HTTP 訪問的問題。
詳見: https://letsencrypt.org/how-it-works/
HTTP
大概的意思是,在服務器上創建一個路徑 /.well-known/challenge/,讓 letsencrypt 能下載其中的內容。以 www.example.com 爲例,就是 letsencrypt 通過執行 http://www.example.com/.well-known/challenge/ 能下載生成在 challenge 文件下的隨機字符串,以此來驗證服務器。
DNS
如果是內網需要獲取 SSL 證書,HTTP 方式就不行,因爲 HTTP 訪問不了內網的服務器,此時可以通過 DNS 方式申請,此時需要從 letsencrypt上獲取一個隨機字符,然後在DNS上添加一條 txt 的記錄,以此驗證你對域名的所有權。
獲取證書
letsencrypt 爲了方便用戶獲取證書,已開發了一個工具。
安裝 certbot 工具:
$ git clone https://github.com/certbot/certbot.git
$ cd certbot
$ chmod a+x certbot-auto
certbot 獲取證書有多種模式,可參考: https://certbot.eff.org/docs/using.html#third-party-plugins
HTTP 方式
創建及配置訪問路徑
以 webroot 模式爲例。
創建 /.well-known/challenge/ 訪問路徑:
$ mkdir -p /var/www/www.example.com/
由於更新證書也需要用到這個路徑,建議將該路徑單獨存放。
修改配置文件,添加如下配置:
location /.well-known/acme-challenge/{
root /var/www/www.example.com;
}
執行命令,生成證書:
$ ./certbot-auto certonly --webroot -w /var/www/www.example.com -d www.example.com -d example.com
如果有多個地址,可以多用幾次 -w 和 -d。
如果不出意外的話,可以看到:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for www.sm.zhihuixupu.com
Using the webroot path /var/www/www.example.com for all unmatched domains.
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/www.example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/www.example.com/privkey.pem
Your cert will expire on 2018-04-11\. 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
上面顯示證書生成的路徑及有效期。
- cert.pem:Apache 服務器端證書
- chain.pem:Apache 根證書和中繼證書
- fullchain.pem:Nginx 所需要 ssl_certificate 文件
- privkey.pem:安全證書 KEY 文件
如果報如下 DNS 錯誤信息:
To fix these errors, please make sure that your domain name was
entered correctly and the DNS A/AAAA record(s) for that domain
contain(s) the right IP address.
很有可能是你域名解析DNS服務商的問題,域名一定要解析到當前 VPS 服務器。目前已知 dnspod(已驗證)可用。網上說 cloudxns 也可以(筆者未驗證過)。
配置 ssl
以 NGINX 爲例:
ssl on;
ssl_certificate /etc/letsencrypt/live/www.exampe.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers oddn;
建議不要更改證書路徑,因爲更新的證書也存在這個位置。
更新證書
letsencrypt 生成的證書有效期只有 90 天,這是出於安全考慮。
在到期前,可以再次執行命令生成新的證書,如果覺得執行命令繁瑣,可以將腳本寫入 crontab,定時執行。
檢查證書更新:
$ /data/certbot/certbot-auto --dry-run renew
更新證書腳本:
0 3 1 * * root /data/certbot/certbot-auto renew --quiet --renew-hook "/usr/local/nginx/sbin/nginx -s reload"
DNS 方式
獲取 TXT 記錄
./certbot-auto --manual --preferred-challenges dns certonly
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Please enter in your domain name(s) (comma and/or space separated) (Enter 'c'
to cancel): www.example.com
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for www.example.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
-------------------------------------------------------------------------------
Please deploy a DNS TXT record under the name
_acme-challenge.www.example.com with the following value:
9Jmg5r1nZ6Y2z_mEhvWUj9YPQAbsay99YZKPgPnIXpI
Before continuing, verify the record is deployed.
-------------------------------------------------------------------------------
添加 TXT 記錄
主機記錄:_acme-challenge
記錄類型:TXT
記錄值:9Jmg5r1nZ6Y2z_mEhvWUj9YPQAbsay99YZKPgPnIXpI
測試 TXT 記錄是否生效。
$ dig -t txt _acme-challenge.example.com
生效後,回車。
看到如下內容:
Press Enter to Continue
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/www.exmaple.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/www.exmaple.com/privkey.pem
Your cert will expire on 2018-04-11\. 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
證書生成成功!
配置 ssl
同上。
證書更新
同上。
參考地址:
http://www.techug.com/post/https-ssl-tls.html
https://baike.baidu.com/item/https/285356?fr=aladdin
letsencrypt 官網:https://letsencrypt.org/
https://www.jianshu.com/p/13bf14eff6dc
DNS 生成方式:http://blog.csdn.net/u012291393/article/details/78768547
acme:https://github.com/Neilpang/acme.sh
https://github.com/xdtianyu/scripts/tree/master/le-dns