STUDY WHILE YOU ARE BORING


  • 首頁

  • 搜索

  • 技术

  • Tags

  • 关于我

申请免费的SSL证书构建HTTPS站点并自动续订

發表於 May 28, 2019   |   作者: Tim   |   分类: 技术   |   评论

为什么要SSL证书?

安全,普通的HTTP网站传输是明文传输的,内容很容易被窃取或者劫持。HTTPS使用SSL证书,传输加密,安全性更高。SSL证书通过在客户端浏览器和web浏览器之间建立一条SSL安全通道(Secure socket layer(SSL),对传送的数据进行加密和隐藏;确保数据在传送中不被改变,保证数据的完整性,现已成为该领域中全球化的标准。由于SSL技术已建立到所有主要的浏览器和WEB服务器程序中,因此,仅需安装服务器证书就可以激活该功能了,即通过它可以激活SSL协议,实现数据信息在客户端和服务器之间的加密传输,可以防止数据信息的泄露,保证了双方传递信息的安全性,而且用户可以通过服务器证书验证他所访问的网站是否是真实可靠。
同时谷歌、百度等巨头已经默认使用HTTPS安全链接。火狐、Chrome等浏览器也默认将非HTTPS链接显示为‘不安全’。

请输入图片描述

贵吗? 为什么使用letsencrypt.org ?

如果要启用HTTPS,我们就需要从证书授权机构(以下简称CA) 处获取一个证书,Let's Encrypt 就是一个 CA。我们可以从 Let's Encrypt 获得网站域名的免费的证书。这篇文章也主要讲的是通过 Let's Encrypt + Apache 来让网站升级到HTTPS。
https://letsencrypt.org/about/][2]
好处:

  • 支持子域名
  • 支持泛域名
  • 支持API
  • 免费

难点:

  • 申请下来的证书一般只有3个月有效期
  • 需要命令号知识

申请和安装

  1. 申请证书

    wget https://dl.eff.org/certbot-auto
    chmod a+x certbot-auto
    sudo ./certbot-auto --manual certonly

    注意:

    多个域名之间用半角逗号或者空格隔开
    泛域名只支持DNS验证

    根据步骤一步一步下去,最后成功生成的证书会保存在下面的目录。

    /etc/letsencrypt/live/www.yourdomain.com/

  2. 安装证书到Apache

    yum install mod_ssl openssl

    修改配置文件

    vi /etc/httpd/conf.d/ssl.conf

    找到下面的内容并替换如下

    SSLEngine on
    SSLProtocol all -SSLv2
    SSLCipherSuite DEFAULT:!EXP:!SSLv2:!DES:!IDEA:!SEED:+3DES
    
    SSLCertificateKeyFile /etc/letsencrypt/live/www.yourdomain.com/privkey.pem
    SSLCertificateFile /etc/letsencrypt/live/www.yourdomain.com/cert.pem
    SSLCertificateChainFile /etc/letsencrypt/live/www.yourdomain.com/chain.pem

    保存并重启Apache

    service restart httpd

续订证书

不像大部分的付费证书,LE的证书有效期比较短,所以需要我们经常进行续订操作,可能也是为了证书的有效性。

手动续订

./certbot-auto renew

自动续订

申请证书的时候需要验证域名的有效归属性,续订的时候也需要,例如放指定文件到指定的网站目录;或者是添加DNS记录等。幸好Certbo 可以设置各种挂钩,实现自动验证。

  1. 以cloudflare.com为例,通过调用CF的API实现自动更新DNS,并自动验证

    vi /etc/letsencrypt/renewal/msg2.xyz.sh

    #!/bin/bash
    curl --tlsv1.2   -X PUT "https://api.cloudflare.com/client/v4/zones/ZONEID/dns_records/RECORDID" \
     -H "X-Auth-Email: [email protected]" \
     -H "X-Auth-Key: APIAUTHKEY" \
     -H "Content-Type: application/json" \
     --data '{"type":"TXT","name":"_acme-challenge.msg2.xyz","content":"'${CERTBOT_VALIDATION}'","ttl":120,"proxied":false}'
    sleep 5

    CF API文档 https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record

  2. 运行命令参数 --manual-auth-hook

    ./certbot-auto renew --manual-auth-hook /etc/letsencrypt/renewal/msg2.xyz.sh

    如果一切正常,那么命令运行之后不需要再手动操作,直到最后成功,如下:

    Saving debug log to /var/log/letsencrypt/letsencrypt.log
    
    ` - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Processing /etc/letsencrypt/renewal/msg2.xyz.conf
    `- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Plugins selected: Authenticator manual, Installer None
    Renewing an existing certificate
    Running deploy-hook command: /etc/letsencrypt/renewal-hooks/deploy/httpd.sh
    `- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    new certificate deployed without reload, fullchain is
    /etc/letsencrypt/live/msg2.xyz/fullchain.pem
    `- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    
    `- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    
    Congratulations, all renewals succeeded. The following certs have been renewed:
      /etc/letsencrypt/live/msg2.xyz/fullchain.pem (success)
    `- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    提示: Certbo会记住上一次运行的参数,所以再次运行续订的时候,可以不添加--manual-auth-hook

使用renewal-hooks实现Apache自动重启,使最新的证书在网页端生效

证书虽然续订成功,但是Apache需要重启一下才会载入最新的证书

vi /etc/letsencrypt/renewal-hooks/deploy/httpd.sh

#!/bin/sh
set -e
service httpd restart > /dev/null

添加运行权限

chmod +x /etc/letsencrypt/renewal-hooks/deploy/httpd.sh

设置自动运行

0 0,12 * * * python -c 'import random; import time; time.sleep(random.random() * 3600)' && /path/to/certbot-auto renew
apache, https, centos, letsencrypt, ssl, free, certification, certbo, wildcard, cloudflare, renew
Get your free SSL certification and extend it automatically
Mac环境PHP strtoupper 中文乱码

仅有一条 关于 " 申请免费的SSL证书构建HTTPS站点并自动续订 "的评论.

  1. lamp环境下使用certbot将http升级https R11; 早新
    October 19th, 2019 at 05:19 pm

    [...]从零开始折腾博客(5):全站从http升级到https: https://idealclover.top/archives/320/ 申请免费的SSL证书构建HTTPS站点并自动续订:http://howboring.us/archives/free-SSL-certification-for-https-and-extend-it-automatically-in-centos.html Red[...]

    回复

钜添

56 文章
2 分類
1 页面
GitHub 日记技术
php apache centos macos mysql https shadowsocks mac 微信 ups svn javascript outline accesskey Chrome Extension letsencrypt ssl free certification certbo wildcard renew cloudflare pgsql 归档 binlog isset array_key_exists redmine axure
© 2023 钜添   |   文章 RSS     |  登录
由 Typecho 强力驱动
主題 - NexT
Send message encrypted and private - Msg2
Build your Under Construction Page without hosting - UnderConstruct.IO