SHARE
TWEET

bulk-create-apache2-vhosts.sh ; revision: 20160704

sufehmi Oct 3rd, 2016 145 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. vhost_location='/etc/apache2/sites-available/'
  4. #vhost_location='/tmp/tmp/'
  5.  
  6. vhost_docroot='/var/www/mycompany/dep/SUBDOM/controller'
  7.  
  8. vhost_template="<VirtualHost *:80>\n
  9.        ServerAdmin     engineers@mycompany.com\n
  10.        ServerName      VHOST\n
  11. \n
  12.        DocumentRoot $vhost_docroot/\n
  13. \n
  14.        <Directory $vhost_docroot>\n
  15.                Options Indexes FollowSymLinks MultiViews\n
  16.                AllowOverride All\n
  17.                Order allow,deny\n
  18.                allow from all\n
  19.        </Directory>\n
  20. \n
  21.        ErrorLog \${APACHE_LOG_DIR}/error-VHOST.log\n
  22.        LogLevel error\n
  23. \n
  24.        CustomLog \${APACHE_LOG_DIR}/access-VHOST.log combined\n
  25. </VirtualHost>"
  26.  
  27. ########### START ################################
  28. vhost=( `cat "vhosts-list.txt" `)
  29.  
  30. for t in "${vhost[@]}"
  31. do
  32.     echo $t
  33.  
  34.     # write VHOST.conf
  35.     echo -e $vhost_template > $vhost_location/$t.conf
  36.  
  37.     # get the SUBDOM
  38.     subdom=`echo $t | /usr/bin/cut -d'.' -f 1`
  39.  
  40.     # find & replace all VHOST
  41.     /bin/sed -i -e "s/VHOST/$t/g" $vhost_location/$t.conf
  42.  
  43.     # find & replace all SUBDOM
  44.     /bin/sed -i -e "s/SUBDOM/$subdom/g" $vhost_location/$t.conf
  45.  
  46.     # enable this vhost
  47.     /usr/sbin/a2ensite $t
  48.  
  49.     # if you need SSL certificate for this vhost,
  50.     # you can use Let's Encrypt
  51.     # NOTE: this vhost's DNS must be already resolvable
  52.     #/usr/bin/certbot --apache -d $t
  53.  
  54. done
  55.  
  56. # restart apache to activate the new vhosts
  57. /etc/init.d/apache2 restart
  58.  
  59. # done !
RAW Paste Data
Top