免費SSL安全證書Let's Encrypt安裝使用教程(附Nginx/Apache配置)
Let's Encrypt是最近很火的一個免費SSL證書發行項目,Let's Encrypt是由ISRG提供的免費免費公益項目,自動化發行證書,但是證書只有90天的有效期。適合個人使用或者臨時使用,不用再忍受自簽發證書不受瀏覽器信賴的提示。前段時間一直是內測,現在已經開放了。本教程安裝不需要停掉當前Web服務(Nginx/Apache),直接生成證書,廢話不多說下面開始:
目前已經更新為新工具certbot,使用教程:http://www.0794baidu.com/build/letsencrypt-certbot.html 配置文件和以前一樣。
建議使用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默認支持ssl不需要另外單獨編譯,接下來先以LNMP一鍵安裝包為例,LNMP用戶可以直接參考此教程(LNMP v1.4自帶此功能,可以直接使用lnmp ssl add (已經添加過該域名) 或lnmp vhost add (未添加過該域名)添加,1.3及之前用戶可以升級到1.4,lnmp1.3升級1.4教程):
執行:mkdir -p /home/wwwroot/域名/.well-known/acme-challenge 創建臨時目錄,當然這個.well-known/acme-challenge前面的目錄要替換為你自己的網站目錄,根據你自己的實際情況修改。
正式開始生成證書
接下來正式進行證書生成操作:
./letsencrypt-auto certonly --email 郵箱 -d 域名 --webroot -w /網站目錄完整路徑 --agree-tos
如果多個域名可以加多個-d 域名,注意替換上面的郵箱、域名和網站目錄,注意這里的網站目錄完整路徑只是你單純的網站目錄也就是虛擬主機配置文件里的,如Nginx虛擬主機配置里的root,Apache虛擬主機配置里的DocumentRoot。
首先Let's Encrypt會檢測系統安裝一些依賴包,安裝完依賴包會有藍色的讓閱讀TOS的提示,Agree回車 稍等片刻就行了可添加--agree-tos參數屏蔽該窗口。
生成證書后會有如下提示:
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.0794baidu.com/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虛擬主機的設置
接下來進行配置Nginx虛擬主機文件,LNMP模式下完整配置如下:
server
{
listen 443 ssl; ? #lnmp1.3+版本默認支持http2,可以加上http2,lnmp1.2版本默認支持spdy,可以加上spdy
server_name www.0794baidu.com; ? ? ##這里是你的域名
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/www.0794baidu.com; ? ? ? ? ? ?#網站目錄
ssl_certificate /etc/letsencrypt/live/www.0794baidu.com/fullchain.pem; ? ?#前面生成的證書,改一下里面的域名就行
ssl_certificate_key /etc/letsencrypt/live/www.0794baidu.com/privkey.pem; ? #前面生成的密鑰,改一下里面的域名就行
SSLCertificateChainFile /etc/letsencrypt/live/www.0794baidu.com/chain.pem; #Apache 2.2版本需要加入該中間證書,否則瀏覽器可能不信任
ssl_ciphers "EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
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,如果需要HSTS,可以加上add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"; 前面參數里的數值是秒。?開啟后有效期內只能https訪問而且所有二級域名也都是強制https,不確定的話不要開啟。
LNMPA模式下只需要在Nginx上設置,只需要將location ~ [^/]\.php(/|$) { 到下一個 } 的php解析部分替換為 include proxy-pass-php.conf; 即可。
Apache虛擬主機上的設置
LAMP模式按下面配置文件。Apache在生成證書后也需要修改一下apache的配置文件 /usr/local/apache/conf/httpd.conf ,查找httpd-ssl將前面的#去掉。
然后再執行:
Apache 2.2如下:
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+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5
SSLProxyCipherSuite EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5
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"
SSLStrictSNIVHostCheck on
NameVirtualHost *:443
EOF
Apache 2.4如下:
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+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5
SSLProxyCipherSuite EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5
SSLHonorCipherOrder on
SSLProtocol all -SSLv2 -SSLv3
SSLProxyProtocol all -SSLv2 -SSLv3
SSLPassPhraseDialog builtin
SSLSessionCache "shmcb:/usr/local/apache/logs/ssl_scache(512000)"
SSLSessionCacheTimeout 300
Mutex sysvsem default
SSLStrictSNIVHostCheck on
EOF
并在對應apache虛擬主機配置文件的最后</VirtualHost>下面添加上SSL部分的配置文件:
<VirtualHost *:443>
DocumentRoot /home/wwwroot/www.0794baidu.com ? #網站目錄
ServerName www.0794baidu.com:443 ? #域名
ServerAdmin licess@vpser.net ? ? ?#郵箱
ErrorLog "/home/wwwlogs/www.0794baidu.com-error_log" ? #錯誤日志
CustomLog "/home/wwwlogs/www.0794baidu.com-access_log" common ? ?#訪問日志
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/www.0794baidu.com/fullchain.pem ? #改一下里面的域名就行
SSLCertificateKeyFile /etc/letsencrypt/live/www.0794baidu.com/privkey.pem ? ?#改一下里面的域名就行
<Directory "/home/wwwroot/www.0794baidu.com"> ? #網站目錄
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不行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
3、如果啟用了防火墻需要將443端口加入允許規則,一般iptables可以參考:http://www.0794baidu.com/security/linux-iptables.html
4、如果LNMP下訪問http://abc.com/.well-known/acme-challenge/**** 這個鏈接403的話是因為默認LNMP 1.3的虛擬主機里是禁止.開頭的隱藏文件及目錄的。需要將
location ~ /\.
{
deny all;
}
這段配置刪掉或注釋掉或在這段配置前面加上
location ~ /.well-known {
allow all;
}
以上配置代碼,然后重啟nginx。
證書續期
最后要說的是續期,因為證書只有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
/etc/init.d/nginx reload
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.0794baidu.com;
location /.well-known/ {
add_header Content-Type 'text/plain;';
root /網站目錄完整路徑;
}
location / {
return 301 http://www.0794baidu.com$request_uri;
}
}
目前就先說這些,有問題可以在本文章下部留言或到VPS論壇發帖。
VPS偵探論壇邀請碼:https://bbs.vpser.net/reg.php?invitecode=4957327b6f7rDwOe ?有效期至:2015-12-13 14:34
>>轉載請注明出處:VPS偵探 本文鏈接地址:http://www.0794baidu.com/build/letsencrypt-free-ssl.html











