Add certificate file
readme
./ addurl.sh product name (must be the same as the product name in vhost) For example: ./ addurl.sh E68 addurl.sh (script name, can be modified)
All domain name files must end in txt
The certificate directory and domain name files must be in the same directory
The name of the domain name txt file must be the same as the subject name of the certificate directory,
For example: The directory of the certificate file is called longdu03.com_nginx The name of the domain name file is longdu03.com.txt or longdu03.txt, otherwise the file will not be found due to matching problems
Script addcrt.sh
#!/bin/bash # Date: 2020/02/18 # Usage: Add new crts and keys and modifi config date=`date +%F_%H:%M` product=$1 crtdir="/opt/nginx/conf/crt" vhostdir="/opt/nginx/conf/vhost" RED_COLOR='\E[1;31m' GREEN_COLOR='\E[1;32m' YELLOW_COLOR='\E[1;33m' BLUE_COLOR='\E[1;34m' RES='\E[0m' newdir=`find -type d -a -name "*nginx*"` check_product_num=`find $vhostdir -type d -a -name "$product" | wc -l` ## check your options if [ -z $product ];then echo "Usage $0 E68|qile|qy" echo "Please input Your product" exit 1 elif [ $check_product_num -eq 0 ];then echo "Options ERROR, please check your product in your $vhostdir" exit 1 be echo -e "========================${RED_COLOR} $date START ${RES} ==========================" >> log.log for i in $newdir do newname1=`echo $i| awk -F "." '{ print $(NF-1)}' | awk -F '/' '{print $2}'` newname2=`echo $i| awk -F "." '{ print $(NF-1)}' ` newcrtname=${newname1:-"$newname2"} if [ -z $newcrtname ];then echo -e "$date New filename $newcrtname is $RED_COLOR empty ${RES} , Please check." >> log.log exit 1 else echo -e "======${YELLOW_COLOR} New filename is $newcrtname ${RES} ============" echo -e "$date New filename is ${GREEN_COLOR} $newcrtname ${RES}." >> log.log be crtnum=`find $crtdir -name "*$newcrtname*" | wc -l` newcrtfile=`ls $i/*.crt` newkeyfile=`ls $i/*.key` function process_crtdir() { if [ $crtnum -gt 0 ];then echo -e "$date $crtdir/$newcrtname is ${RED_COLOR} exist ${RES} Please check it " >> log.log exit 1 else mkdir -p $crtdir/$newcrtname if [ $? -eq 0 ];then echo -e "$date Creat new folder $crtdir/$newcrtname is ${GREEN_COLOR} successfully ${RES}" >> log.log # copy crt cp -rp $ newcrtfile $ crtdir / $ newcrtname / if [ $? -eq 0 ];then echo -e "$date Copy new crt $newcrtfile is ${GREEN_COLOR} successfully ${RES}" >> log.log else echo -e "$date Copy new crt $newcrtfile is ${RED_COLOR} false ${RES}. So exit" >> log.log exit 1 be #copy key cp -rp $ newkeyfile $ crtdir / $ newcrtname / if [ $? -eq 0 ];then echo -e "$date Copy new crt $newkeyfile is ${GREEN_COLOR} successfully ${RES}" >> log.log else echo -e "$date Copy new crt $newkeyfile is ${RED_COLOR} false ${RES} So exit" >> log.log exit 1 be else echo -e "$date Creat new folder $crtdir/$newcrtname is ${RED_COLOR} false ${RES} So exit" >> log.log exit 1 be be } process_crtdir echo "process crtdir is ok" newsslfile="server_name_e68_ssl_$newcrtname" newsslfile_num=`find $vhostdir -name "$newsslfile" | wc -l ` function process_sslfile(){ if [ $newsslfile_num -gt 0 ];then echo -e "$date $vhostdir/$newsslfile is ${RED_COLOR} exist ${RES} Please check it " >> log.log exit 1 else echo -e "server_name\n;" >> $vhostdir/$product/$newsslfile if [ $? -eq 0 ];then echo -e "$date Create new ssl file $vhostdir/$product/$newsslfile is ${GREEN_COLOR} successfully ${RES}" >> log.log else echo -e "$date Create new ssl file $vhostdir/$product/$newsslfile is ${RED_COLOR} false ${RES}. So exit" >> log.log exit 1 be be echo -e "Create $vhostdir/$product/$newsslfile is ${GREEN_COLOR} successfully ${RES} " newurlfile = `$ ls newcrtname * .txt` # delete blank line sed -i '/^$/d' $newurlfile # add url to nwessl file sum=0 for j in `cat $newurlfile` do sed -i '/;/ i'$j'' "$vhostdir/$product/$newsslfile" if [ $? -eq 0 ];then ((sum=sum+1)) else echo -e "$date Add $j in $vhostdir/$product/$newsslfile is ${RED_COLOR} false ${RES}. So exit" >> log.log exit 1 be done if [ $? -eq 0 ];then echo -e "$date Add new url in $vhostdir/$product/$newsslfile is ${GREEN_COLOR} successfully ${RES}" >> log.log else echo -e "$date Add new url in $vhostdir/$product/$newsslfile is ${RED_COLOR} false ${RES}. So exit" >> log.log exit 1 be echo -e "There are $sum url add in $vhostdir/$product/$newsslfile " } process_sslfile echo "sslfile is ok" model_configfile=`ls $vhostdir/$product/*.conf | head -n 10 | tail -n 1 ` new_configfile=ssl_"$product"_"$newcrtname".conf new_configfile_num=`find $vhostdir/$product/ -name "$new_configfile" | wc -l ` function prosee_config(){ if [ $new_configfile_num -gt 0 ];then echo -e "$date $vhostdir/$product/$new_configfile is ${RED_COLOR} exist ${RES} . So exit " >> log.log exit 1 else cp -rp $model_configfile $vhostdir/$product/$new_configfile if [ $? -eq 0 ];then echo -e "$date Create $vhostdir/$product/$new_configfile is ${GREEN_COLOR} successfully ${RES} " >> log.log else echo -e "$date Create $vhostdir/$product/$new_configfile is ${RED_COLOR} false ${RES} So exit" >> log.log exit 1 be be ## Modified config ## Delete strings sed -i '/include/d' $vhostdir/$product/$new_configfile if [ $? -eq 0 ];then echo -e "$date Delete include $vhostdir/$product/$new_configfile is ${GREEN_COLOR} successfully ${RES} " >> log.log else echo -e "$date Delete include $vhostdir/$product/$new_configfile is ${RED_COLOR} false ${RES} So exit " >> log.log exit1 be sed -i '/ssl_certificate/d' $vhostdir/$product/$new_configfile if [ $? -eq 0 ];then echo -e "$date Delete ssl_certificate $vhostdir/$product/$new_configfile is ${GREEN_COLOR} successfully ${RES} " >> log.log else echo -e "$date Delete ssl_certificate $vhostdir/$product/$new_configfile is ${RED_COLOR} false ${RES} So exit" >> log.log exit1 be ## Add strings real_newcrt_name=`echo $newcrtfile | awk -F "/" '{ print $NF}'` real_newkey_name=`echo $newkeyfile | awk -F "/" '{ print $NF}'` sed -i "/listen/a \ include vhost/$product/$newsslfile;\n include vhost/conf_global;\n include vhost/conf_proxy;" $vhostdir/$product/$new_configfile if [ $? -eq 0 ];then echo -e "$date Replace strings include in $vhostdir/$product/$new_configfile is ${GREEN_COLOR} successfully ${RES} " >> log.log else echo -e "$date Replace strings include in $vhostdir/$product/$new_configfile ${RED_COLOR} false ${RES} So exit" >> log.log exit 1 be sed -i "/access_log/i \ ssl_certificate crt/$newcrtname/$real_newcrt_name;" $vhostdir/$product/$new_configfile sed -i "/access_log/i \ ssl_certificate_key crt/$newcrtname/$real_newkey_name;" $vhostdir/$product/$new_configfile if [ $? -eq 0 ];then echo -e "$date Replace strings sslcrt and sslkey in $vhostdir/$product/$new_configfile is ${GREEN_COLOR} successfully ${RES} " >> log.log else echo -e "$date Replace strings sslcrt and sslkey in $vhostdir/$product/$new_configfile ${RED_COLOR} false ${RES} So exit" >> log.log exit 1 be } prosee_config echo "process config file $vhostdir/$product/$new_configfile is ok " echo -e "=============== ${BLUE_COLOR} $newcrtname done ${RES}=====================================" done #### change group and user to www chown -R www.www "$crtdir" if [ $? -eq 0 ];then echo -e "$date Change $crtdir Owner to www is ${GREEN_COLOR} successfully ${RES} " >> log.log else echo -e "$date Change $crtdir Owner to www is ${RED_COLOR} false ${RES} " >> log.log be chown -R www.www $vhostdir if [ $? -eq 0 ];then echo -e "$date Change $vhostdir Owner to www is ${GREEN_COLOR} successfully ${RES} " >> log.log else echo -e "$date Change $vhostdir Owner to www is ${RED_COLOR} false ${RES} " >> log.log be echo -e "*****${YELLOW_COLOR}all ok ${RES}*****"
Detection. Remove duplicate domain names and file scripts
Detect redundant domain names
readme
The script and nginx configuration need to be in the same directory.
Script check.sh
#!/bin/bash for i in `find -name "server_name*"` do for j in ` cat url.txt` do urlnum=`grep -rnw "$j" $i | wc -l` if [ $urlnum -gt 0 ] ;then sed -i /$j/d $i echo "Delete $j from $i " >> check.log be done done
Script mvfile.sh
#!/bin/bash file=`find -name "server_name*" -a -size -16c | awk -F "\/" '{ print $2}'` bak_dir="/home/bak/vhost2020211" for i in $file do filename=`echo "$i" | awk -F "_" '{ print $NF}'` sslfile=`sudo grep -rnw "$i" ./* | awk -F ":" '{ print $1}'` echo "$i" >> log.log echo "$sslfile" >> log.log mv $ i $ bak_dir mv $ sslfile $ bak_dir done