Let's Encrypt證書安裝

申請讓咱們加密泛域名證書及Nginx / Apache證書配置

讓咱們加密基礎知識

  • 讓咱們加密是一個認證機構(證書頒發機構= CA)
  • 想使用HTTPS須要認證機構頒發的電子證書
  • 讓咱們加密和其餘認證機構的區別(或者說是賣點):
    • 免費,Let's Encrypt提供期限是90天的免費電子證書
    • 提供工具certbot自動生成電子證書文件
  • 讓咱們加密爲了自動生成電子證書搞了一個ACME協議
    • 自動證書管理環境= ACME,自動認證管理環境協議
    • 協議草案已經提交IETF
    • ACME協議的基本思路是:
      • 在你服務器上生成一次性的隨機特徵數據(隨機數)
      • 而後經過Let's Encrypt的服務器覈對這個數據
      • 覈對成功發放證書
      • 有兩種方式,HTTP和DNS,通常使用的是前者
  • certbot官方安裝文檔

獲取泛域名證書

安裝Certbot

從官方源安裝最新版certbot(最新版爲0.22.0,從0.22.0版本纔開始支持泛域名申請,不推薦從Debian的源安裝,常年不更新,還停留在0.10)html

//dl.eff.org/certbot-autowget https:
autochmod a+x ./certbot-

初始化nginx

auto./certbot-

獲取證書

由於目前大多數國內的DNS服務商不在API支持的列表裏,因此如下使用手動方式進行DNS認證,將只要下方命令中的*.minirplus.com替換爲本身的域名便可git

autopublic-01//acme-v02.api.letsencrypt.org/directory./certbot-certonly --manual -d *.minirplus.com --agree-tos --no-bootstrap --manual--ip-logging-ok --preferred-challenges dns--server https:

!域名注意的 minirplus.com 解析記錄必須以A記錄方式指向當前運行命令的服務器IP,而不能使用CNAME記錄不然會報錯,報錯信息以下:github

IMPORTANT NOTES:
- The following errors were reported by the server:
 
 
   Domain: minirplus.com
   Type:   connection
   Detail: DNS problem: SERVFAIL looking up TXT for
   _acme-challenge.minirplus.com
 
 
   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. Additionally, please check that
   your computer has a publicly routable IP address and that no
   firewalls are preventing the server from communicating with the
   client. If you're using the webroot plugin, you should also verify
   that you are serving files from the webroot path you provided.

運行該命令後,會要求輸入郵箱,用於接收證書過時通知web

Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):

接着會出現一段廣告,大意是收集客戶郵箱給贊助商,YN都可算法

-------------------------------------------------------------------------------
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 EFF and
our work to encrypt the web, protect its users and defend digital rights.
-------------------------------------------------------------------------------
(Y)es/(N)o:

接着重要的部分來了,在DNS中記錄一個添加_acme-challenge前綴的域名TXT記錄,記錄的內容爲中間顯示的隨機碼xVloe7V1kMEd2ZlOLlUxv-HltYfTDaMhrrwKjFU47DUapache

-------------------------------------------------------------------------------
Please deploy a DNS TXT record under the name
_acme-challenge.minirplus.com with the following value:
 
 
xVloe7V1kMEd2ZlOLlUxv-HltYfTDaMhrrwKjFU47DU
 
 
Before continuing, verify the record is deployed.
-------------------------------------------------------------------------------
Press Enter to Continue

確保接着當前域名的根記錄 minirplus.com 爲A記錄而且指向當前服務器IP(這條本來不成問題,由於國外的服務商的DNS根域名只能添加甲記錄,可是國內的DNSPOD則更加靈活,能夠添加CNAME記錄,因此會在認證的時候出現問題)bootstrap

按回車,進行認證ubuntu

等待片刻,出現以下信息,說明認證成功api