@lswim, 他們這個程序是自動安裝依賴的,應該是依賴沒安裝上
virtualenv: command not found
怎么辦?
@無名氏, 這就是HSTS存在的缺點,必須要訪問過https才能http強制到https
域名配置文件上加上了add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
為什么不自動從http跳轉到https?
@funbox, 2.6支持不好吧,建議2.7及以上版本,他這上面讓你加上--debug參數
WARNING: Python 2.6 support is very experimental at present...
if you would like to work on improving it, please ensure you have backups
and then run this script again with the --debug flag!
什么鬼~~
@大叔, /usr/local/nginx/sbin/nginx -t 測試配置
同時檢查是否開啟了防火墻,如果開啟了,將443端口加入允許規則
配置完為毛用https訪問打不開網頁,配置過程順利啊
@灰姑娘, 一個可能是超時,另外一個可能是CAA。dnspod目前可能支持了,據說是更新了CAA的應答請求。dnspod的人說的
“letsencrypt 在最終驗證時使用了隨機大小寫( 0x20 encode )方式進行 DNS 解析請求,以及請求 CAA 記錄(可以不支持)。 DNSPod 為了提高解析服務器的性能,以及應對隨機大小寫方式的 DNS FLOOD 攻擊,是沒有對該驗證方式進行支持的,一律返回小寫。我們正在對該問題進行驗證,驗證 OK 后將盡快更新支持 letsencrypt 。”
@PCDotFan, urlopen error [Errno -2] Name or service not known 好像是服務器上dns的問題,換一個dns試試
好不容易搞定了……結果第二步 urlopen error [Errno -2] Name or service not known
File "/tmp/acme_tiny.py", line 198, in
main(sys.argv[1:])
File "/tmp/acme_tiny.py", line 194, in main
signed_crt = get_crt(args.account_key, args.csr, args.acme_dir, log=LOGGER, CA=args.ca)
File "/tmp/acme_tiny.py", line 85, in get_crt
"agreement": "https://letsencrypt.org/documents/LE-SA-v1.0.1-July-27-2015.pdf",
File "/tmp/acme_tiny.py", line 47, in _send_signed_request
protected["nonce"] = urlopen(CA + "/directory").headers['Replay-Nonce']
File "/usr/lib64/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib64/python2.7/urllib2.py", line 404, in open
response = self._open(req, data)
File "/usr/lib64/python2.7/urllib2.py", line 422, in _open
'_open', req)
File "/usr/lib64/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/usr/lib64/python2.7/urllib2.py", line 1224, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "/usr/lib64/python2.7/urllib2.py", line 1186, in do_open
raise URLError(err)
urllib2.URLError:
@PCDotFan, 按這個提示是網絡不通,先后臺看看vps是否正常,不行找他們客服問問
軍哥,阿里云提示 Failed to establish a new connection: [Errno 101] Network is unreachable 。我連ss都讓服務器連上了還是不行……
“1、國內DNS服務商可能會不行,目前已知dnspod、cloudxns不行”
沒看懂這句什么意思、
據我所知,https和DNS沒啥關系。博主可否解釋下呢?
試了一下,firefox、chrome、ie都是綠的,不錯就是時間短點
好詳細,有空整下。
看來ssl以后是主流