qsadfasdgfgads

Untitled

Apr 9th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.79 KB | None | 0 0
  1. #!/bin/bash
  2. ### Set Language
  3. TEXTDOMAIN=virtualhost
  4.  
  5. ### Set default parameters
  6. action=$1
  7. domain=$2
  8. rootDir=$3
  9. owner=$(who am i | awk '{print $1}')
  10. apacheUser=$(ps -ef | egrep '(httpd|apache2|apache)' | grep -v root | head -n1 | awk '{print $1}')
  11. email='webmaster@localhost'
  12. sitesEnabled='/etc/apache2/sites-enabled/'
  13. sitesAvailable='/etc/apache2/sites-available/'
  14. userDir='/var/www/'
  15. sitesAvailabledomain=$sitesAvailable$domain.conf
  16.  
  17. ### don't modify from here unless you know what you are doing ####
  18.  
  19. if [ "$(whoami)" != 'root' ]; then
  20.     echo $"You have no permission to run $0 as non-root user. Use sudo"
  21.         exit 1;
  22. fi
  23.  
  24. if [ "$action" != 'create' ] && [ "$action" != 'delete' ]
  25.     then
  26.         echo $"You need to prompt for action (create or delete) -- Lower-case only"
  27.         exit 1;
  28. fi
  29.  
  30. while [ "$domain" == "" ]
  31. do
  32.     echo -e $"Please provide domain. e.g.dev,staging"
  33.     read domain
  34. done
  35.  
  36. if [ "$rootDir" == "" ]; then
  37.     rootDir=${domain//./}
  38. fi
  39.  
  40. ### if root dir starts with '/', don't use /var/www as default starting point
  41. if [[ "$rootDir" =~ ^/ ]]; then
  42.     userDir=''
  43. fi
  44.  
  45. rootDir=$userDir$rootDir
  46.  
  47. if [ "$action" == 'create' ]
  48.     then
  49.         ### check if domain already exists
  50.         if [ -e $sitesAvailabledomain ]; then
  51.             echo -e $"This domain already exists.\nPlease Try Another one"
  52.             exit;
  53.         fi
  54.  
  55.         ### check if directory exists or not
  56.         if ! [ -d $rootDir ]; then
  57.             ### create the directory
  58.             mkdir $rootDir
  59.             ### give permission to root dir
  60.             chmod 755 $rootDir
  61.             ### write test file in the new domain dir
  62.             if ! echo "<?php echo phpinfo(); ?>" > $rootDir/phpinfo.php
  63.             then
  64.                 echo $"ERROR: Not able to write in file $rootDir/phpinfo.php. Please check permissions"
  65.                 exit;
  66.             else
  67.                 echo $"Added content to $rootDir/phpinfo.php"
  68.             fi
  69.         fi
  70.  
  71.         ### create virtual host rules file
  72.         if ! echo "
  73.         <VirtualHost *:80>
  74.             ServerAdmin $email
  75.             ServerName $domain
  76.             ServerAlias $domain
  77.             DocumentRoot $rootDir
  78.             <Directory />
  79.                 AllowOverride All
  80.             </Directory>
  81.             <Directory $rootDir>
  82.                 Options Indexes FollowSymLinks MultiViews
  83.                 AllowOverride all
  84.                 Require all granted
  85.             </Directory>
  86.             ErrorLog /var/log/apache2/$domain-error.log
  87.             LogLevel error
  88.             CustomLog /var/log/apache2/$domain-access.log combined
  89.         </VirtualHost>" > $sitesAvailabledomain
  90.         then
  91.             echo -e $"There is an ERROR creating $domain file"
  92.             exit;
  93.         else
  94.             echo -e $"\nNew Virtual Host Created\n"
  95.         fi
  96.  
  97.         ### Add domain in /etc/hosts
  98.         if ! echo "127.0.0.1    $domain" >> /etc/hosts
  99.         then
  100.             echo $"ERROR: Not able to write in /etc/hosts"
  101.             exit;
  102.         else
  103.             echo -e $"Host added to /etc/hosts file \n"
  104.         fi
  105.  
  106.         ### Add domain in /mnt/c/Windows/System32/drivers/etc/hosts (Windows Subsytem for Linux)
  107.         if [ -e /mnt/c/Windows/System32/drivers/etc/hosts ]
  108.         then
  109.             if ! echo -e "\r127.0.0.1       $domain" >> /mnt/c/Windows/System32/drivers/etc/hosts
  110.             then
  111.                 echo $"ERROR: Not able to write in /mnt/c/Windows/System32/drivers/etc/hosts (Hint: Try running Bash as administrator)"
  112.             else
  113.                 echo -e $"Host added to /mnt/c/Windows/System32/drivers/etc/hosts file \n"
  114.             fi
  115.         fi
  116.  
  117.         if [ "$owner" == "" ]; then
  118.             iam=$(whoami)
  119.             if [ "$iam" == "root" ]; then
  120.                 chown -R $apacheUser:$apacheUser $rootDir
  121.             else
  122.                 chown -R $iam:$iam $rootDir
  123.             fi
  124.         else
  125.             chown -R $owner:$owner $rootDir
  126.         fi
  127.  
  128.         ### enable website
  129.         a2ensite $domain
  130.  
  131.         ### restart Apache
  132.         /etc/init.d/apache2 reload
  133.  
  134.         ### show the finished message
  135.         echo -e $"Complete! \nYou now have a new Virtual Host \nYour new host is: http://$domain \nAnd its located at $rootDir"
  136.         exit;
  137.     else
  138.         ### check whether domain already exists
  139.         if ! [ -e $sitesAvailabledomain ]; then
  140.             echo -e $"This domain does not exist.\nPlease try another one"
  141.             exit;
  142.         else
  143.             ### Delete domain in /etc/hosts
  144.             newhost=${domain//./\\.}
  145.             sed -i "/$newhost/d" /etc/hosts
  146.  
  147.             ### Delete domain in /mnt/c/Windows/System32/drivers/etc/hosts (Windows Subsytem for Linux)
  148.             if [ -e /mnt/c/Windows/System32/drivers/etc/hosts ]
  149.             then
  150.                 newhost=${domain//./\\.}
  151.                 sed -i "/$newhost/d" /mnt/c/Windows/System32/drivers/etc/hosts
  152.             fi
  153.  
  154.             ### disable website
  155.             a2dissite $domain
  156.  
  157.             ### restart Apache
  158.             /etc/init.d/apache2 reload
  159.  
  160.             ### Delete virtual host rules files
  161.             rm $sitesAvailabledomain
  162.         fi
  163.  
  164.         ### check if directory exists or not
  165.         if [ -d $rootDir ]; then
  166.             echo -e $"Delete host root directory ? (y/n)"
  167.             read deldir
  168.  
  169.             if [ "$deldir" == 'y' -o "$deldir" == 'Y' ]; then
  170.                 ### Delete the directory
  171.                 rm -rf $rootDir
  172.                 echo -e $"Directory deleted"
  173.             else
  174.                 echo -e $"Host directory conserved"
  175.             fi
  176.         else
  177.             echo -e $"Host directory not found. Ignored"
  178.         fi
  179.  
  180.         ### show the finished message
  181.         echo -e $"Complete!\nYou just removed Virtual Host $domain"
  182.         exit 0;
  183. fi
Advertisement
Add Comment
Please, Sign In to add comment