Let's Encrypt是最近很火的一個(gè)免費(fèi)SSL證書發(fā)行項(xiàng)目,Let's Encrypt是由ISRG提供的免費(fèi)免費(fèi)公益項(xiàng)目,自動(dòng)化發(fā)行證書,但是證書只有90天的有效期。適合個(gè)人使用或者臨時(shí)使用,不用再忍受自簽發(fā)證書不受瀏覽器信賴的提示。前段時(shí)間一直是內(nèi)測(cè),現(xiàn)在已經(jīng)開放了。本教程安裝不需要停掉當(dāng)前Web服務(wù)(Nginx/Apache),直接生成證書,廢話不多說下面開始:
建議使用git 以后有了新版更新方便,沒安裝的話Debian/Ubuntu:apt-get install git ,CentOS:yum install git-core
git clone https://github.com/letsencrypt/letsencrypt.git
cd letsencrypt
不安裝git的話:wget -c https://github.com/letsencrypt/letsencrypt/archive/master.zip && unzip master.zip && cd letsencrypt-master
LNMP一鍵安裝包都是Nginx/Apache默認(rèn)支持ssl不需要另外單獨(dú)編譯,接下來先以LNMP一鍵安裝包為例,LNMP用戶可以直接參考此教程:
執(zhí)行:mkdir -p /home/wwwroot/域名/.well-known/acme-challenge 創(chuàng)建臨時(shí)目錄,當(dāng)然這個(gè).well-known/acme-challenge前面的目錄要替換為你自己的網(wǎng)站目錄,根據(jù)你自己的實(shí)際情況修改。
正式開始生成證書
接下來正式進(jìn)行證書生成操作:
./letsencrypt-auto certonly --email 郵箱 -d 域名 --webroot -w /網(wǎng)站目錄完整路徑 --agree-tos
如果多個(gè)域名可以加多個(gè)-d 域名,注意替換上面的郵箱、域名和網(wǎng)站目錄,注意這里的網(wǎng)站目錄完整路徑只是你單純的網(wǎng)站目錄也就是虛擬主機(jī)配置文件里的,如Nginx虛擬主機(jī)配置里的root,Apache虛擬主機(jī)配置里的DocumentRoot。
首先Let's Encrypt會(huì)檢測(cè)系統(tǒng)安裝一些依賴包,安裝完依賴包會(huì)有藍(lán)色的讓閱讀TOS的提示,Agree回車 稍等片刻就行了可添加--agree-tos參數(shù)屏蔽該窗口。
生成證書后會(huì)有如下提示:
IMPORTANT NOTES:
- If you lose your account credentials, you can recover through
e-mails sent to licess@vpser.net.
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/www.vpser.net/fullchain.pem. Your cert will
expire on 2016-03-07. To obtain a new version of the certificate in
the future, simply run Let's Encrypt again.
- Your account credentials have been saved in your Let's Encrypt
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 Let's
Encrypt so making regular backups of this folder is ideal.
- If like Let's Encrypt, please consider supporting our work by:Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
Nginx虛擬主機(jī)的設(shè)置
接下來進(jìn)行配置Nginx虛擬主機(jī)文件,完整配置如下:
server
{
listen 443 ssl; //如果需要spdy也可以加上,lnmp1.2及其后版本都默認(rèn)支持spdy,lnmp1.3 nginx 1.9.5以上版本默認(rèn)支持http2
server_name www.vpser.net; //這里是你的域名
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/www.vpser.net; //網(wǎng)站目錄
ssl_certificate /etc/letsencrypt/live/www.vpser.net/fullchain.pem; //前面生成的證書,改一下里面的域名就行,不建議更換路徑
ssl_certificate_key /etc/letsencrypt/live/www.vpser.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; //這個(gè)是偽靜態(tài)根據(jù)自己的需求改成其他或刪除
#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;
}
需將上述配置根據(jù)自己的實(shí)際情況修改后,添加到虛擬主機(jī)配置文件最后面。
添加完需要執(zhí)行:/etc/init.d/nginx reload 重新載入配置使其生效。
如果需要HSTS,可以加上add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
Apache虛擬主機(jī)上的設(shè)置
Apache在生成證書后也需要修改一下apache的配置文件 /usr/local/apache/conf/httpd.conf ,查找httpd-ssl將前面的#去掉。
然后再執(zhí)行:
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
并在對(duì)應(yīng)apache虛擬主機(jī)配置文件的最后</VirtualHost>下面添加上SSL部分的配置文件:
<VirtualHost *:443>
DocumentRoot /home/wwwroot/www.vpser.net //網(wǎng)站目錄
ServerName www.vpser.net:443 //域名
ServerAdmin licess@vpser.net //郵箱
ErrorLog "/home/wwwlogs/www.vpser.net-error_log" //錯(cuò)誤日志
CustomLog "/home/wwwlogs/www.vpser.net-access_log" common //訪問日志
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/www.vpser.net/fullchain.pem //改一下里面的域名就行,不建議更換路徑
SSLCertificateKeyFile /etc/letsencrypt/live/www.vpser.net/privkey.pem //改一下里面的域名就行,不建議更換路徑
<Directory "/home/wwwroot/www.vpser.net"> //網(wǎng)站目錄
SetOutputFilter DEFLATE
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
DirectoryIndex index.html index.php
</Directory>
</VirtualHost>
需將上述配置根據(jù)自己的實(shí)際情況修改后,添加到虛擬主機(jī)配置文件最后面。注意要重啟apache使其實(shí)現(xiàn)。執(zhí)行:/etc/init.d/httpd restart 重啟Apache使其生效。
下面說一下可能會(huì)遇到的問題:
1、國內(nèi)DNS服務(wù)商可能會(huì)不行,目前已知dnspod、cloudxns不行
Namecheap、Route 53的都可以。
2、Linode福利或IPv6用戶福利
可能目前Linode用戶應(yīng)該遇到了
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.
這個(gè)不一定是因?yàn)镮P注冊(cè)的次數(shù)過多,可能是因?yàn)镮Pv6的事,具體解決方法如下:
執(zhí)行:sysctl -w net.ipv6.conf.all.disable_ipv6=1 來臨時(shí)禁用IPv6
再生成證書后執(zhí)行:sysctl -w net.ipv6.conf.all.disable_ipv6=0 再來解除禁用IPv6
證書續(xù)期
最后要說的是續(xù)期,因?yàn)樽C書只有90天,所以建議60左右的時(shí)候進(jìn)行一次續(xù)期,續(xù)期很簡(jiǎn)單可以交給crontab進(jìn)行完成,執(zhí)行:
cat >/root/renew-ssl.sh<<EOF
#!/bin/bash
mkdir -p /網(wǎng)站目錄完整路徑/.well-known/acme-challenge
/root/letsencrypt/letsencrypt-auto --renew-by-default certonly --email 郵箱 -d 域名 --webroot -w /網(wǎng)站目錄完整路徑 --agree-tos
EOF
chmod +x /root/renew-ssl.sh
注意要修改上面letsencrypt-auto的路徑為你自己的,并且里面的郵箱和域名也要修改。
再crontab里添加上:0 3 */60 * * /root/renew-ssl.sh 具體crontab教程點(diǎn)擊查看
2015.12.13更新
官網(wǎng)更新了參數(shù),對(duì)本文進(jìn)行了部分參數(shù)的調(diào)整。原-a webroot --webroot-path=/網(wǎng)站目錄完整路徑替換為--webroot -w;--renew替換為--renew-by-default;增加--agree-tos參數(shù)。
同時(shí)這里提醒一下如果設(shè)置了http 301跳到https的用戶,再續(xù)期前還需要在nginx設(shè)置如下:
80端口的虛擬主機(jī)上需要添加上,不加的話會(huì)無法驗(yàn)證的
location /.well-known/ {
add_header Content-Type 'text/plain;';
root /網(wǎng)站目錄完整路徑;
}
附完整的nginx下301 http跳到https的配置:
server
{
listen 80;
server_name www.vpser.net;
location /.well-known/ {
add_header Content-Type 'text/plain;';
root /網(wǎng)站目錄完整路徑;
}
location / {
return 301 https://www.vpser.net$request_uri;
}
}