申請Let's Encrypt永久免費SSL證書過程教程及常見問題,及續期


近幾年,在瀏覽器廠商的強力推動下,HTTPS的使用率大增。據統計,Firefox加載的網頁中啟用HTTPS的占比為67%,谷歌搜索結果中HTTPS站點占比已達50%,HTTPS網站已獲得瀏覽器和搜索引擎的共同青睞。據悉,瀏覽器開發商Mozilla,Google准備采取下一步措施:將所有的HTTP網站標記為不安全。

 

近兩年,微信小程序也異常火爆,小程序相關話題頻頻在朋友圈刷屏。如果你想開發微信小程序,一樣繞不開HTTPS。根據微信小程序開發文檔介紹,小程序與服務器交互必須使用HTTPS協議。

 

不管是大勢所趨,還是實際項目開發需要,掌握如何給網站添加HTTPS協議支持(Web服務器設置使用SSL證書后,網站就能支持HTTPS訪問了)都是非常有必要的。

 

一般來說,很多公司都會直接購買由GlobalSignGeoTrustVerisign等全球公認的數字證書頒發機構頒發的SSL證書。購買?沒錯,大多數SSL證書都需要按年付費使用,而且價格不菲。很多小公司、創業團隊或個人開發者往往不願意承擔這筆費用,那有沒有免費的SSL證書可以使用?

 

Let's Encrypt 是一個免費、開放,自動化的證書頒發機構,由 ISRG(Internet Security Research Group)運作。ISRG 是一個關注網絡安全的公益組織,其贊助商包括 Mozilla、Akamai、Cisco、EFF、Chrome、IdenTrust、Facebook等公司。ISRG 的目的是消除資金和技術領域的障礙,全面推進網站從HTTP到HTTPS過度的進程。

 

目前,包括FireFox、Chrome在內的主流瀏覽器都支持Let's Encrypt證書,已經有不少用戶在真實項目中使用Let's Encrypt證書。Let's Encrypt免費SSL證書的有效期是90天,到期后可以再續期,這樣也就可以變相長期使用了。 

 

官方網站:https://letsencrypt.org/

項目主頁:https://github.com/letsencrypt/letsencrypt

 

實驗環境

[plain] view plain copy

 
 

 

  1. Ubuntu 14.04.2 LTS  
  2. Python 2.7.6  
  3. tomcat-7.0.68  

PS:所有不說明實驗環境的技術文章都是耍流氓!

筆者使用的是一台 Ubuntu 測試機,上面沒有跑 Apache 和 Nginx,單獨運行的 Tomcat,然后通過 iptables 進行端口轉發(將 80 端口的請求轉發到 8080,將 443 端口的請求轉發到 8443),並且已完成域名解析。

 

獲取Let's Encrypt免費SSL證書

首先,我們需要從 https://github.com/letsencrypt/letsencrypt 上下載 letsencrypt-auto 腳本,使用它能夠很方便地完成域名驗證和SSL證書的獲取。

 

[plain] view plain copy

 
 

 

  1. # 使用git下載腳本  
  2. $ git clone https://github.com/letsencrypt/letsencrypt  
  3.   
  4. # 進入到腳本所在目錄  
  5. $ cd letsencrypt  
  6.   
  7. # 查看 letsencrypt-auto 工具的用法  
  8. $ ./letsencrypt-auto --help  

腳本下載好了,來看看怎么使用吧。letsencrypt-auto工具的用法如下:

[plain] view plain copy

 
 

 

  1. root@iZ28425ljejZ:~/letsencrypt# ./letsencrypt-auto --help  
  2.   
  3. -------------------------------------------------------------------------------  
  4.   
  5.   letsencrypt-auto [SUBCOMMAND] [options] [-d DOMAIN] [-d DOMAIN] ...  
  6.   
  7. Certbot can obtain and install HTTPS/TLS/SSL certificates.  By default,  
  8. it will attempt to use a webserver both for obtaining and installing the  
  9. certificate. The most common SUBCOMMANDS and flags are:  
  10.   
  11. obtain, install, and renew certificates:  
  12.     (default) run   Obtain & install a certificate in your current webserver  
  13.     certonly        Obtain or renew a certificate, but do not install it  
  14.     renew           Renew all previously obtained certificates that are near expiry  
  15.    -d DOMAINS       Comma-separated list of domains to obtain a certificate for  
  16.   
  17.   --apache          Use the Apache plugin for authentication & installation  
  18.   --standalone      Run a standalone webserver for authentication  
  19.   --nginx           Use the Nginx plugin for authentication & installation  
  20.   --webroot         Place files in a server's webroot folder for authentication  
  21.   --manual          Obtain certificates interactively, or using shell script hooks  
  22.   
  23.    -n               Run non-interactively  
  24.   --test-cert       Obtain a test certificate from a staging server  
  25.   --dry-run         Test "renew" or "certonly" without saving any certificates to disk  
  26.   
  27. manage certificates:  
  28.     certificates    Display information about certificates you have from Certbot  
  29.     revoke          Revoke a certificate (supply --cert-path)  
  30.     delete          Delete a certificate  
  31.   
  32. manage your account with Let's Encrypt:  
  33.     register        Create a Let's Encrypt ACME account  
  34.   --agree-tos       Agree to the ACME server's Subscriber Agreement  
  35.    -m EMAIL         Email address for important account notifications  
  36.   
  37. More detailed help:  
  38.   
  39.   -h, --help [TOPIC]    print this message, or detailed help on a topic;  
  40.                         the available TOPICS are:  
  41.   
  42.    all, automation, commands, paths, security, testing, or any of the  
  43.    subcommands or plugins (certonly, renew, install, register, nginx,  
  44.    apache, standalone, webroot, etc.)  
  45. -------------------------------------------------------------------------------  

