免費https ssl證書 Ubuntu

Let's Encrypt簡介

Let's Encrypt作爲一個公共且免費SSL的項目逐漸被廣大用戶傳播和使用,是由Mozilla、Cisco、Akamai、IdenTrust、EFF等組織人員發起,主要的目的也是爲了推進網站從HTTP向HTTPS過度的進程,目前已經有越來越多的商家加入和贊助支持。

Let's Encrypt免費SSL證書的出現,也會對傳統提供付費SSL證書服務的商家有不小的打擊。到目前爲止,Let's Encrypt獲得IdenTrust交叉簽名,這就是說可以應用且支持包括FireFox、Chrome在內的主流瀏覽器的兼容和支持,雖然目前是公測階段,但是也有不少的用戶在自有網站項目中正式使用起來。

Let's Encrypt 的最大貢獻是它的 ACME 協議,第一份全自動服務器身份驗證協議,以及配套的基礎設施和客戶端。這是爲了解決一直以來 HTTPS TLS X.509 PKI 信任模型,即證書權威(Certificate Authority, CA)模型缺陷的一個起步。在客戶端-服務器數據傳輸中,公私鑰加密使得公鑰可以明文傳輸而依然保密數據,但公鑰本身是否屬於服務器,或公鑰與服務器是否同屬一個身份,是無法簡單驗證的。證書權威模型通過引入事先信任的第三方,由第三方去驗證這一點,並通過在服務器公鑰上簽名的方式來認證服務器。第三方的公鑰則在事先就約定並離線準備好,以備訪問時驗證簽名之用。這個第三方就稱爲證書權威,簡稱CA。相應的,CA驗證過的公鑰被稱爲證書。問題是,如果服務器私鑰泄露,CA無法離線使對應的證書無效化,只能另外發布無效記錄供客戶端查詢。也就是說,在私鑰泄露到CA發佈無效記錄的窗口內,中間人可以肆意監控服-客之間的傳輸。如果中間人設法屏蔽了客戶端對無效記錄的訪問,那麼直到證書過期,中間人都可以進行監控。而由於當前CA驗證和簽發證書大多手動,證書有效期往往在一年到三年。Let's Encrypt 簽發的證書有效期只有90天,甚至希望縮短到60天。有效期越短,泄密後可供監控的窗口就越短。

前提:

假定你已經用nginx搭建好網站

怎麼搭建nginx的,這裏我就不多說了

安裝Certbot

Certbot是維護Let's Encrypt的Package。

添加package repository

sudo add-apt-repository ppa:certbot/certbot

直接回車,添加完畢後,更新apt源數據:

sudo apt-get update

然後安裝Certbot的Nginx package:

sudo apt-get install python-certbot-nginx

簽發ssl證書

現在使用Let's Encrypt簽發ssl證書:

sudo certbot --nginx -d your-domian.com -d www.your-domain.com

注意這裏的 your-domain.com 換成你自己的域名,如果你第一次運行certbot的話,會讓你輸入郵箱,還要接受Let's Encrypt的協議,最後會讓你選擇是否重定向http到https:

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.

選2,重定向即可。

最後可以看到生成的證書的位置:


IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/oyty.me/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/oyty.me/privkey.pem
   Your cert will expire on 2018-09-24. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

 這個時候,ssl證書已經自動簽發完畢了,你可以訪問網站,發現已經是https的了。

查看我之前的nginx配置文件

server {
      # listen       80;
       listen         443 ssl;
	server_name www.you.com;
	ssl_certificate      /etc/letsencrypt/live/www.you.org/fullchain.pem;
	ssl_certificate_key   /etc/letsencrypt/live/www.you.org/privkey.pem;
	ssl_session_cache    shared:SSL:1m;
	ssl_session_timeout  5m;
	ssl_ciphers  HIGH:!aNULL:!MD5;
	ssl_prefer_server_ciphers  on;
    #以上代碼纔是重點

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
       
        location / {
            root   /opt/www;
            index index.php index.html index.htm;
	    if (!-e $request_filename) {
		rewrite  ^(.*)$  /index.php?s=$1  last;
		break;
	    }
        }
	location ~ \.php$ {
        	fastcgi_pass   127.0.0.1:9000;
        	fastcgi_index  index.php;
        	fastcgi_param  SCRIPT_FILENAME  /opt/www$fastcgi_script_name;

		fastcgi_connect_timeout 300;
		fastcgi_send_timeout 300;
		fastcgi_read_timeout 300;
		fastcgi_buffer_size 64k;
		fastcgi_buffers 4 64k;
		include        fastcgi_params;
	}        

    }

ssl證書文件自動集成了,如果沒有你也可以根據生成的證書地址自己配置。

自動更新證書

因爲 Let's Encrypt 簽發的 SSL 證書有效期只有 90 天,所有在過期之前,我們需要自動更新 SSL 證書,而如果你使用最新的 certbot 的話,Let's Encrypt 會幫你添加自動更新的腳本到 /etc/cron.d 裏,你只需要去檢測一下這個命令是否生效就OK!

sudo certbot renew --dry-run

 

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