apache2 Config file template

Submitted by code_admin on Fri, 07/20/2018 - 16:02
  1. #!/bin/bash
  2. #Config file string replacer
  3. #This allows me to automatically generate my apache2 config files
  4. TEMPLATE_FILE=template.conf
  5.  
  6. VAR_001_SITE_NAME=site001
  7. VAR_001_SITE_HOST=www.site001.com
  8. VAR_001_SITE_ROOT=/var/www/site001
  9. VAR_001_SITE_ROOT_NOSSL=/var/wwwNOSSL/site001
  10.  
  11. VAR_002_SITE_NAME=site002
  12. VAR_002_SITE_HOST=www.site002.com
  13. VAR_002_SITE_ROOT=/var/www/site002
  14. VAR_002_SITE_ROOT_NOSSL=/var/wwwNOSSL/gosecure
  15.  
  16. VAR_003_SITE_NAME=theONE
  17. VAR_003_SITE_HOST=www.site003.com
  18. VAR_003_SITE_ROOT=/var/www/site003
  19. VAR_003_SITE_ROOT_NOSSL=/var/wwwNOSSL/gosecure
  20.  
  21. VAR_NUMS="001 002 003"
  22.  
  23. #$1 = number, $2 = name
  24. function readVar_fn()
  25. {
  26.     TMP=$(eval echo \${VAR_${1}_${2}})
  27.     echo ${TMP//\//\\\/}
  28. }
  29.  
  30. #$1 = filename, $2 = var to replace
  31. function replace_fn()
  32. {
  33.     sed "s/{{\$\$${2}\$\$}}/$(eval echo \${${2}})/g" ${1} > ${1}.replace_fn_tmp
  34.     mv ${1}.replace_fn_tmp ${1}
  35. }
  36.  
  37. for CUR in ${VAR_NUMS}; do
  38.     SITE_NAME=$(readVar_fn ${CUR} SITE_NAME)
  39.     SITE_HOST=$(readVar_fn ${CUR} SITE_HOST)
  40.     SITE_ROOT=$(readVar_fn ${CUR} SITE_ROOT)
  41.     SITE_ROOT_NOSSL=$(readVar_fn ${CUR} SITE_ROOT_NOSSL)
  42.  
  43.     echo "Creating config file for ${SITE_NAME} (${SITE_HOST})"
  44.     if [ -f ${SITE_NAME}.conf ]; then
  45.         echo "Removing current config"
  46.         rm ${SITE_NAME}.conf
  47.     fi
  48.     cp template.conf ${SITE_NAME}.conf
  49.    
  50.     replace_fn ${SITE_NAME}.conf SITE_NAME
  51.     replace_fn ${SITE_NAME}.conf SITE_HOST
  52.     replace_fn ${SITE_NAME}.conf SITE_ROOT
  53.     replace_fn ${SITE_NAME}.conf SITE_ROOT_NOSSL
  54.    
  55.     echo
  56. done
  57.  
  58. exit 0
RJM Article Type
Work Notes