申請操做成功後,會在界面中輸出證書的存放路徑,以及證書的到期時間(90

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/minirplus.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/minirplus.com/privkey.pem
   Your cert will expire on 2018-06-19. 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

證書的存放路徑

這裏以example.com爲例

生成證書中會建立/etc/letsencrypt文件夾,證書文件默認存放在/etc/letsencrypt/live/example.com文件夾中,其中example.com取自第一個域名
example.com文件夾中包含4個文件./cert.pem ./chain.pem ./fullchain.pem ./privkey.pem

  • cert.pem域名證書
  • chain.pem根證書及中間證書
  • fullchain.pem由cert.pem和chain.pem合併而成
  • privkey.pem證書私人

建立一個2048位的Diffie-Hellman文件
(nginx默認使用1024位的Diffie-Hellman進行密鑰交換,安全性過低)

out2048openssl dhparam -/etc/letsencrypt/live/dhparams.pem

nginx TSL配置(強制http重定向到https)

這裏以example.com爲例

首先對http協議進行301重定向到https協議

server {
  listen 80;
  server_name example.com www.example.com;
return$request_uri 301 https://example.com;
}

nginx https相關配置

這裏以example.com爲例

server {
    listen 443 ssl;
    server_name example.com www.example.com;
   
# 配置站點證書文件地址    
    ssl_certificate      /etc/letsencrypt/live/example.com/fullchain.pem;
# 配置證書私鑰   
    ssl_certificate_key  /etc/letsencrypt/live/example.com/privkey.pem;
   
# 配置 Diffie-Hellman 交換算法文件地址    
    ssl_dhparam          /etc/letsencrypt/live/dhparams.pem;
   
# 配置服務器可以使用的加密算法    
'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS'    ssl_ciphers;
 
# 指定服務器密碼算法在優先於客戶端密碼算法時,使用 SSLv3 和 TLS 協議   
    ssl_prefer_server_ciphers  on;
   
# ssl 版本 可用 SSLv2,SSLv3,TLSv1,TLSv1.1,TLSv1.2     
# ie6 只支持 SSLv2,SSLv3 可是存在安全問題, 故不支持    
    ssl_protocols        TLSv1 TLSv1.1 TLSv1.2;
   
# 配置 TLS 握手後生成的 session 緩存空間大小 1m 大約能存儲 4000 個 session    
    ssl_session_cache          shared:SSL:50m;
# session 超時時間   
    ssl_session_timeout        1d;
   
# 負載均衡時使用 此處暫時關閉 詳情見 https://imququ.com/post/optimize-tls-handshake.html     
# 1.5.9 及以上支持    
    ssl_session_tickets off;
   
# 瀏覽器可能會在創建 TLS 鏈接時在線驗證證書有效性,從而阻塞 TLS 握手,拖慢總體速度。OCSP stapling 是一種優化措施,服務端經過它能夠在證書鏈中封裝證書頒發機構的 OCSP(Online Certificate Status Protocol)響應,從而讓瀏覽器跳過在線查詢。服務端獲取 OCSP 一方面更快(由於服務端通常有更好的網絡環境),另外一方面能夠更好地緩存 以上內容來自 https://imququ.com/post/my-nginx-conf-for-wpo.html    
# 1.3.7 及以上支持   
    ssl_stapling               on;
    ssl_stapling_verify        on;
# 根證書 + 中間證書   
    ssl_trusted_certificate    /etc/letsencrypt/live/example.com/fullchain.pem;
   
# HSTS 能夠告訴瀏覽器,在指定的 max-age 內,始終經過 HTTPS 訪問該域名。即便用戶本身輸入 HTTP 的地址,或者點擊了 HTTP 連接,瀏覽器也會在本地替換爲 HTTPS 再發送請求 相關配置見 https://imququ.com/post/sth-about-switch-to-https.html    
    add_header Strict-Transport-Security max-age=60;
   
# 在此填寫本來 http 協議中的配置    
}
 

以上配置完成後,重啓nginx便可完成對https的切換
如遇權限問題請使用sudo

service nginx restart

或者

sudo systemctl reload nginx

更新證書

certbot生成的證書是有90天期限的。
使用如下命令便可進行續期,續期成功後須要服務器

auto./certbot-renew

該命令只會對快到期的證書纔會進行更新,若是但願強制更新,能夠增長--force-renewal參數


修改Apache的配置文件

進入的/ etc / apache2的/網站可用,修改泛域名配置文件(這裏以000-default.conf爲例),添加SSL配置,將下面配置中的SSL證書地址,替換爲以前成功獲取的證書地址(如直接使用如下配置,請修改的DocumentRoot和目錄目錄爲泛域名指向的目錄)

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/vps
    ServerSignature Off
<Directory /var/www/vps >   
        Options -Indexes
</Directory>   
</VirtualHost>
 
 
<IfModule mod_ssl.c>
<VirtualHost *:443>   
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/vps
        ServerSignature Off
<Directory /var/www/vps >       
            Options -Indexes
</Directory>       
        SSLEngine on
        SSLCertificateFile /etc/letsencrypt/live/minirplus.com/fullchain.pem;
        SSLCertificateKeyFile /etc/letsencrypt/live/minirplus.com/privkey.pem
</VirtualHost>   
</IfModule>

效果

當用戶訪問任意域名,例如https://xVloe7V1kMEd2ZlOLlUxv.minirplus.com

都會看到綠色的HTTPS鏈接標誌。

總結

有了泛域名證書以後有幾個好處

  1. 只要申請一次,全部子域名均可以使用,不再用重複申請證書了。
  2. 用戶隨機生成的子域名也可使用HTTPS訪問了。
相關文章
相關標籤/搜索