這里只對幾個重要的命令參數進行說明:

[plain] view plain copy

 
 

 

  1. run:獲取並安裝證書到當前的Web服務器  
  2. certonly:獲取或續期證書,但是不安裝  
  3. renew:在證書快過期時,續期之前獲取的所有證書  
  4. -d DOMAINS:一個證書支持多個域名,用逗號分隔  
  5.   
  6. --apache:使用 Apache 插件來認證和安裝證書  
  7. --standalone:運行獨立的 web server 來驗證  
  8. --nginx:使用 Nginx 插件來認證和安裝證書  
  9. --webroot:如果目標服務器已經有 web server 運行且不能關閉,可以通過往服務器的網站根目錄放置文件的方式來驗證  
  10. --manual:通過交互式方式,或 Shell 腳本手動獲取證書  

關於域名驗證和證書的獲取安裝,上面提到了5種方式:--apache, --standalone, --nginx, --webroot 和 --manual,請根據實際情況選擇其一。再次重申,筆者使用的是公司的其中一台測試機,上面沒有跑 Apache 和 Nginx,單獨運行的 Tomcat,然后通過 iptables 進行端口轉發(將 80 端口的請求轉發到 8080,將 443 端口的請求轉發到 8443),並且已完成域名解析。因此,筆者選擇采用 --standalone 方式進行域名驗證和證書獲取。

[plain] view plain copy

 
 

 

  1. # 獲取證書  
  2. $ ./letsencrypt-auto certonly --standalone --email 58472399@qq.com -d quqianzhao.top -d www.quqianzhao.top  

此處需要注意:

                        (1)執行此命令必須使用 root用戶獲得文件夾的權限

                        (2)域名能訪問並且有綁定的公網IP

                         (3)必須在此域名綁定的服務器上運行

                            (4)會使用80斷端口,如果nginx監聽80端口,把nginx先關掉

注意將上面的郵箱和域名替換成自己的。上面命令中的 certonly 表示只獲取證書,不安裝;-d 有兩個,表示將要獲取的SSL證書綁定兩個域名。

 

上面的命令在執行過程中,會有兩次確認。命令執行完成后,如果看到提示信息"Congratulations! Your certificate and chain..."就說明證書創建成功了,如下圖所示。

 

如果使用nginx,可以直接在nginx上配置,具體參見https://blog.csdn.net/u013378306/article/details/80029828,把自己生成的正證書替換一下

 

  1.  
    server {
  2.  
    listen 443 ssl;
  3.  
    server_name kf.aigoes.com;
  4.  
    #ssl on;
  5.  
     
  6.  
    ssl_certificate /etc/letsencrypt/live/quqianzhao.top/fullchain.pem; #2
  7.  
    ssl_certificate_key /etc/letsencrypt/live/quqianzhao.top/privkey.pem; #3
  8.  
    ssl_session_cache shared:SSL:1m;
  9.  
    ssl_session_timeout 5m;
  10.  
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  11.  
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  12.  
    ssl_prefer_server_ciphers on;
  13.  
    location / {
  14.  
    root html;
  15.  
    index index.html index.htm;
  16.  
    }
  17.  
    location /aaa {
  18.  
     
  19.  
    proxy_redirect off;
  20.  
     
  21.  
    proxy_set_header Host $host;
  22.  
     
  23.  
    proxy_set_header X-Real-IP $remote_addr;
  24.  
     
  25.  
    proxy_set_header X-Forwarded-Proto https;
  26.  
    proxy_read_timeout 3600s;
  27.  
    proxy_pass http://127.0.0.1:8080;
  28.  
     
  29.  
    }}

 

 

Tomcat7配置Let's Encrypt免費SSL證書

 

 

從上圖(證書獲取成功的提示信息)可以看出,獲取到的 Let's Encrypt 證書保存在 /etc/letsencrypt/live/quqianzhao.top/ 目錄下,相關的證書文件有4個:

 

[plain] view plain copy

 
 

 

  1. cert.pem  
  2. chain.pem  
  3. fullchain.pem  
  4. privkey.pem  

我們需要用到的是后面兩個證書文件: fullchain.pem 和 privkey.pem。接下來,還需要用到 Linux 下的 openssl 和 keytool 工具,將SSL證書由 .pem 格式轉換成 Tomcat 所支持的 .jks 格式。

 

