English |
  • 美国VPS主机推荐
  • |
  • 代购服务
  • |
  • 10美元以下VPS
  • |
  • VPS新手指南/教程
  • |
  • 留言板
  • |
  • 关于
  • | 登录 |

    免费SSL安全证书Let's Encrypt安装使用教程(附Nginx/Apache配置)

    2015年12月9日 上午 | 作者:VPS侦探

    letsencrypt

    Let's Encrypt是最近很火的一个免费SSL证书发行项目,Let's Encrypt是由ISRG提供的免费免费公益项目,自动化发行证书,但是证书只有90天的有效期。适合个人使用或者临时使用,不用再忍受自签发证书不受浏览器信赖的提示。前段时间一直是内测,现在已经开放了。本教程安装不需要停掉当前Web服务(Nginx/Apache),直接生成证书,废话不多说下面开始:

    目前已经更新为新工具certbot,使用教程:https://www.vpser.net/build/letsencrypt-certbot.html 配置文件和以前一样。
    建议使用git 以后有了新版更新方便,没安装的话Debian/Ubuntu:apt-get install git ,CentOS:yum install git-core
    git clone https://github.com/letsencrypt/letsencrypt.git
    cd letsencrypt

    不安装git的话:wget -c https://github.com/letsencrypt/letsencrypt/archive/master.zip && unzip master.zip && cd letsencrypt-master

    LNMP一键安装包都是Nginx/Apache默认支持ssl不需要另外单独编译,接下来先以LNMP一键安装包为例,LNMP用户可以直接参考此教程(LNMP v1.4自带此功能,可以直接使用lnmp ssl add (已经添加过该域名) 或lnmp vhost add (未添加过该域名)添加,1.3及之前用户可以升级到1.4,lnmp1.3升级1.4教程):

    执行:mkdir -p /home/wwwroot/域名/.well-known/acme-challenge 创建临时目录,当然这个.well-known/acme-challenge前面的目录要替换为你自己的网站目录,根据你自己的实际情况修改。

    正式开始生成证书

    接下来正式进行证书生成操作:
    ./letsencrypt-auto certonly --email 邮箱 -d 域名 --webroot -w /网站目录完整路径 --agree-tos
    如果多个域名可以加多个-d 域名,注意替换上面的邮箱、域名和网站目录,注意这里的网站目录完整路径只是你单纯的网站目录也就是虚拟主机配置文件里的,如Nginx虚拟主机配置里的root,Apache虚拟主机配置里的DocumentRoot。

    首先Let's Encrypt会检测系统安装一些依赖包,安装完依赖包会有蓝色的让阅读TOS的提示,Agree回车 稍等片刻就行了可添加--agree-tos参数屏蔽该窗口。
    生成证书后会有如下提示:

    IMPORTANT NOTES:
    - If you lose your account credentials, you can recover through
    e-mails sent to licess@vpser.net.
    - Congratulations! Your certificate and chain have been saved at
    /etc/letsencrypt/live/www.vpser.net/fullchain.pem. Your cert will
    expire on 2016-03-07. To obtain a new version of the certificate in
    the future, simply run Let's Encrypt again.
    - 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 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

    Nginx虚拟主机的设置

    接下来进行配置Nginx虚拟主机文件,LNMP模式下完整配置如下:

    server
    {
    listen 443 ssl;   #lnmp1.3+版本默认支持http2,可以加上http2,lnmp1.2版本默认支持spdy,可以加上spdy
    server_name www.vpser.net;     ##这里是你的域名
    index index.html index.htm index.php default.html default.htm default.php;
    root /home/wwwroot/www.vpser.net;            #网站目录
    ssl_certificate /etc/letsencrypt/live/www.vpser.net/fullchain.pem;    #前面生成的证书,改一下里面的域名就行
    ssl_certificate_key /etc/letsencrypt/live/www.vpser.net/privkey.pem;   #前面生成的密钥,改一下里面的域名就行
    SSLCertificateChainFile /etc/letsencrypt/live/www.vpser.net/chain.pem; #Apache 2.2版本需要加入该中间证书,否则浏览器可能不信任
    ssl_ciphers "EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;

    include wordpress.conf;  #这个是伪静态根据自己的需求改成其他或删除
    #error_page 404 /404.html;
    location ~ [^/]\.php(/|$)
    {
    # comment try_files $uri =404; to enable pathinfo
    try_files $uri =404;
    fastcgi_pass unix:/tmp/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;     #lnmp 1.0及之前版本替换为include fcgi.conf;
    #include pathinfo.conf;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
    expires 30d;
    }

    location ~ .*\.(js|css)?$
    {
    expires 12h;
    }

    access_log off;
    }

    需将上述配置根据自己的实际情况修改后,添加到虚拟主机配置文件最后面。

    添加完需要执行:/etc/init.d/nginx reload 重新载入配置使其生效。

    不要轻易添加HSTS,如果需要HSTS,可以加上add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"; 前面参数里的数值是秒。 开启后有效期内只能https访问而且所有二级域名也都是强制https,不确定的话不要开启。

    LNMPA模式下只需要在Nginx上设置,只需要将location ~ [^/]\.php(/|$) { 到下一个 } 的php解析部分替换为 include proxy-pass-php.conf; 即可。

    Apache虚拟主机上的设置

    LAMP模式按下面配置文件。Apache在生成证书后也需要修改一下apache的配置文件 /usr/local/apache/conf/httpd.conf ,查找httpd-ssl将前面的#去掉。

    然后再执行:

    Apache 2.2如下:
    cat >/usr/local/apache/conf/extra/httpd-ssl.conf<<EOF
    Listen 443

    AddType application/x-x509-ca-cert .crt
    AddType application/x-pkcs7-crl .crl

    SSLCipherSuite EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5
    SSLProxyCipherSuite EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5
    SSLHonorCipherOrder on

    SSLProtocol all -SSLv2 -SSLv3
    SSLProxyProtocol all -SSLv2 -SSLv3
    SSLPassPhraseDialog builtin

    SSLSessionCache "shmcb:/usr/local/apache/logs/ssl_scache(512000)"
    SSLSessionCacheTimeout 300

    SSLMutex "file:/usr/local/apache/logs/ssl_mutex"

    SSLStrictSNIVHostCheck on
    NameVirtualHost *:443
    EOF

    Apache 2.4如下:

    cat >/usr/local/apache/conf/extra/httpd-ssl.conf<<EOF
    Listen 443

    AddType application/x-x509-ca-cert .crt
    AddType application/x-pkcs7-crl .crl

    SSLCipherSuite EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5
    SSLProxyCipherSuite EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5
    SSLHonorCipherOrder on

    SSLProtocol all -SSLv2 -SSLv3
    SSLProxyProtocol all -SSLv2 -SSLv3
    SSLPassPhraseDialog builtin

    SSLSessionCache "shmcb:/usr/local/apache/logs/ssl_scache(512000)"
    SSLSessionCacheTimeout 300

    Mutex sysvsem default

    SSLStrictSNIVHostCheck on
    EOF

    并在对应apache虚拟主机配置文件的最后</VirtualHost>下面添加上SSL部分的配置文件:

    <VirtualHost *:443>
    DocumentRoot /home/wwwroot/www.vpser.net   #网站目录
    ServerName www.vpser.net:443   #域名
    ServerAdmin licess@vpser.net      #邮箱
    ErrorLog "/home/wwwlogs/www.vpser.net-error_log"   #错误日志
    CustomLog "/home/wwwlogs/www.vpser.net-access_log" common    #访问日志
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/www.vpser.net/fullchain.pem   #改一下里面的域名就行
    SSLCertificateKeyFile /etc/letsencrypt/live/www.vpser.net/privkey.pem    #改一下里面的域名就行
    <Directory "/home/wwwroot/www.vpser.net">   #网站目录
    SetOutputFilter DEFLATE
    Options FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
    DirectoryIndex index.html index.php
    </Directory>
    </VirtualHost>

    需将上述配置根据自己的实际情况修改后,添加到虚拟主机配置文件最后面。注意要重启apache使其实现。执行:/etc/init.d/httpd restart 重启Apache使其生效。

    下面说一下可能会遇到的问题:

    1、国内DNS服务商可能会不行,目前已知dnspod、cloudxns不行dnspod、cloudxns目前已经可以

    Namecheap、Route 53的都可以。

    2、Linode福利或IPv6用户福利

    可能目前Linode用户应该遇到了
    An unexpected error occurred:
    There were too many requests of a given type :: Error creating new registration :: Too many registrations from this IP
    Please see the logfiles in /var/log/letsencrypt for more details.
    这个不一定是因为IP注册的次数过多,可能是因为IPv6的事,具体解决方法如下:
    执行:sysctl -w net.ipv6.conf.all.disable_ipv6=1 来临时禁用IPv6
    再生成证书后执行:sysctl -w net.ipv6.conf.all.disable_ipv6=0 再来解除禁用IPv6

    3、如果启用了防火墙需要将443端口加入允许规则,一般iptables可以参考:https://www.vpser.net/security/linux-iptables.html

    4、如果LNMP下访问http://abc.com/.well-known/acme-challenge/**** 这个链接403的话是因为默认LNMP 1.3的虚拟主机里是禁止.开头的隐藏文件及目录的。需要将
    location ~ /\.
    {
    deny all;
    }
    这段配置删掉或注释掉或在这段配置前面加上

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

    以上配置代码,然后重启nginx。

    证书续期

    最后要说的是续期,因为证书只有90天,所以建议60左右的时候进行一次续期,续期很简单可以交给crontab进行完成,执行:

    cat >/root/renew-ssl.sh<<EOF
    #!/bin/bash
    mkdir -p /网站目录完整路径/.well-known/acme-challenge
    /root/letsencrypt/letsencrypt-auto --renew-by-default certonly --email 邮箱 -d 域名 --webroot -w /网站目录完整路径 --agree-tos
    /etc/init.d/nginx reload
    EOF
    chmod +x /root/renew-ssl.sh

    注意要修改上面letsencrypt-auto的路径为你自己的,并且里面的邮箱和域名也要修改。
    再crontab里添加上:0 3 */60 * * /root/renew-ssl.sh 具体crontab教程点击查看

    2015.12.13更新
    官网更新了参数,对本文进行了部分参数的调整。原-a webroot --webroot-path=/网站目录完整路径替换为--webroot -w;--renew替换为--renew-by-default;增加--agree-tos参数。

    同时这里提醒一下如果设置了http 301跳到https的用户,再续期前还需要在nginx设置如下:
    80端口的虚拟主机上需要添加上,不加的话会无法验证的

    location /.well-known/ {
    add_header Content-Type 'text/plain;';
    root /网站目录完整路径;
    }

    附完整的nginx下301 http跳到https的配置:

    server
    {
    listen 80;
    server_name www.vpser.net;
    location /.well-known/ {
    add_header Content-Type 'text/plain;';
    root /网站目录完整路径;
    }
    location / {
    return 301 https://www.vpser.net$request_uri;
    }
    }

    目前就先说这些,有问题可以在本文章下部留言或到VPS论坛发帖。

    VPS侦探论坛邀请码:https://bbs.vpser.net/reg.php?invitecode=4957327b6f7rDwOe  有效期至:2015-12-13 14:34

    >>转载请注明出处:VPS侦探 本文链接地址:https://www.vpser.net/build/letsencrypt-free-ssl.html
    VPS侦探推荐:
    遨游主机VultrLinode搬瓦工LOCVPSKVMLAHOSTKVMHostXen80VPS美国VPS主机,国内推荐腾讯云阿里云
    欢迎加入VPS侦探论坛交流:https://bbs.vpser.net

    发表评论

    *必填

    *必填 (不会被公开)

    评论(70条评论)

    1. VPS侦探说道:

      @lswim, 他们这个程序是自动安装依赖的,应该是依赖没安装上

    2. lswim说道:

      virtualenv: command not found
      怎么办?

    3. VPS侦探说道:

      @无名氏, 这就是HSTS存在的缺点,必须要访问过https才能http强制到https

    4. 无名氏说道:

      域名配置文件上加上了add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";

      为什么不自动从http跳转到https?

    5. VPS侦探说道:

      @funbox, 2.6支持不好吧,建议2.7及以上版本,他这上面让你加上--debug参数

    6. funbox说道:

      WARNING: Python 2.6 support is very experimental at present...
      if you would like to work on improving it, please ensure you have backups
      and then run this script again with the --debug flag!

      什么鬼~~

    7. VPS侦探说道:

      @大叔, /usr/local/nginx/sbin/nginx -t 测试配置
      同时检查是否开启了防火墙,如果开启了,将443端口加入允许规则

    8. 大叔说道:

      配置完为毛用https访问打不开网页,配置过程顺利啊

    9. VPS侦探说道:

      @灰姑娘, 一个可能是超时,另外一个可能是CAA。dnspod目前可能支持了,据说是更新了CAA的应答请求。dnspod的人说的
      “letsencrypt 在最终验证时使用了随机大小写( 0x20 encode )方式进行 DNS 解析请求,以及请求 CAA 记录(可以不支持)。 DNSPod 为了提高解析服务器的性能,以及应对随机大小写方式的 DNS FLOOD 攻击,是没有对该验证方式进行支持的,一律返回小写。我们正在对该问题进行验证,验证 OK 后将尽快更新支持 letsencrypt 。”

    10. VPS侦探说道:

      @PCDotFan, urlopen error [Errno -2] Name or service not known 好像是服务器上dns的问题,换一个dns试试

    11. PCDotFan说道:

      好不容易搞定了……结果第二步 urlopen error [Errno -2] Name or service not known
      File "/tmp/acme_tiny.py", line 198, in
      main(sys.argv[1:])
      File "/tmp/acme_tiny.py", line 194, in main
      signed_crt = get_crt(args.account_key, args.csr, args.acme_dir, log=LOGGER, CA=args.ca)
      File "/tmp/acme_tiny.py", line 85, in get_crt
      "agreement": "https://letsencrypt.org/documents/LE-SA-v1.0.1-July-27-2015.pdf",
      File "/tmp/acme_tiny.py", line 47, in _send_signed_request
      protected["nonce"] = urlopen(CA + "/directory").headers['Replay-Nonce']
      File "/usr/lib64/python2.7/urllib2.py", line 127, in urlopen
      return _opener.open(url, data, timeout)
      File "/usr/lib64/python2.7/urllib2.py", line 404, in open
      response = self._open(req, data)
      File "/usr/lib64/python2.7/urllib2.py", line 422, in _open
      '_open', req)
      File "/usr/lib64/python2.7/urllib2.py", line 382, in _call_chain
      result = func(*args)
      File "/usr/lib64/python2.7/urllib2.py", line 1224, in https_open
      return self.do_open(httplib.HTTPSConnection, req)
      File "/usr/lib64/python2.7/urllib2.py", line 1186, in do_open
      raise URLError(err)
      urllib2.URLError:

    12. VPS侦探说道:

      @PCDotFan, 按这个提示是网络不通,先后台看看vps是否正常,不行找他们客服问问

    13. PCDotFan说道:

      军哥,阿里云提示 Failed to establish a new connection: [Errno 101] Network is unreachable 。我连ss都让服务器连上了还是不行……

    14. 灰姑娘说道:

      “1、国内DNS服务商可能会不行,目前已知dnspod、cloudxns不行”
      没看懂这句什么意思、

      据我所知,https和DNS没啥关系。博主可否解释下呢?

    15. jack说道:

      试了一下,firefox、chrome、ie都是绿的,不错就是时间短点

    16. Andrew说道:

      好详细,有空整下。
      看来ssl以后是主流

    1 2 3 5