Guest User

Updated vHost maker

a guest
Jul 22nd, 2015
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.88 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ####========================================================================================================================####
  4. ####This script is designed to create new vhost files for apache configurations. This script will also allow you to add     ####      
  5. ####SSL certificates if you choose to use one.                                                                              ####
  6. ####                                                                                                                        ####
  7. ####Written by: Christopher S                                                                                               ####
  8. ####Linux Infrastructure Engineer, Credera                                                                                  ####
  9. ####========================================================================================================================####
  10.  
  11. set -e
  12.  
  13. apache=/etc/apache2/sites-available
  14. httpd=/etc/httpd/sites-available
  15.  
  16. vhostfunc () {
  17.    if [[ $ssl == "n" ]]
  18.     then
  19. echo "<VirtualHost *:80>
  20. ServerName $domain
  21. ServerAlias www.$domain
  22. DocumentRoot /var/www/$domain
  23.  
  24. <Directory /var/www/$domain>
  25.     Options Indexes FollowSymLinks MultiViews
  26.        AllowOverride All
  27.        Require all granted
  28. </Directory>
  29.  
  30. ErrorLog /var/log/apache2/$domain/error.log
  31. CustomLog /var/log/apache2/$domain/access.log combined
  32.  
  33.  
  34. </VirtualHost>" >> $apache/${domain}.conf
  35.         fi
  36. }
  37.  
  38. sslvhostfunc () {
  39.         if [[ $ssl == "y" ]]
  40.          then
  41.            echo "<VirtualHost *:80>
  42. ServerName $domain
  43. ServerAlias www.$domain
  44. DocumentRoot /var/www/$domain
  45.  
  46.     <Directory /var/www/$domain>
  47.         Options Indexes FollowSymLinks MultiViews
  48.         AllowOverride All
  49.         Require all granted
  50.     </Directory>
  51.  
  52. ErrorLog /var/log/apache2/$domain/error.log
  53. CustomLog /var/log/apache2/$domain/access.log combined
  54. </VirtualHost>
  55.      
  56. <VirtualHost *:443>
  57. ServerName $domain
  58. ServerAlias www.$domain
  59. DocumentRoot /var/www/$domain
  60.  
  61. SSLEngine on
  62. SSLCertificateFile /etc/ssl/$domain/crt
  63. SSLCertificateKeyFile /etc/ssl/$domain/key
  64.  
  65.     <Directory /var/www/$domain>
  66.         Options INdexes FollowSymLinks MultiViews
  67.         AllowOverride All
  68.         Require all granted
  69.     </Directory>
  70.  
  71. ErrorLog /var/log/apache2/$domain/error.log
  72. CustomLog /var/log/apache2/$domain/access.log combined
  73. </VirtualHost>" >> $apache/${domain}.conf
  74.         fi
  75. }
  76.  
  77.  
  78. if [[ $EUID != 0 ]]
  79.    then
  80.      echo "Please run this script as root...exiting"
  81.      exit
  82. fi
  83.  
  84.  
  85. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
  86. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
  87. echo "First we need to check what OS we're running on."
  88. echo "Checking now...."
  89.  
  90. #Check for Distro
  91. if [ -f /etc/lsb-release ]; then
  92.     . /etc/lsb-release
  93.     DISTRO=$DISTRIB_ID
  94. elif [ -f /etc/debian_version ]; then
  95.     DISTRO=Debian
  96. elif [ -f /etc/redhat-release ]; then
  97.     DISTRO="Red Hat"
  98. else
  99.     DISTRO=$(uname -s)
  100. fi
  101.  
  102. echo "Looks like we're running $DISTRO"
  103. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
  104. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
  105.  
  106. read -p "Would you like to create a new vhost? (y/n) " vhost
  107.  
  108. if [[ $vhost == "y" ]]
  109. then
  110.  
  111. read -p "What is the new domain to create a vhost for? " domain
  112. read -p "Will this site be using SSL? y/n " ssl
  113.  
  114.  
  115. if [[ $ssl == "y" ]]
  116.  then
  117.       read -p "Do you have the SSL Certificate and Key ready to paste? (y/n) " cert
  118.    if [[ $cert == "y" ]]
  119.      then
  120.      read -p "Please paste your certificate now: " certificate
  121.      read -p "Please paste your key file now: " key
  122.     var1=$(openssl x509 -noout -subject -in certificate 2>/dev/null | cut -d = -f4| cut -d / -f1)
  123.       if [[ $? == 0 ]]
  124.         then
  125.           if [[ $var1 == $domain ]]
  126.               then
  127.             echo "Common name match"
  128.             mkdir -p /etc/ssl/$domain
  129.             echo $key >> /etc/ssl/$domain/key
  130.             echo $certificate >> /etc/ssl/$domain/crt
  131.               else
  132.             echo "Common name mismatch..This script will not exit."
  133.             exit
  134.               fi
  135.             else
  136.           echo "Invalid Certificate or Key"
  137.           exit
  138.      fi  
  139.     fi
  140.  
  141.  
  142.    if [[ $cert == "n" ]]
  143.      then
  144.      mkdir -p /etc/ssl/$domain
  145.      echo "SSL Directory created. Do not choose to reload apache as it will fail until you have your SSL certs installed in this /etc/ssl/$domain"
  146.    fi
  147. fi
  148.  
  149.  
  150. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
  151. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
  152.  
  153. #If Ubuntu then do the following
  154. if [[ "$DISTRO" == "Ubuntu" ]]
  155.  then
  156.  
  157.     echo "Your Distro is Ubuntu so we are creating the vhost file in /etc/apache2/sites-available"
  158.     touch $apache/${domain}.conf
  159.     mkdir -p /var/www/$domain
  160.     mkdir -p /var/log/apache2/$domain
  161.  
  162.  
  163.  
  164. vhostfunc
  165.  
  166.  
  167. sslvhostfunc
  168.  
  169.  
  170.  
  171. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
  172. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
  173. echo "New Vhost created /etc/apache2/sites-available/$domain"
  174.  
  175.  
  176. #Ask user if they want to restart Apache
  177.     if [[ $cert == "y" ]]
  178.      then
  179.        read -p "Would you like to restart Apache now and enable this domain? NOTE: If your SSL Certificate is incorrect this could cause apache to not restart. (y/n)" apachereload
  180.  
  181.         if [[ $apachereload == "y" ]]
  182.          then
  183.            echo "Performing configuration check..."
  184.              apache2ctl -t
  185.              result=$?
  186.              if [[ $result -eq 0 ]]
  187.             then
  188.                echo "Configuration pass"
  189.                a2ensite ${domain}.conf
  190.                service apache2 reload
  191.             else
  192.                echo "Failed"
  193.                 fi
  194.         fi
  195.  
  196.     fi
  197.  
  198.     if [[ $apachereload == "n" ]]
  199.      then
  200.        echo "To enable this site type 'a2ensite ${domain}.conf' and then 'service apache2 reload'"
  201.     fi
  202.  
  203.  
  204.  
  205. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
  206. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
  207. echo "New Log file created in /var/log/$domain/"
  208. echo "New DocumentRoot created in /var/www/$domain/"
  209. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
  210. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
  211.  
  212.  
  213. #If Red Hat then do the following
  214. if [[ "$DISTRO" == "Red Hat" ]]
  215.  then
  216.  
  217. #Check to see if httpd is using sites-available
  218.     if [[ ! -d $httpd ]]
  219.      then
  220.        echo "Directory not found...creating now."
  221.        mkdir -p /etc/httpd/sites-available
  222.        mkdir -p /etc/httpd/sites-enabled
  223.     fi
  224.  
  225. echo "Appending httpd.conf to include new vhost directories"
  226. echo "Include /etc/httpd/sites-enabled/*.conf" >> /etc/httpd/httpd.conf
  227.  
  228. touch $httpd/${domain}.conf/httpd
  229. mkdir -p /var/www/$domain
  230. mkdir -p /var/log/httpd/$domain
  231.  
  232.  
  233.  
  234. vhostfunc
  235.  
  236.  
  237.  
  238. sslvhostfunc
  239.  
  240.  
  241.  
  242.  
  243. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
  244. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
  245. echo "New Vhost created /etc/httpd/sites-available/$domain"
  246.  
  247. #Ask user if they want to reload apache and enable new vhost to be live
  248.     if [[ $cert == "y" ]]
  249.      then
  250.        read -p "Would you like to restart Apache now and enable this domain? NOTE: If your configuration has syntax errors this could cause apache to not restart. (y/n)" apachereload
  251.  
  252.  
  253.         if [[ $apachereload == y ]]
  254.          then
  255.            echo "Creating symbolic link..."
  256.            ln -s /etc/httpd/sites-available/${domain}.conf /etc/httpd/sites-enabled/
  257.            echo "Reloading httpd service..."
  258.            service httpd reload
  259.         fi
  260.     fi
  261.  
  262.  
  263.     if [[ $apachereload == "n" ]]
  264.      then
  265.        echo "To enable this site type 'ln -s /etc/httpd/sites-available/${domain}.conf /etc/httpd/sites-enabled/' and then a 'service httpd reload'"
  266.     fi
  267.  
  268. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
  269. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
  270. echo "New Log file created in /var/log/$domain/"
  271. echo "New DocumentRoot created in /var/www/$domain/"
  272.  
  273.     if [[ $ssl == "y" ]]
  274.      then
  275.        echo "New SSL Directory created at /etc/ssl/$domain/"
  276.     fi
  277.  
  278. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
  279. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
  280.  
  281. fi
  282. fi
  283. fi
  284.  
  285. if [[ $vhost == "n" ]]
  286.  then
  287.    echo "Exiting script..."
  288.    exit
  289. fi
Advertisement
Add Comment
Please, Sign In to add comment