[plain] view plain copy

 
 

 

  1. # 導出.p12格式的證書  
  2. $ openssl pkcs12 -export -in fullchain.pem -inkey privkey.pem -out zyxx_letsencrypt.p12 -name tomcat_letsencrypt  
  3.   
  4. # 再將證書由.p12格式轉換成.jks格式  
  5. $ keytool -importkeystore -deststorepass 'zxxx_123' -destkeypass 'zxxx_123' -destkeystore zyxx_letsencrypt.jks -srckeystore zyxx_letsencrypt.p12 -srcstoretype PKCS12 -srcstorepass 'zxxx_123' -alias tomcat_letsencrypt  

說明:執行 openssl 命令導出 .p12 格式證書時會要求設置密鑰,執行 keytool 命令時也有3處要寫密鑰,最簡單的方式就是所有需要密鑰的地方,都使用同一個,這樣也不會搞混。

 

經過上面的兩步操作,/etc/letsencrypt/live/quqianzhao.top/ 目錄下就生成了一個新的證書 zyxx_letsencrypt.jks。接下來,修改 %tomcat%/conf/server.xml 文件,添加 keystoreFile 和 keystorePass 兩行配置。其中,keystoreFile 指向 jks 證書文件,而 keystorePass 則為證書的密鑰。修改后的關鍵配置如下:

 

[plain] view plain copy

 
 

 

  1. <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol" SSLEnabled="true"  
  2.     maxThreads="150" scheme="https" secure="true"  
  3.     clientAuth="false" sslProtocol="TLS"  
  4.     keystoreFile="/home/test/zyxx_letsencrypt.jks"  
  5.     keystorePass="zxxx_123"  
  6. />  

OK,到這里 Tomcat 的配置工作就完成了。接下來,重啟 Tomcat,然后使用瀏覽器訪問 https://www.quqianzhao.top 來驗證服務器證書是否生效。

查看服務器證書信息:

        

 

Let's Encrypt 證書續期

出於安全原因,Let's Encrypt 頒發的 SSL 證書有效期為90天,我們可以通過自動續期來解決。如果到期沒有更新證書,Let's Encrypt 會向申請證書時提交的email發送提醒郵件。

進入到 letsencrypt-auto 腳本所在目錄,執行下面的命令即可完成 SSL 證書的續期。

 

[plain] view plain copy

 
 

 

  1. ./letsencrypt-auto renew  

默認情況下,在證書即將到期之前才能執行續期操作,否則會提示“Cert not yet due for renewal”,即證書尚未到期。如果需要強制執行續期操作,可以加上參數 --force-renew ,命令如下:

 

[plain] view plain copy

 
 

 

  1. ./letsencrypt-auto renew --force-renew  

以下是筆者強制執行證書續期的操作結果:

 

[plain] view plain copy

 
 

 

  1. root@iZ28425ljejZ:~/letsencrypt# ./letsencrypt-auto renew --force-renew  
  2. Saving debug log to /var/log/letsencrypt/letsencrypt.log  
  3.   
  4. -------------------------------------------------------------------------------  
  5. Processing /etc/letsencrypt/renewal/quqianzhao.top.conf  
  6. -------------------------------------------------------------------------------  
  7. Plugins selected: Authenticator standalone, Installer None  
  8. Renewing an existing certificate  
  9. Performing the following challenges:  
  10. tls-sni-01 challenge for quqianzhao.top  
  11. tls-sni-01 challenge for www.quqianzhao.top  
  12. Waiting for verification...  
  13. Cleaning up challenges  
  14.   
  15. -------------------------------------------------------------------------------  
  16. new certificate deployed without reload, fullchain is  
  17. /etc/letsencrypt/live/quqianzhao.top/fullchain.pem  
  18. -------------------------------------------------------------------------------  
  19.   
  20. -------------------------------------------------------------------------------  
  21.   
  22. Congratulations, all renewals succeeded. The following certs have been renewed:  
  23.   /etc/letsencrypt/live/quqianzhao.top/fullchain.pem (success)  
  24. -------------------------------------------------------------------------------  

考慮到有可能會忘記續期,或者每3個月就要續期一次太麻煩,可以使用 linux crontab 每兩個月(0 0 1 */2 *)執行一次證書續期操作。

 

關於 https 頁面使用http請求的問題

 

首先https頁面不支持http請求,會報錯  Mixed Content:This request has been blocked; the content must be served over HTTPS.,

    (1)如果請求的地址可以為https,則可以直接在<head> 中加入

        <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">

(2)可以在相應的頁面的<head>里加上這句代碼,意思是自動將http的不安全請求升級為https

如果必須為http請求,則 可以使用<iframe> 把http請求的頁面嵌套進來

 

最后注意ssl 443端口 防火牆 問題

 

其他參考地址https://zhuanlan.zhihu.com/p/24996258?refer=diamondfsd


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM