Virtual Host Manager For Ubuntu


/ Published in: Bash
Save to your folder(s)

This is a bash file to Create or Delete Virtual Host in Ubuntu system and Apache server.
This file open the Virtual Host in "/home/{user}/www/{domain}"

To run this file you need to make this file executable:
> sudo chmod u+x /path/to/file

Then whenever you need to open a new host just go to terminal and run the file:
> sudo /path/to/file create domain.dev.com

To delete host:
> sudo /path/to/file delete domain.dev.com


Copy this code and paste it in your HTML
  1. #!/bin/bash
  2. ### Set default parameters
  3. action=$1
  4. domain=$2
  5. owner=$(who am i | awk '{print $1}')
  6. sitesEnable='/etc/apache2/sites-enabled/'
  7. sitesAvailable='/etc/apache2/sites-available/'
  8. userDir=$(eval echo ~${SUDO_USER})'/www/'
  9.  
  10. if [ "$(whoami)" != 'root' ]; then
  11. echo "You have no permission to run $0 as non-root user. Use sudo !!!"
  12. exit 1;
  13. fi
  14.  
  15. if [ "$action" != 'create' ] && [ "$action" != 'delete' ]
  16. then
  17. echo "You need to prompt for action (create or delete) -- Lower-case only !!!!!!"
  18. exit 1;
  19. fi
  20.  
  21. while [ "$domain" == "" ]
  22. do
  23. echo -e "Please provide domain. e.g.dev,staging"
  24. read domain
  25. done
  26.  
  27. rootdir=${domain//./}
  28.  
  29. if [ "$action" == 'create' ]
  30. then
  31. ### check if domain already exists
  32. if [ -e $sitesAvailable$domain ]; then
  33. echo -e 'This domain already exists.\nPlease Try Another one'
  34. exit;
  35. fi
  36.  
  37. ### check if directory exists or not
  38. if [ -d $userDir$rootdir ]; then
  39. echo -e 'Directory already exists !'
  40. exit;
  41. fi
  42.  
  43. ### create the directory
  44. mkdir $userDir$rootdir
  45.  
  46. ### create virtual host rules file
  47. if ! echo "<VirtualHost *:80>
  48. ServerAdmin $email
  49. ServerName $domain
  50. ServerAlias $domain www.$domain
  51. DocumentRoot $userDir$rootdir
  52. <Directory />
  53. AllowOverride All
  54. </Directory>
  55. <Directory $rootdir>
  56. AllowOverride All
  57. </Directory>
  58. ErrorLog /var/log/apache2/$domain
  59. LogLevel error
  60. CustomLog /var/log/apache2/$domain custom
  61. </VirtualHost>" > $sitesAvailable$domain
  62. then
  63. echo -e 'There is an ERROR create $domain file'
  64. exit;
  65. else
  66. echo -e '\nNew Virtual Host Created\n'
  67. fi
  68.  
  69. ### Add domain in /etc/hosts
  70. if ! echo "127.0.0.1 $domain" >> /etc/hosts
  71. then
  72. echo "ERROR: Not able write in /etc/hosts"
  73. exit;
  74. else
  75. echo -e "Host added to /etc/hosts file \n"
  76. fi
  77.  
  78. ### enable website
  79. a2ensite $domain
  80.  
  81. ### restart Apache
  82. /etc/init.d/apache2 reload
  83.  
  84. ### give permission to root dir
  85. chmod 755 $userDir$rootdir
  86.  
  87. ### write test file in the new domain dir
  88. if ! echo "<?php echo phpinfo(); ?>" > $userDir$rootdir/phpinfo.php
  89. then
  90. echo "ERROR: Not able to write in file "$userDir"/"$rootdir"/phpinfo.php. Please check permissions."
  91. exit;
  92. else
  93. echo "Added content to "$userDir$rootdir"/phpinfo.php."
  94. fi
  95.  
  96. if [ "$owner" == "" ]; then
  97. chown -R $(whoami):$(whoami) $userDir$rootdir
  98. else
  99. chown -R $owner:$owner $userDir$rootdir
  100. fi
  101.  
  102. ### show the finished message
  103. echo -e "Complete!
  104. You now have a new Virtual Host
  105. Your new host is: http://"$domain"
  106. And its located at "$userDir$rootdir
  107. exit;
  108. else
  109. ### check whether domain already exists
  110. if ! [ -e $sitesAvailable$domain ]; then
  111. echo -e 'This domain dont exists.\nPlease Try Another one'
  112. exit;
  113. fi
  114.  
  115. ### check if directory exists or not
  116. if ! [ -d $userDir$rootdir ]; then
  117. echo -e 'Directory not exists !'
  118. exit;
  119. fi
  120.  
  121. ### Delete the directory
  122. rm -rf $userDir$rootdir
  123.  
  124. ### Delete virtual host rules file
  125. rm $sitesAvailable$domain
  126.  
  127. ### Delete domain in /etc/hosts
  128. newhost=${domain//./\\.}
  129. sed -i "/$newhost/d" /etc/hosts
  130.  
  131. ### enable website
  132. rm $sitesEnable$domain
  133.  
  134. ### restart Apache
  135. /etc/init.d/apache2 reload
  136.  
  137. ### show the finished message
  138. echo -e "Complete!
  139. You just removed Virtual Host "$domain
  140. exit 0;
  141. fi

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.