2015年10月份,微博上偶然看到Let's Encrypt 推出了beta版,作為一個曾經被https虐出血的碼農來說,這無疑是一個重磅消息。並且在全站Https的大趨勢下,Let's Encrypt 脫穎而出,無疑會對傳統SSL證書提供商造成不小的打擊,並將Https的應用和推廣上升到一個空前火熱的階段。
Let’s Encrypt是由ISRG(Internet Security Research Group)提供的免費SSL項目,現由Linux基金會託管,他的來頭很大,由Mozilla、思科、Akamai、IdenTrust和EFF等組織發起,現在已經得到Google、Facebook等大公司的支持和贊助,目的就是向網站免費籤發和管理證書,並且通過其自身的自動化過程,消除了購買、安裝證書的複雜性,只需幾行命令,就可以完成證書的生成並投入使用,甚至十幾分鐘就可以讓自己的http站點華麗轉變成Https站點。
Let's Encrypt背後的力量
下面從實戰的角度,為大家詳細地介紹Let's Encrypt的使用過程
1、安裝Let's Encrypt
第一步:執行以下命令
git clone https://github.com/letsencrypt/letsencrypt cd letsencrypt ./letsencrypt-auto certonly --email xxx@xx.com
提示:
1、如果提示git命令無效的話,需要安裝一下GIt,直接執行命令 yum install git-all 完成安裝,
2、如果是RedHat/CentOs6系統的話,需要提前安裝EPEL(Extra Packages for Enterprise Linux),執行命令 yum install epel-release
3、 整個過程需要主機連接外網,否則會導致報以下錯誤
IMPORTANT NOTES: - The following errors were reported by the server: Domain: on-img.com Type: urn:acme:error:connection Detail: Failed to connect to host for DVSNI challenge Domain: www.on-img.com Type: urn:acme:error:connection Detail: Failed to connect to host for DVSNI challenge
4、Let's encrypt 是由python編寫的開源項目,基於python2.7環境,如果系統安裝的是python2.6,會提示升級
也可以執行以下命令(官方不推薦)./letsencrypt-auto certonly --email xxx@xx.com --debug
第二步:接下來提示輸入域名 多個用空格隔開
出現以下提示說明證書生成成功
2、使用證書
進入/etc/letsencrypt/live/on-img.com/下,on-img.com是第二部中填寫的域名,到時候換成自己的域名即可。
cert.pem 伺服器證書
privkey.pem 是證書私鑰
如果是雲伺服器+負載均衡的話,直接添加以上證書,綁定負載均衡,直接訪問https:// xxx.com
如果是自己配置的Nginx的,需要以下配置
server{listen 443 ssl; /server_name xxx.com; //這裡是你的域名index index.html index.htm index.php default.html default.htm default.php;root /opt/wwwroot/ //網站目錄ssl_certificate /etc/letsencrypt/live/xxx.com/fullchain.pem; //前面生成的證書,改一下裡面的域名就行,不建議更換路徑ssl_certificate_key /etc/letsencrypt/live/xxx.com/privkey.pem; //前面生成的密鑰,改一下裡面的域名就行,不建議更換路徑 ...}
最後 別忘了重載Nginx
執行 /etc/init.d/nginx reload 使其生效。
Nginx虛擬主機的設置接下來進行配置Nginx虛擬主機文件,完整配置如下:
server
{
listen 443 ssl; //如果需要spdy也可以加上,lnmp1.2及其後版本都默認支持spdy,lnmp1.3 nginx 1.9.5以上版本默認支持http2
server_name www.vpser.net; //這裡是你的域名
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/www.77169.net; //網站目錄
ssl_certificate /etc/letsencrypt/live/www.77169.net/fullchain.pem; //前面生成的證書,改一下裡面的域名就行,不建議更換路徑
ssl_certificate_key /etc/letsencrypt/live/www.77169.net/privkey.pem; //前面生成的密鑰,改一下裡面的域名就行,不建議更換路徑
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
include wordpress.conf; //這個是偽靜態根據自己的需求改成其他或刪除
#error_page 404 /404.html;
location ~ [^/]\.php(/|$)
{
# comment try_files $uri =404; to enable pathinfo
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf; //lnmp 1.0及之前版本替換為include fcgi.conf;
#include pathinfo.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
access_log off;
}
需將上述配置根據自己的實際情況修改後,添加到虛擬主機配置文件最後面。
添加完需要執行:/etc/init.d/nginx reload 重新載入配置使其生效。
如果需要HSTS,可以加上add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
Apache虛擬主機上的設置
Apache在生成證書後也需要修改一下apache的配置文件 /usr/local/apache/conf/httpd.conf ,查找httpd-ssl將前面的#去掉。
然後再執行:
cat >/usr/local/apache/conf/extra/httpd-ssl.conf<<EOF
Listen 443
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl .crl
SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH
SSLProxyCipherSuite EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH
SSLHonorCipherOrder on
SSLProtocol all -SSLv2 -SSLv3
SSLProxyProtocol all -SSLv2 -SSLv3
SSLPassPhraseDialog builtin
SSLSessionCache "shmcb:/usr/local/apache/logs/ssl_scache(512000)"
SSLSessionCacheTimeout 300
SSLMutex "file:/usr/local/apache/logs/ssl_mutex"
EOF
並在對應apache虛擬主機配置文件的最後</VirtualHost>下面添加上SSL部分的配置文件:
<VirtualHost *:443>
DocumentRoot /home/wwwroot/www.vpser.net //網站目錄
ServerName www.77169.net:443 //域名
ServerAdmin licess@77169.net //郵箱
ErrorLog "/home/wwwlogs/www.vpser.net-error_log" //錯誤日誌
CustomLog "/home/wwwlogs/www.vpser.net-access_log" common //訪問日誌
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/www.77169.net/fullchain.pem //改一下裡面的域名就行,不建議更換路徑
SSLCertificateKeyFile /etc/letsencrypt/live/www.77169.net/privkey.pem //改一下裡面的域名就行,不建議更換路徑
<Directory "/home/wwwroot/www.vpser.net"> //網站目錄
SetOutputFilter DEFLATE
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
DirectoryIndex index.html index.php
</Directory>
</VirtualHost>
需將上述配置根據自己的實際情況修改後,添加到虛擬主機配置文件最後面。注意要重啟apache使其實現。執行:/etc/init.d/httpd restart 重啟Apache使其生效。
下面說一下可能會遇到的問題:1、國內DNS服務商可能會不行,目前已知dnspod、cloudxns不行Namecheap、Route 53的都可以。
2、Linode福利或IPv6用戶福利可能目前Linode用戶應該遇到了
An unexpected error occurred:
There were too many requests of a given type :: Error creating new registration :: Too many registrations from this IP
Please see the logfiles in /var/log/letsencrypt for more details.
這個不一定是因為IP註冊的次數過多,可能是因為IPv6的事,具體解決方法如下:
執行:sysctl -w net.ipv6.conf.all.disable_ipv6=1 來臨時禁用IPv6
再生成證書後執行:sysctl -w net.ipv6.conf.all.disable_ipv6=0 再來解除禁用IPv6
最後要說的是續期,因為證書只有90天,所以建議60左右的時候進行一次續期,續期很簡單可以交給crontab進行完成,執行:
cat >/root/renew-ssl.sh<<EOF
#!/bin/bash
mkdir -p /網站目錄完整路徑/.well-known/acme-challenge
/root/letsencrypt/letsencrypt-auto --renew-by-default certonly --email 郵箱 -d 域名 --webroot -w /網站目錄完整路徑 --agree-tos
EOF
chmod +x /root/renew-ssl.sh
注意要修改上面letsencrypt-auto的路徑為你自己的,並且裡面的郵箱和域名也要修改。
再crontab裡添加上:0 3 */60 * * /root/renew-ssl.sh 具體crontab教程點擊查看
2015.12.13更新
官網更新了參數,對本文進行了部分參數的調整。原-a webroot --webroot-path=/網站目錄完整路徑替換為--webroot -w;--renew替換為--renew-by-default;增加--agree-tos參數。
同時這裡提醒一下如果設置了http 301跳到https的用戶,再續期前還需要在nginx設置如下:
80埠的虛擬主機上需要添加上,不加的話會無法驗證的
location /.well-known/ {
add_header Content-Type 'text/plain;';
root /網站目錄完整路徑;
}
附完整的nginx下301 http跳到https的配置:
server
{
listen 80;
server_name www.vpser.net;
location /.well-known/ {
add_header Content-Type 'text/plain;';
root /網站目錄完整路徑;
}
location / {
return 301 https://www.77169.com$request_uri;
}
}
3、最後
以上配置無誤,就直接可以使用https,我的測試環境是 虛擬主機 + 負載均衡,整體搭建到上線使用,不到二十分鐘,各大瀏覽器測試均正常
比特幣贊助打賞地址:13sbdFRyFBeqmXY9GJQf66s5cwmvLvWaAD
----
要聞、乾貨、原創、專業
關注「黑白之道」 微信:i77169
華夏黑客同盟我們堅持,自由,免費,共享!