莫失莫忘
仙壽恒昌
前言
最近項目推進频蛔,Boss要求為了跟上時代潮流,將組里所有Http項目全部升級為Https項目秦叛,因此學習并實踐了這方面的內(nèi)容晦溪,同時做了一個記錄。
介紹
HTTP與HTTPS
HTTP(超文本傳送協(xié)議)定義了瀏覽器怎樣向服務器請求資源挣跋,以及服務器如何將資源傳送給服務器三圆。HTTP是面向事務的應用層協(xié)議,它是網(wǎng)絡上可靠交換文件的基礎避咆。HTTP使用了面向連接的TCP作為運輸層協(xié)議舟肉,保證了數(shù)據(jù)的可靠傳輸,因此HTTP不必考慮丟失重傳的問題(注:Http協(xié)議本身是無連接查库、無狀態(tài)的)路媚。
HTTPS(提供安全服務的HTTP協(xié)議)則確保了(1)用戶請求的服務器屬于真正的服務商(2)報文內(nèi)容在傳輸過程中沒有被更改(3)傳輸過程中敏感信息不被竊聽。要保證以上安全服務樊销,需要使用運輸層的安全協(xié)議整慎,現(xiàn)在廣泛使用的有如下兩個:
- 安全套接字層SSL(Secure Socket Layer)
- 運輸層安全TLS(Transport Layer Security)
SSL協(xié)議作用在端系統(tǒng)應用層的HTTP和運輸層之間,在TCP之上建立一個安全通道围苫,為通過TCP傳輸?shù)膽脤訑?shù)據(jù)提供安全保障裤园。之后,IETF在SSL 3.0的基礎上對其進行了標準化剂府,設計了TLS協(xié)議拧揽,為所有基于TCP的網(wǎng)絡應用提供安全數(shù)據(jù)傳輸服務。(注:SSL應該是運輸層協(xié)議周循,然而實際上强法,需要使用安全運輸?shù)膽贸绦颍ㄈ鏗TTP)卻把SSL駐留在應用層,因而應用層擴大了)
應用層使用SSL最多的就是HTTP湾笛,但SSL并非僅用于HTTP饮怯,而是可用于任何應用層的協(xié)議。HTTP調(diào)用SSL時嚎研,對整個網(wǎng)頁進行加密蓖墅。這時,在發(fā)送方临扮,SSL從SSL套接字接收應用層的數(shù)據(jù)(如HTTP報文或IMAP報文)论矾,對數(shù)據(jù)進行加密,然后把加密的數(shù)據(jù)送往TCP套接字杆勇;在接收方贪壳,SSL從TCP套接字讀取數(shù)據(jù),解密后蚜退,通過SSL套接字把數(shù)據(jù)交給應用層闰靴。
SSL提供的安全服務可歸納為以下三種:
- SSL服務器鑒別彪笼,允許用戶鑒別服務器身份。支持SSL的客戶端通過驗證來自服務器的證書蚂且,來鑒別服務器的真實身份并獲得服務器的公鑰
- SSL客戶鑒別配猫,SSL的可選安全服務,允許服務器證實客戶的身份
- 加密的SSL會話杏死,對客戶和服務器之間發(fā)送的所用報文進行加密泵肄,并檢測報文是否被篡改
Let's Encrypt
Let's Encrypt作為一個公共且免費SSL的項目逐漸被廣大用戶傳播和使用,是由Mozilla淑翼、Cisco腐巢、Akamai、IdenTrust玄括、EFF等組織人員發(fā)起系忙,主要的目的也是為了推進網(wǎng)站從HTTP向HTTPS過渡的進程,目前已經(jīng)有越來越多的商家加入和贊助支持惠豺。
過程
1.安裝 Let’s Encrypt 客戶端
yum install git python#安裝git
git clone https://github.com/letsencrypt/letsencrypt#克隆倉庫到本地
2.驗證安裝是否成功
使用以下命令運行一次客戶端银还,將自動檢查更新并升級(letsencrypt啟動后,總是會自動檢查更新并升級洁墙,除非使用--no-self-upgrade參數(shù)顯示指定)蛹疯,如果一切正常(事實上,升級后letsencrypt在某些系統(tǒng)热监、某些云服務商的機器上常常不能正常運行捺弦,因為涉及到各種源,版本依賴等問題)孝扛,將會顯示完整的幫助文檔列吼。
cd letsencrypt
./letsencrypt-auto --help all
3.驗證域名所有權(quán)并獲取證書
認證插件通過certonly命令啟用,認證功能用于確認你是域名的所有者苦始,并為你的域名獲取證書寞钥,證書被放置在你的域名所在服務器的/etc/letsencrypt/live/[domain]目錄。如果你一次性對多個域名進行認證陌选,則這些域名將共用一個證書文件理郑。
./letsencrypt-auto certonly
正常情況下,進入交互式界面咨油,提示你輸入郵箱(在證書失效前收到通知郵件)您炉,并同意官方協(xié)議
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): ********
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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: Y
驗證域名,選擇方式3役电,文件驗證
Requesting to rerun ./letsencrypt-auto with root privileges...
[sudo] password for zfy:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
How would you like to authenticate with the ACME CA?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: Apache Web Server plugin (apache)
2: Spin up a temporary webserver (standalone)
3: Place files in webroot directory (webroot)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-3] then [enter] (press 'c' to cancel): 3
Plugins selected: Authenticator webroot, Installer None
Please enter in your domain name(s) (comma and/or space separated) (Enter 'c'
to cancel): lilab.jysw.suda.edu.cn
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for lilab.jysw.suda.edu.cn
Input the webroot for lilab.jysw.suda.edu.cn: (Enter 'c' to cancel): /home/web/public/htdocs
Waiting for verification...
Cleaning up challenges
驗證成功赚爵,獲得證書
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/lilab.jysw.suda.edu.cn/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/lilab.jysw.suda.edu.cn/privkey.pem
Your cert will expire on 2019-04-04. To obtain a new or tweaked
version of this certificate in the future, simply run
letsencrypt-auto again. To non-interactively renew *all* of your
certificates, run "letsencrypt-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
4.安裝證書(基于nginx)
./letsencrypt-auto install --nginx --nginx-server-root <nginx conf path> --nginx-ctl <nginx binary path>
證書生成成功后,會讓你選擇是否將所有的 HTTP 請求重定向到 HTTPS(輸入 1 或者 2)。如果選 1冀膝,則通過 HTTP 和 HTTPS 都可以訪問膏蚓。如果選 2,則所有通過 HTTP 來的請求畸写,都會被 301 重定向到 HTTPS。
Requesting to rerun ./letsencrypt-auto with root privileges...
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator None, Installer nginx
Which certificate would you like to install?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: lilab.jysw.suda.edu.cn
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press 1 [enter] to confirm the selection (press 'c' to cancel): 1
Deploying Certificate to VirtualHost /usr/local/nginx/conf/nginx.conf
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 1
nginx配置文件如下:
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /etc/letsencrypt/live/lilab.jysw.suda.edu.cn/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/lilab.jysw.suda.edu.cn/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root /home/web/htdocs;
index index.html index.htm;
}
}
安裝成功
5.證書管理
- 查看letsencrypt在當前服務器獲取的證書
./letsencrypt-auto certificates
返回:
Found the following certs:
Certificate Name: lilab.jysw.suda.edu.cn
Domains: lilab.jysw.suda.edu.cn
Expiry Date: 2019-04-04 00:39:44+00:00 (VALID: 89 days)
Certificate Path: /etc/letsencrypt/live/lilab.jysw.suda.edu.cn/fullchain.pem
Private Key Path: /etc/letsencrypt/live/lilab.jysw.suda.edu.cn/privkey.pem
- 基本操作
通過--cert-name來指定證書的名稱氓扛,并對證書執(zhí)行操作枯芬,可選的操作有run、certonly采郎、certificates千所、renew、delete
./letsencrypt-auto certonly --cert-name <name> [operate]
run:獲取和安裝證書
certonly:獲取證書
certificates:查看和--cert-name指定的名稱匹配的證書信息
renew:更新快要過期的證書
delete:刪除證書
- 更新證書
證書的更新命令是renew蒜埋,renew命令會在本機找出所有的證書淫痰,并檢查證書的過期時間,它只會對有效期不足30天的證書執(zhí)行更新整份。如果證書不需要更新待错,它不會和letsencrypt服務器產(chǎn)生通信,因此烈评,renew命令可以頻繁地執(zhí)行而不會受到letsencrypt服務器的連接次數(shù)限制的影響火俄。也是基于這一特點,可以在crontab設置定期任務讲冠,頻繁地執(zhí)行renew操作瓜客,確保證書不會過期。
./letsencrypt-auto renew
設置定時任務
crontab -e
0 3 * * * ./letsencrypt-auto renew#在每天凌晨3點運行竿开。該命令將檢查服務器上的證書是否將在未來30天內(nèi)過期谱仪,如果是,則進行更新
資料
- 《計算機網(wǎng)絡》——謝希仁
- Nginx 實現(xiàn) HTTPS(基于 Let's Encrypt 的免費證書)
- https 證書工具 Letsencrypt 簡單教程