网站首页学无止境LINUX

Let's Encrypt HTTPS证书一键创建shell分享

发布时间:2017-06-25 17:05:06编辑:songlin阅读(195)

    上篇文章已经讲解过了Let's Encrypt HTTPS证书的创建过程(主要借鉴与https://imququ.com博文,略作修改与注释)。不知各位使用的时候,是否也和我一样,需要一遍又一遍的去复制、改阐述、粘贴,感觉有点麻烦。先本人做一个简单的shell脚本无需粘贴复制去执行直接在脚本的后面家上参数就好。代码如下

    1. #!/bin/bash   
    2.    
    3. # 执行时需要的参数    
    4. #   -d [必填] 域名    
    5. #   -c [选填] openssl.cnf的路径 默认为/etc/pki/tls/openssl.cnf 如不是请根据自己服务器的地址填写-c参数   
    6. #   -p [选填] 此项为验证域名时使用 用于存放验证域名是生成的临时文件 默认为脚本当前文件下的sslCsrVerify目录,可自定义;验证域名的Nginx/Apache配置可在 https://wangsonglin.cn/study/nginx/c97.html 这边博文中查看   
    7. # ./create_ssl.sh -d wangsonglin.cn -c /etc/pki/tls/openssl.cnf -p /data/ssl/sslCsrVerify 
    8.  
    9. path='sslCsrVerify' 
    10. domain='' 
    11. opensslcnf='/etc/pki/tls/openssl.cnf' 
    12. PWD=`pwd
    13.  
    14. while getopts "d:c:p:" opt; do 
    15.     case $opt in 
    16.         d) 
    17.             domain=$OPTARG 
    18.             ;; 
    19.         c) 
    20.             opensslcnf=$OPTARG 
    21.             ;; 
    22.         p) 
    23.             Path=$OPTARG 
    24.             ;; 
    25.         \?) 
    26.             echo "Invalid option: -$OPTARG" 
    27.             ;; 
    28.     esac 
    29. done 
    30.  
    31. if [ "${domain}" == "" ];then 
    32.     echo "error: domain is empty. please add -d param." 
    33.     exit
    34. fi 
    35.  
    36. if [ "${Path}" == "" ];then 
    37.     csrVerifyPath="${PWD}/${path}/" 
    38. else 
    39.     csrVerifyPath=${Path} 
    40.      
    41. fi 
    42.  
    43. openssl genrsa 4096 > account.key 
    44.  
    45. openssl genrsa 4096 > ${domain}.key 
    46.  
    47. openssl req -new -sha256 -key ${domain}.key -subj "/" -reqexts SAN -config <(cat ${opensslcnf} <(printf "[SAN]\nsubjectAltName=DNS:${domain}")) > ${domain}.csr 
    48.  
    49. wget -O - https://raw.githubusercontent.com/diafygi/acme-tiny/master/acme_tiny.py > acme_tiny.py 
    50.  
    51. mkdir -p ${csrVerifyPath} 
    52.  
    53. chmod 755 ${csrVerifyPath} 
    54.  
    55. read -p " Notice : Configure the configuration file before you perform the next step.\n Please select your web soft:Apache/Nginx; Enter 1 or 2 " webSoftTmp 
    56. if [ "$webSoftTmp" == "2" ];then 
    57.     Soft='Nginx'
    58.     echo "you select Nginx ; 
    59.     Copy the following string to your Nginx 80 port configuration file AND reload Nginx  
    60.      
    61.         ##---------------------- START ----------------------------- 
    62.         location ^~ /.well-known/acme-challenge/ {   
    63.             alias ${csrVerifyPath};   
    64.             try_files \$uri =404;   
    65.         } 
    66.         ##---------------------- END ----------------------------- 
    67.          
    68.     "; 
    69. elif [ "$webSoftTmp" == "1" ];then 
    70.     Soft='Apache'
    71.     echo "you select Apache 
    72.     Copy the following string to your Apache 80 port configuration file AND restart Apache 
    73.          
    74.         ##---------------------- START ----------------------------- 
    75.         <IfModule alias_module>   
    76.             scriptAlias /.well-known/acme-challenge \"${csrVerifyPath}\"   
    77.             <Directory \"${csrVerifyPath}\">   
    78.                 Options Indexes FollowSymLinks   
    79.                 AllowOverride None   
    80.                 Order allow,deny   
    81.                 Allow from all   
    82.             </Directory>   
    83.         </IfModule> 
    84.         ##---------------------- END ----------------------------- 
    85.          
    86.     " 
    87. fi 
    88. read -p " Notice : If the configuration file configuration is complete, we proceed to the next step: to validate the domain name and press any key to continue : "
    89. echo "${Soft} Configure complete ! verify domain start !"
    90.  
    91. python acme_tiny.py --account-key ./account.key --csr ./${domain}.csr --acme-dir ${csrVerifyPath} > ./signed.crt 
    92.  
    93. wget -O - https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem > intermediate.pem 
    94.  
    95. cat signed.crt intermediate.pem > chained.pem 
    96.  
    97. wget -O - https://letsencrypt.org/certs/isrgrootx1.pem > root.pem 
    98.  
    99. cat intermediate.pem root.pem > full_chained.pem 
    100.  
    101. echo " create SSL certificate finish.;"
    102. echo " ##------------ START ----------------"
    103. if [ "$Soft" == 'Nginx' ];then 
    104.     echo " 
    105.         ssl             on; 
    106.         ssl_certificate     ${PWD}/chained.pem;  
    107.         ssl_certificate_key ${PWD}/${domain}.key;  
    108.     "; 
    109. else 
    110.     echo " 
    111.         SSLEngine on   
    112.         SSLCertificateFile ${PWD}/chained.pem   
    113.         SSLCertificateKeyFile ${PWD}/${domain}.key   
    114.     "; 
    115. fi 
    116. echo " ##------------ END ---------------- 
    117. "; 
    118. echo " Copy the above string to your ${Soft}'s 443 port configuration file to complete the HTTPS certificate for Let's Encrypt. "
    119. echo " complete!!!! "; 

    脚本执行需可执行权限

    1. chmod +x create_ssl.sh  

    注意:执行本脚本之前需先更改Nginx/Apache配置文件负责域名验证出错。配置 可在https://wangsonglin.cn/study/nginx/c97.html 这边博文中查看

    2017/07/17 日更新不需要实现配置 Nginx/Apache的配置文件文件,改在程序执行时根据提示配置(程序执行是会生成配置 直接复制黏贴就可以)

    PS:感谢小崔(博客)的提示

    shell下载地址:https://d.wangsonglin.cn/2017/06/25/create_ssl.zip

     

    因能力有限,脚本比较简陋(我感觉是这样)。大神请指教,喷喷们请飞过。

     

    本文为博主原创,转载请指明出处:https://wangsonglin.cn