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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.