centos6.5+nginx+letsencrypt+ssl

letsencrypt有提供免费的ssl证书,因为决定在CentOS上安装试用一下。

安装过程很简单,按照教程一步步来就能搞定:

$ git clone https://github.com/certbot/certbot
$ cd certbot
$ ./certbot-auto --help

但是教程的下一步就有问题了,安装完之后的目录下并没有certbot这个可执行文件,而只有certbot-auto,但其实它们两个是一回事,直接用就可以。

当我执行./certbot-auto时,出现了以下错误:

Error:  Multilib version problems found. This often means that the root
       cause is something else and multilib version checking is just
       pointing out that there is a problem. Eg.:

         1. You have an upgrade for openssl which is missing some
            dependency that another package requires. Yum is trying to
            solve this by installing an older version of openssl of the
            different architecture. If you exclude the bad architecture
            yum will tell you what the root cause is (which package
            requires what). You can try redoing the upgrade with
            --exclude openssl.otherarch ... this should give you an error
            message showing the root cause of the problem.

         2. You have multiple architectures of openssl installed, but
            yum can only see an upgrade for one of those arcitectures.
            If you don't want/need both architectures anymore then you
            can remove the one with the missing update and everything
            will work.

         3. You have duplicate versions of openssl installed already.
            You can use "yum check" to get yum show these errors.

感觉上好像是openssl版本不匹配,于是执行

yum update openssl

然后再次执行./certbot-auto,这次就没问题了。

先退出界面,然后执行

./certbot-auto --help

这次发现多了一些内容。然后执行:

./certbot-auto certonly --standalone -d www.myserver.com

因为是standalone,它试图在80端口上启动一个服务器,但是因为80端口已经被nginx占用,所以执行不成功,需要暂时停用一下nginx。因为我不想中断服务,所以我手动把nginx停用,把以前备用的一个apache启动起来,占住80端口以提供服务。这样我就不再需要standalone参数,而可以使用apache参数了,如下:

./certbot-auto certonly --apache -d www.myserver.com

但又出现了错误,它在443的虚拟主机上找不到我的服务器,原来我只在80端口上配置了虚拟主机,于是在Apache的conf文件上胡乱配上一个虚拟主机,以便使用443端口。但还是连接不通。报如下错误:

 - The following errors were reported by the server:

   Domain: www.myserver.com
   Type:   connection
   Detail: Failed to connect to host for DVSNI challenge

仔细一想,原来是我在防火墙上把443端口禁用了,打开443端口后,终于成功!

 - Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/www.myserver.com/fullchain.pem. Your
   cert will expire on 2016-08-15. To obtain a new version of the
   certificate in the future, simply run Certbot again.

接下来,你会在上述目录下看到4个文件:cert.pem@ chain.pem@ fullchain.pem@ privkey.pem@

这4个文件里,我们在nginx配置中只会用到后2个,因为fullchain.pem就相当于cert.pem+chain.pem。

nginx的配置如下:

server {
    listen       443;
    server_name  www.myserver.com;
    root   /var/www/html;

    ssl                  on;
    ssl_certificate      /etc/letsencrypt/live/www.myserver.com/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/www.myserver.com/privkey.pem;

    location / {
        index  index.php index.html index.htm;
    }

    location ~ /\. {
        return 403;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

最后还要记得配置80端口,这样它才会强行把所有指向80端口的http链接转变为https请求:

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

到止为止,重启nginx,终于可以在浏览器端看见那个漂亮的绿色小锁头了!


2016年6月9日补充:

其实在nginx下配置letsencrypt远没有那么麻烦,首先需要在ini文件中的server块中添加如下设置:

location ~ /.well-known {
    allow all;
}

主要目的是因为letsencrypt在验证时需要往这个文件夹下写文件验证,但其实你自己不必创建这个文件夹。

然后你再执行如下语句:

./letsencrypt-auto certonly -a webroot --webroot-path=/var/www/html -d www.example.com

其余步骤同上。

Let’s Encrypt是EFF、Mozilla、Cisco、Akamai、IdenTrust与密西根大学研究人员共同创立的组织,这是一个免费的凭证中心(Certification Authority,CA),目的在于推动全球所有的网站都使用HTTPS加密传输,并由非营利的网际网路安全研究组织Internet Security Research Group(ISRG)负责营运。

对于开发者来说,部署HTTPS是一个双赢的选择,用户可以借此获得更加安全的使用体验,而站点也能够抵御恶意软件的注入或广告追踪。Let’s Encrypt已于2015年12月3日进入公测(Public Beta),所有网站都可以免费获取Let’s Encrypt的证书。

据说Google优先收录带HTTPS加密的网站。

获取Let’s Encrypt证书

项目GitHub地址:https://github.com/letsencrypt/letsencrypt – > https://github.com/certbot/certbot

官方教程:https://certbot.eff.org/docs/using.html

2016年5月更新:

最近官方做了调整,简化了获取证书的难度,并将项目名改为了certbot,以下为全新安装方法:

等待安装依耐环境并初始化,然后直接看第四步。

以前的获取方法

4.运行客户端获取证书

获取证书有几种方案,官方已经开发了用于Apache服务器的插件,而Nginx的插件还是实验性功能。本文不探讨使用插件的方式,读者可以自行阅读官方的User Guide。

下面是两种通用获取方法,推荐使用 方案二

方案一)获取证书时会使用80端口,如果你的服务器可以关闭,可以使用如下方式:

关闭占用80端口的服务器,比如Apache或者Nginx

输入以下命令来获取证书:

-d 后面为需要获取证书的域名,申请完成后所有域名共用一个证书。

如果申请成功,会显示以下信息,可惜的是获取一次有效期只有90天,到期后需要续签:

证书文件放在了/etc/letsencrypt/live下。

方案二)如果你不想关闭服务器,也可以指定webroot来获取证书:

