如何在 Apache 上部署 Let's Encrypt 证书 &&自动续期脚本

来源:互联网 发布:2017淘宝网店数量 编辑:程序博客网 时间:2024/06/13 22:55
姊妹篇:http://bbs.qcloud.com/thread-12059-1-1.html 
Linux基金会宣布它将托管Let's Encrypt项目和互联网安全研究组(ISRG)。Let’s Encrypt CA项目由Mozilla、思科、Akamai、IdenTrust和EFF等组织发起,向网站自动签发和管理免费证书,加速将Web从HTTP过渡到HTTPS。ISRG则是开发Let’s Encrypt CA的非营利组织。

今天我来教大家在Nginx上部署Let's Encrypt证书
  • 下载Let's Encrypt客户端
    首先我们要安装git
  1. apt-get update
  2. apt-get -y install git
复制代码
然后,检出Let's Encrypt的客户端源码
  1. git clone https://github.com/letsencrypt/letsencrypt
复制代码
这样,我们就成功的下载了Let's Encrypt的客户端
  • 签发证书

进入目录
  1. cd letsencrypt
复制代码
使用 Let's Encrypt的Apache插件生成证书即可
  1. ./letsencrypt-auto --apache -d example.com
复制代码
如果只签一个域名,按照上面的命令就可以了
他会自动安装插件,然后你需要输入邮箱来用于证书的找回。同时还会要求你选择是否同时开启Http和https和是否开启强制https。
可以参照http://bbs.qcloud.com/thread-12059-1-1.html
如果是多个域名,就用下面的命令生成
  1. ./letsencrypt-auto --apache -d example.com -d www.example.com
复制代码

  1. #!/bin/bash
  2. #================================================================
  3. # Let's Encrypt renewal script for Apache on Ubuntu/Debian
  4. # @author Erika Heidi<erika@do.co>
  5. # Usage: ./le-renew.sh [base-domain-name]
  6. #================================================================
  7. domain=$1
  8. le_path='/opt/letsencrypt'
  9. le_conf='/etc/letsencrypt'
  10. exp_limit=30;

  11. get_domain_list(){
  12.         certdomain=$1
  13.         config_file="$le_conf/renewal/$certdomain.conf"
  14.         
  15.         if [ ! -f $config_file ] ; then
  16.                 echo "[ERROR] The config file for the certificate $certdomain was not found."
  17.                 exit 1;
  18.         fi

  19.         domains=$(grep --only-matching --perl-regex "(?<=domains \= ).*" "${config_file}")
  20.         last_char=$(echo "${domains}" | awk '{print substr($0,length,1)}')

  21.         if [ "${last_char}" = "," ]; then
  22.                 domains=$(echo "${domains}" |awk '{print substr($0, 1, length-1)}')
  23.         fi

  24.         echo $domains;
  25. }

  26. if [ -z "$domain" ] ; then
  27.         echo "[ERROR] you must provide the domain name for the certificate renewal."
  28.         exit 1;
  29. fi

  30. cert_file="/etc/letsencrypt/live/$domain/fullchain.pem"

  31. if [ ! -f $cert_file ]; then
  32.         echo "[ERROR] certificate file not found for domain $domain."
  33.         exit 1;
  34. fi

  35. exp=$(date -d "`openssl x509 -in $cert_file -text -noout|grep "Not After"|cut -c 25-`" +%s)
  36. datenow=$(date -d "now" +%s)
  37. days_exp=$(echo \( $exp - $datenow \) / 86400 |bc)

  38. echo "Checking expiration date for $domain..."

  39. if [ "$days_exp" -gt "$exp_limit" ] ; then
  40.         echo "The certificate is up to date, no need for renewal ($days_exp days left)."
  41.         exit 0;
  42. else
  43.         echo "The certificate for $domain is about to expire soon. Starting renewal request..."
  44.         domain_list=$( get_domain_list $domain )
  45.         "$le_path"/letsencrypt-auto certonly --apache --renew-by-default --domains "${domain_list}"
  46.         echo "Restarting Apache..."
  47.         /usr/sbin/service apache2 reload
  48.         echo "Renewal process finished for domain $domain"
  49.         exit 0;
  50. fi
复制代码
将这个脚本加上可执行权限,再配置每个月或每两个月自动执行就可以保证你的证书不过期了!
原创粉丝点击