启用https的安全连接基于LetsEncrypt SSL的nginx配置

星期四, 2016-05-05 | Author: Lee | JAVA-and-J2EE, linux | 3,857 views

现在网站不是https都不好意思和别人说了,顺便也跟下潮流.

操作系统:Centos6.5版本

官方文档参考: let’s encrypt getting started

具体介绍就不废话了,知道是免费、时效是90天即可,记得及时自动续期就好.

一.系统环境配置

Git

1
yum -y install git

python 2.7 检查

1
/usr/bin/python -V #查看版本

安装编译需要的工具

1
yum install zlib-devel bzip2-devel openssl-devel xz-libs wget xz

安装 Python2.7.8

1
2
3
4
5
6
7
8
9
10
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

1
2
3
4
5
6
7
8
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 库中取得代码

1
2
git clone https://github.com/letsencrypt/letsencrypt  
cd letsencrypt

自动认证 standalone 模式(不推荐,需要停止服务器)
运行认证程序

1
2
service nginx stop #停止 Nginx 服务器  
./letsencrypt-auto certonly --standalone -d iatodo.com -d www.iatodo.com

letsencrypt-auto 按照提示输入 E-mail 和域名即可。在运行认证程序前,要先停用 nginx,因为接下来的环节需要占用80等端口。之后证书会生成到 /etc/letsencrypt/live/iatodo.com/ 下,其中的 iatodo.com 改为自己的域名

手动认证 webroot 模式(推荐)

Nginx 添加目录访问:

1
2
3
4
location /.well-known/acme-challenge/ {  
    default_type text/plain;
    root /home/iatodo/acme-challenge/;
}

添加目录:

1
mkdir -p /home/iatodo/acme-challenge

进行认证并生成证书

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

认真阅读输出信息,输入邮箱且同意协议后,成功后会输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/iatodo.com/fullchain.pem. Your cert will
   expire on 2016-08-02. To obtain a new version of the certificate in
   the future, simply run Let's Encrypt again.
 - If you lose your account credentials, you can recover through
   e-mails sent to [email protected].
 - 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 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/iatodo.com/ 下

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

1
2
3
4
5
6
7
listen 80;  
listen 443 ssl;  
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;  
server_name iatodo.com;
 
ssl_certificate     /etc/letsencrypt/live/iatodo.com/fullchain.pem;  
ssl_certificate_key /etc/letsencrypt/live/iatodo.com/privkey.pem

开放443端口
设置防火墙
你现在应该拥有一个以自我签署凭证来支持 https 的网站。如果你未能连接,你或许需要在防火墙上打开端口。要这样做,请更改你的 iptables 规则:

1
2
3
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
/sbin/service iptables save
iptables -L -v

重启 nginx 即可

四:定时任务定时签发证书
我们可以先测试一个 renew 证书是否可以成功

1
./letsencrypt-auto renew --email service@i5a6.com --dry-run --agree-tos

当看到 Congratulations, all renewals succeeded. The following certs have been renewed 时,测试就是通过的。使用 –dry-run 参数来测试并不会保存任何证书。
如果需要自动更新,先使用“crontab -e”,选择编辑器后,在最底部加入(每月执行一次续期即可) 需要启用4096的密钥可以参数即可( –rsa-key-size 4096)

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

crontab 的时间格式为

1
2
*  *  *  *  *  command
分  时  日  月  周    命令

添加成功后,可以使用 crontab -l 查看当前用户的定时任务,确认是否已经生效。

五:转发设置
如果希望访问 http 都跳转至 https 进行访问,可以通过两种方法进行转发。(如果是使用 –webroot 进行认证的,在 nginx 设置中要把 /.well-known/acme-challenge/ 例外不进行转发)

一个是直接利用 nginx 进行转发

1
2
3
4
5
6
server {  
    listen 80;
    server_name iatodo.com;
    return https://www.iatodo.com$request_uri;
 
...

一个是设置HSTS (推荐)

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

要达到检测的A+ 标准见nginx的配置:nginx配置https使其达到A+水平

Tags: , ,

文章作者: Lee

本文地址: https://www.pomelolee.com/1562.html

除非注明,Pomelo Lee文章均为原创,转载请以链接形式标明本文地址

2 Comments to 启用https的安全连接基于LetsEncrypt SSL的nginx配置

neil
2016 年 05 月 06 日

写的不错,表扬下!

Lee
2017 年 05 月 18 日

提速证书的访问:
生成 dhparam.pem

$ openssl dhparam -out dhparam.pem 4096
配置到 nginx

ssl_dhparam /etc/ssl/certs/dhparam.pem;

Leave a comment

Search

文章分类

Links

Meta