使用 Let’s Encrypt 提供的免费证书

准备

安装 Let’s Encrypt

git clone https://github.com/letsencrypt/letsencrypt
cd letsencrypt
./letsencrypt-auto
ln -s /path/to/git/clone/letsencrypt-auto /usr/local/bin/letsencrypt

将要申请证书的域名解析到执行操作的主机

配置

vi /etc/letsencrypt/cli.ini

写入以下内容,根据情况进行修改,查看帮助:./letsencrypt-auto –help

rsa-key-size = 4096
email = root@example.com
domains = example.com 
domains = www.example.com
text = True
authenticator = standalone
standalone-supported-challenges = http-01

生成证书

letsencrypt certonly

配置 nginx

server {
    listen 80;
    server_name example.com www.example.com;
    location / {
        return 301 https://$server_name$request_uri;
    }
}
server {
    listen       443 ssl spdy;
    server_name  example.com www.example.com;

    # certificates from letsencrypt
    ssl_certificate      /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;

    # Diffie-Hellman parameter for DHE ciphersuites
    # ssl_dhparam /etc/nginx/dhparam.pem;

    # modern configuration
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!3DES:!MD5:!PSK';
    ssl_prefer_server_ciphers on;

    # HSTS (ngx_http_headers required) - 6 months
    # add_header Strict-Transport-Security max-age=15768000;

    # OCSP stapling
    ssl_stapling on;
    ssl_stapling_verify on;

    # verify chain of trust of OCSP response
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;

    location / {
        root /data/web/example_com;
    }
}

过期处理

Let’s Encrypt 提供的证书有效期为3个月,即将到期时只需简单操作即可:

service nginx stop # 停止 nginx,释放80端口给认证程序
letsencrypt certonly --renew
service nginx start

或计划任务:

crontab -e
@monthly service nginx stop && letsencrypt certonly && service nginx start