天天看点

Centos中使用Let's Encrypt配置SSL证书

Centos中使用Let's Encrypt配置SSL证书

Let’s Encrypt 介绍

Let’s Encrypt 是一个免费、开放,自动化的证书颁发机构,由 ISRG(Internet Security Research Group)运作。

ISRG 是一个关注网络安全的公益组织,其赞助商从非商业组织到财富100强公司都有,包括 Mozilla、Akamai、Cisco、Facebook,密歇根大学等等。ISRG 以消除资金,技术领域的障碍,全面推进加密连接成为互联网标配为自己的使命。

Let’s Encrypt 项目于2012年由 Mozilla 的两个员工发起,2014年11年对外宣布公开,2015年12月3日开启公测。

Let’s Encrypt 验证方式

Let’s Encrypt 使用两种方式对申请的域名进行验证:

  1. 手动验证 按照提示在申请证书的服务器上使用一个指定的URL提供一个指定的文件内容来进行验证,进行手动验证的服务器IP地址会被 Let’s Encrypt 服务端记录在案。
  2. 自动验证 在目标服务器 (指域名解析对应的IP地址的服务器,下同)上运行客户端,并启动一个 80 或 443 端口进行自动验证。包括独立模式(standalone)和其他web sever验证模式

Let’s Encrypt 安装

本文记录在目标服务器上采用Standalone方式进行自动验证的安装过程。

在安装之前,先介绍几个概念,防止一头蒙圈。

Standalone

使用独立模式进行自动验证,需要在 目标服务器 上运行 Let’s Encrypt 客户端,并指定 certonly 和 –standalone参数。本模式需要绑定 80 或 443 端口进行域名验证,所以如果服务器上已有web server运行并侦听这2个端口,则需要先关闭web server。

Webroot

如果 目标服务器 已有web server运行,并且不能够关闭服务来获取和安装证书,可以使用 Webroot plugin。在运行 Let’s Encrypt 客户端时指定 certonly 和 –webroot 参数,并使用 –webroot-path 或 -w 参数来指定 webroot 目录,比如 –webroot-path /usr/share/nginx/html

安装过程

1、由于操作系统为centos,所以安装必要的准备:

[root@localhost src]# yum install epel-release
[root@localhost src]# wget https://dl.eff.org/certbot-auto
[root@localhost src]# chmod a+x certbot-auto           

2、安装并生成ssl证书文件

[root@localhost src]# ./certbot-auto certonly --standalone -d renrenshi.me -d www.renrenshi.me           

3、证书文件说明

所有版本已申请的证书放在

/etc/letsencrypt/archive

下,

/etc/letsencrypt/live

是指向最新版本的符号链接。

web server中关于证书的配置建议指向 live 目录下的文件,以免证书更新后还需要更改配置。

每个域名一个目录,主要包含以下几个文件:

  • cert.pem 申请的服务器域名证书文件
  • privkey.pem 服务器域名证书对应的私钥
  • chain.pem 除服务器证书外,浏览器解析所需的其他全部证书,比如根证书和中间证书
  • fullchain.pem 包含服务器证书的全部证书链文件

4、nginx配置证书文件

生成dhparams.pem:

[root@localhost conf]#cd cd /etc/ssl/certs/
[root@localhost certs]# openssl dhparam -out /etc/ssl/certs/dhparams.pem 2048           

打开配置文件

[root@localhost conf]# vim /usr/local/nginx/conf/vhosts/xxx.conf           

编辑xxx.conf文件,开放443端口:

server {

        listen 443 ssl;
        server_name www.xxx.com xxx.com;

        ssl_certificate /etc/letsencrypt/live/xxx.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/xxx.com/privkey.pem;
        ssl_dhparam /etc/ssl/certs/dhparams.pem;
        ssl_prefer_server_ciphers  on;

        location / {
                index index.php index.html index.shtml;
                proxy_pass http://127.0.0.1:4000;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real_IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        #error_page  404              /404.html;
        #
        # redirect server error pages to the static page /50x.html
        #        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
                    root   /usr/share/nginx/html;
        }
}


#Http server
server {
    listen       80;
    server_name  xxx.com www.xxx.com;
    return 301 https://$server_name$request_uri;
}
           

5、检查nginx配置

[[email protected] ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
           

6、启动nginx

[root@localhost ~]# /usr/local/nginx/sbin/nginx           

7、访问并检查https是否成功

Centos中使用Let's Encrypt配置SSL证书

参考链接

Let’s Encrypt SSL证书配置:http://www.jianshu.com/p/eaac0d082ba2

官方配置介绍certbot:https://certbot.eff.org/#centosrhel6-nginx

Let’s Encrypt官网:https://letsencrypt.org/

继续阅读