资讯专栏INFORMATION COLUMN

letsencrypt在nginx下的配置

YacaToy / 979人阅读

摘要:因为是在网站上看到有提供免费的证书,因为决定在上安装试用一下。因为我不想中断服务,所以我手动把停用,把以前备用的一个启动起来,占住端口以提供服务。更便捷的方法,请参考

因为是在segmentfault网站上看到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

其余步骤同上。


更便捷的方法,请参考https://segmentfault.com/a/11...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/39288.html

相关文章

  • Docker Compose 整合发布应用相关服务

    摘要:于是,程序不再是原先单一的服务,而是,变成了一系列密切相关的服务。需要注意的是,在模式下申请证书,需要向证明服务器能被访问。 首先,祝各位新年快乐,万事如意,鸡年大吉。 这次要来说说一个和前端并不太相关的东西——docker compose,一个整合发布应用的利器。 如果,你对 docker 有一些耳闻,那么,你可能知道它是什么。 不过,你不了解也没有关系,在作者眼中,docker 就...

    microcosm1994 评论0 收藏0
  • Docker Compose 整合发布应用相关服务

    摘要:于是,程序不再是原先单一的服务,而是,变成了一系列密切相关的服务。需要注意的是,在模式下申请证书,需要向证明服务器能被访问。 首先,祝各位新年快乐,万事如意,鸡年大吉。 这次要来说说一个和前端并不太相关的东西——docker compose,一个整合发布应用的利器。 如果,你对 docker 有一些耳闻,那么,你可能知道它是什么。 不过,你不了解也没有关系,在作者眼中,docker 就...

    newtrek 评论0 收藏0
  • Let's Encrypt 安装配置教程,免费的 SSL 证书

    摘要:官网安装安装非常简单直接克隆就可以了生成通配符证书期间需要根据提示设置记录用作你对判断你是否拥有域名使用权其中换成你的一级域名即可参数说明表示安装模式,有安装模式和验证模式两种类型的插件。 官网:https://letsencrypt.org/ 安装Lets Encrypt 安装非常简单直接克隆就可以了 git clone https://github.com/letsencrypt/...

    YanceyOfficial 评论0 收藏0
  • Amazon Linux 上使用 Let's encrypt 免费的SSL

    摘要:在上使用免费的如果你使用来做负载均衡,在上可以很方便的使用。提供期限为三个月的免费证书,到期之后需要,官方还提供自动的工具是一个自动申请和续期证书的工具。在官网可以找到各种和服务器下的安装方法。常见的和安装起来十分方便。 在Amazon Linux 上 使用 Lets encrypt 免费的SSL 如果你使用ELB来做负载均衡,在AWS上可以很方便的使用SSL。如果不使用ELB就需要自...

    coolpail 评论0 收藏0
  • 升级你的hexo为https

    摘要:可以使用多个添加多个域名可以帮我们自动注册证书,是静态资源所指的路径。输入命令之后选择即可,后续将会让你继续输入邮箱信息如果出现字样,则证明证书已被自动注册。 本文以Debian 8为服务器栗子 最近升级了个人博客为https协议,写一个详细的教程帮助更多人升级https。 https的详细原理在此文中省略,简略来说,既是客户端在访问服务器时需要一个数字证书(里面包括公钥),它由权威机...

    james 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<