LetsEncrypt SSL 證書籤發(Nginx)

概述

官方文檔參考: let's encrypt getting startedpython

域名認證過程有自動認證與手動認證,自動認證會啓動一個監聽 80 端口的程序來完成自動認證。手動認證使用參數 --webroot 來進行使用網站訪問手動認證,認證時,會訪問網址 /.well-known/acme-challenge/xxxxxxxxnginx

每次簽發的證書有 90 天的有效期,因此咱們還得在每月去從新簽發一個新的證書。git

本文的操做是基於系統 CentOS 6.7 操做進行。github

準備工做

在取得官方代碼前,得先查看系統環境中是否安裝全所須要的工具軟件。web

  • Git
yum -y install git
  • python 2.7 檢查
/usr/bin/python -V #查看版本
  • 安裝編譯須要的工具
yum install zlib-devel bzip2-devel openssl-devel xz-libs wget xz
  • 安裝 Python2.7.8
wget http://www.python.org/ftp/python/2.7.8/Python-2.7.8.tar.xz  
xz -d Python-2.7.8.tar #下載源碼  
tar -xvf Python-2.7.8.tar #解壓  
cd Python-2.7.8 #進入目錄  
./configure --prefix=/usr/local #運行配置
make  
make altinstall #編譯及安裝  
python2.7 -V #檢查版本  
export PATH="/usr/local/bin:$PATH"  
cd ../
  • 安裝 pip 及 virtualenv
wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-1.4.2.tar.gz #下載源碼  
tar -xvf setuptools-1.4.2.tar.gz #解壓  
cd setuptools-1.4.2  
python2.7 setup.py install #用 Python2.7.8安裝setuptools  
cd ../

curl https://bootstrap.pypa.io/get-pip.py | python2.7 -   #安裝pip  
pip2.7 install virtualenv   #安裝virtualenv

域名認證並生成證書

  • 從官方 Git 庫中取得代碼。
git clone https://github.com/letsencrypt/letsencrypt  
cd letsencrypt
  • 自動認證 standalone 模式

運行認證程序。bootstrap

service nginx stop #中止 Nginx 服務器  
./letsencrypt-auto certonly --standalone -d ixiaozhi.com -d www.ixiaozhi.com

letsencrypt-auto 按照提示輸入 E-mail 和域名便可。在運行認證程序前,要先停用 nginx,由於接下來的環節須要佔用80等端口。以後證書會生成到 /etc/letsencrypt/live/ixiaozhi.com/ 下,其中的 ixiaozhi.com 改成本身的域名。服務器

  • 手動認證 webroot 模式

Nginx 添加目錄訪問:dom

location /.well-known/acme-challenge/ {  
    default_type text/plain;
    root /home/ixiaozhi/acme-challenge/;
}

添加目錄,並重啓服務:python2.7

cd /home/ixiaozhi/  
mkdir acme-challenge  
service nginx restart

進行認證並生成證書:curl

service nginx reload

./letsencrypt-auto certonly --webroot -w /home/ixiaozhi/acme-challenge -d ixiaozhi.com -d www.ixiaozhi.com

認真閱讀輸出信息,輸入郵箱且贊成協議後,成功後會輸出:

IMPORTANT NOTES:  
 - Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/ixiaozhi.com/fullchain.pem. Your cert will
   expire on 2016-05-29. To obtain a new version of the certificate in
   the future, simply run Let's Encrypt again.
 - If you 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

看到 Congratulations 咱們就放心了。以後證書會生成到 /etc/letsencrypt/live/ixiaozhi.com/ 下。

配置 Nginx

修改 Nginx 的 nginx.conf,添加配置 ssl。

listen 80;  
listen 443 ssl;  
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;  
server_name ixiaozhi.com;

ssl_certificate     /etc/letsencrypt/live/ixiaozhi.com/fullchain.pem;  
ssl_certificate_key /etc/letsencrypt/live/ixiaozhi.com/privkey.pem;

重啓 nginx 便可。

定時任務定時簽發證書

咱們能夠先測試一個 renew 證書是否能夠成功。

./letsencrypt-auto renew --email admin@ixiaozhi.com --dry-run --agree-tos

當看到 Congratulations, all renewals succeeded. The following certs have been renewed時,測試就是經過的。使用 --dry-run 參數來測試並不會保存任何證書。

若是須要自動更新,先使用「crontab -e」,選擇編輯器後,在最底部加入

0 0 1 * * ./letsencrypt-auto renew --email admin@ixiaozhi.com --agree-tos --force-renewal

crontab 的時間格式爲:

*  *  *  *  *  command
分  時  日  月  周    命令

添加成功後,可使用 crontab -l 查看當前用戶的定時任務,確認是否已經生效。

轉發設置

若是但願訪問 http 都跳轉至 https 進行訪問,能夠經過兩種方法進行轉發。(若是是使用 --webroot 進行認證的,在 nginx 設置中要把 /.well-known/acme-challenge/ 例外不進行轉發)

一個是直接利用 nginx 進行轉發。

server {  
    listen 80;
    server_name ixiaozhi.com;
    return https://ixiaozhi.com$request_uri;

...

一個是設置HSTS。

server {  
    add_header Strict-Transport-Security "max-age=63072000;includeSubdomains; preload";  
    #添加一行
    ...

別忘了上述設置都須要重啓 Nginx。

相關文章
相關標籤/搜索