如何免費把網站從http升級到https

預備知識: 

1.什麼是https, 相對於https有什麼優勢/劣勢?

2.升級https需要準備的文件? 如何升級? 

3.ssl證書類型?  (EV/OV/通配符證書....)

4.如何選擇ssl證書? 如何選擇供應商?  (推薦一家:godaddy比較便宜,Versign/GlobalSign等都比較偏貴)

5.如何免費獲取信任的ssl證書?


    如果對以上幾點還不熟悉的同學可以先去了解以下,本文主要講解第五點如何獲取到免費的被信任的SSL證書。 什麼時候可以考慮到使用免費的證書。 例如:你開發了一個app裏面將要傳輸一些敏感信息,例如:用戶姓名/聯繫方式等等。爲了保護用戶的數據不被泄漏就需要加上加密功能。 但是對於已經release的客戶端我們不可能要求客戶端升級,也就是老的客戶端依然使用的是http協議和老的客戶端程序。 如何保證用戶透明的情況下加密用戶數據呢? 自然我們想到了https,那好既然需要部署https證書如何來呢?  你也可以自簽名一個證書, 但是自簽名的證書對於已經發出去的app會認爲是不合法的CA導致錯誤。 所以,咱們必須搞到一個被信任的CA中心頒發的證書。 根據個人經驗目前有如下幾種方式:


方式一: 到沃通網站上申請免費的SSL證書。 (最近關閉了,可能幾個月後會開放)

方式二: 到各大供應商去申請試用的證書(解決緊急情況),一般有效期是7天或者是30天,RapidSSL是30天。

方式三: 還是開源的力量大, 使用"Let's Encrypt"提供的證書。


接下來, 重點介紹一下如何使用"Let's Encrypt"生成和renewal證書。"Let's Encrypt"是一家非營利的由各大知名網絡和互聯網公司贊助的一個提供免費證書的機構(https://letsencrypt.org/)。 有Akami/CISCO/Chrom...大家還是可以放心使用。 但是,該機構提供的證書有效期只有三個月也就是90天,所以90之後需要更新證書。


好,進入主題下面給大家演示以下如何在Nginx on Debian 7下生成SSL證書:需要準備好nginx和


1. 準備好一個nginx服務器。大家可以自行去下載和安裝nginx。在編譯nginx的時候注意加上http ssl模塊。 具體命令可以參考:

./configure --prefix=/data/server/nginx/nginx1.7/  
--with-pcre=/data/server/nginx/source/pcre-8.35/ 
--with-zlib=/data/server/nginx/source/zlib-1.2.8/ 
--with-http_ssl_module 
--with-http_realip_module 
--with-select_module 
--with-poll_module 
--add-module=/data/server/nginx/source/nginx_accept_language_module-master


2.下載安裝客戶端

wget https://dl.eff.org/certbot-auto

chmod a+x certbot-auto


3.準備好依賴包 (最好使用:ubuntu14.04以上的)

./certbot-auto


4.生成證書

      4.1.因爲大多數情況Nginx都是作爲一個負載均衡的作用在前端,後端放的業務服務。certbot需要在域名對應的webroot下面創建.well-known/acme-challenge文件。 所以,如果對於目前情況。我做了一個映射把這個路徑映射到了nginx的一個目錄下面。

location ~ /.well-known {
       access_log  logs/eapa_access.log  main;
       root html;
       index index.html index.htm;
  }


        4.2.生成證書,使用./certbot-auto certonly命令生成證書。參數說明: -w 指定webroot(即:4.1中配置的root目錄), -d 指定域名,可以是多個。  -w -d 是成對出現,也可以一次生成多個證書。下面我們爲eapa.test.com生成一個證書,該項目的路徑爲:/data/server/nginx/nginx1.7/html。

./certbot-auto certonly
 -w /data/server/nginx/nginx1.7/html
 -d eapa.test.com

     命令執行成功以後證書就放在了:/etc/letsencrypt/live/eapa.test.com/下面,可以看到如下幾個文件:

lrwxrwxrwx 1 root root 42 Nov 30 05:16 cert.pem  (證書文件)
lrwxrwxrwx 1 root root 43 Nov 30 05:16 chain.pem
lrwxrwxrwx 1 root root 47 Nov 30 05:16 fullchain.pem
lrwxrwxrwx 1 root root 45 Nov 30 05:16 privkey.pem (免密鑰的私鑰)


4.3安裝證書到nginx服務器就很簡單了,下面貼出來了源代碼

server {
        listen   443 ssl;
        server_name  eapa.test.com;

        ssl_certificate      /data/server/nginx/nginx1.7/ssl/cert.pem;
        ssl_certificate_key  /data/server/nginx/nginx1.7/ssl/privkey.pem;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location ~ /.well-known {
           access_log  logs/eapa_access.log  main;
           root html;
           index index.html index.htm;
        }

        location ~ / {
            access_log  logs/eapa_access.log  main;

            root   html;
            index  index.html index.htm;

            proxy_set_header  Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header    X-Scheme $scheme;
            client_max_body_size    10m;
            client_body_buffer_size 128k;
            proxy_connect_timeout   300;
            proxy_send_timeout      300;
            proxy_read_timeout      300;
            proxy_buffer_size       4k;
            proxy_buffers           4 32k;
            proxy_busy_buffers_size 64k;
            proxy_temp_file_write_size 64k;

            proxy_pass http://eapa;
            proxy_redirect http:// $scheme://; //(解決:springmvc redirect後https變成http)
        }


訪問:https://eapa.test.com就可以看到成功了.  (注意: 該域名是假域名)


最後, 因爲證書有效期三個月, 你可以手動或者寫個cron自動更新證書通過下面命令:

[yi_liu@tclserver- ssl]$ ./certbot-auto renew
Requesting root privileges to run certbot...
  /home/yi_liu/.local/share/letsencrypt/bin/letsencrypt renew
Saving debug log to /var/log/letsencrypt/letsencrypt.log

-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/eapa.test.com.conf
-------------------------------------------------------------------------------
Cert not yet due for renewal

The following certs are not due for renewal yet:
  /etc/letsencrypt/live/eapa.test.com/fullchain.pem (skipped)
No renewals were attempted.


效果如下圖:

wKiom1g-ibGgcqqoAACBwwgvA9w469.jpg-wh_50

參考文檔:

https://letsencrypt.org/docs/

https://certbot.eff.org/all-instructions/#centos-6-nginx

https://certbot.eff.org/docs/using.html#webroot

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章