–server: 指定 letsencrypt 服务器,-w: 指定网站根目录,-d: 指定网站域名。用 , 分开同一个网站绑定的所有域名

PS:如果你的域名使用的是国内的NS服务商,比如:hichina、dnspod、cloudxns,可能不能正常通过验证,见这条 Issue:DNS CAA query timeout

错误提示类似于:

解决方案是将域名的NS服务商换为国外的,例如namecheap、linode、cloudflare、google cloud DNS。

你可以先尝试是否能正常获取,如果不能的话,可以选择切换域名的NS服务器。我在操作的时候将域名的NS换到了 https://www.cloudflare.com,但是换了NS需要等几个小时才能生效。

配置Nginx添加Https

添加一个网站:

以上设置会将所有的 Http 请求通过301重定向到 Https。

更新证书的时候,letsencrypt 会通过 http 协议访问你网站目录下的一个文件,请求类似于:

而通过上述设置,可以让服务器上的所有网站证书更新时使用同一个目录,而不用分别去配置每个网站的目录。

打开网站看看,已经加上绿色锁头了,可是如果网页上存在有没走Https的链接,就不能全绿。

给博客加把小绿锁,Let's Encrypt免费HTTPS SSL证书获取教程 - 第3张  | 扩软博客

Let’s Encrypt续签

新版续签只需要一条命令:

或者:

你可以选择添加一个计划任务,自动续签。

优化Https安全性

各服务器推荐配置可以参考:https://cipherli.st/

1.首先生成dhparam.pem:

我用的是2048bit加密,当然如果你服务器配置够好,可以选4096bit。

2.生成HTTP Public Key Pinning 参考:Public Key Pinning

在这里直接贴上我的配置:

配置完之后可以测试一下现在的安全级别:https://www.ssllabs.com/ssltest/

给博客加把小绿锁,Let's Encrypt免费HTTPS SSL证书获取教程 - 第4张  | 扩软博客

HTTP Security Report:https://httpsecurityreport.com/

配置参考地址:

1.Configuring Let’s Encrypt for nginx with Automatic Renewal

2.Strong SSL Security on nginx

3.使用 Nginx 构建优化的 https 服务

将网站加入HSTS preload list

HSTS preload list是什么?

HSTS preload list是Chrome浏览器中的HSTS预载入列表,在该列表中的网站,使用Chrome浏览器访问时,会自动转换成HTTPS。Firefox、Safari、Edge浏览器也会采用这个列表。

加入地址:https://hstspreload.appspot.com

加入条件:

  1. 有效的证书;
  2. 将所有HTTP流量重定向到HTTPS;
  3. 确保所有子域名启用HTTPS,特别是www子域名;

同时,HSTS响应头需要满足以下条件。

  1. max-age至少需要18周,10886400秒;
  2. 必须指定includeSubdomains参数;
  3. 必须支持preload参数。

例如:

从申请到审核通过,时间在几天到几周不等。申请后,你可以在 https://hstspreload.appspot.com 网站中查询最新的状态, 也可以在Chrome浏览器的地址框中输入“chrome://net-internals/#hsts”查看。

从审核通过到正式加入到 Chrome 的 stable release 版本中还需要一段时间,因为要经过 canary、dev、beta 以及 stable。

这个文件就是 Chrome 的预加载列表文件:transport_security_state_static.json

参考地址:解决缺陷,让HSTS变得完美

以下为letsencrypt-auto --help all的输出信息:

点击显示隐藏内容

链接:

1.Let’s Encrypt User Guide

2.V2EX我的 Let’s Encrypt 证书也下来了

3.折腾Let‘s Encrypt免费SSL证书

4.One-line certificate generation/renews with Letsencrypt and nginx


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值