Guest User

Untitled

a guest
Nov 20th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. #! /bin/bash
  2.  
  3. # May need to run this as sudo!
  4. # I have it in /usr/local/bin and run command 'vhost' from anywhere, using sudo.
  5.  
  6. #
  7. # Show Usage, Output to STDERR
  8. #
  9. function show_usage {
  10. cat <<- _EOF_
  11.  
  12. Create a new vHost in Ubuntu Server
  13. Assumes /etc/apache2/sites-available and /etc/apache2/sites-enabled setup used
  14.  
  15. -d DocumentRoot - i.e. /var/www/yoursite
  16. -h Help - Show this menu.
  17. -s ServerName - i.e. example.com or sub.example.com
  18.  
  19. how: sudo vhost -d /var/www/myproject -s myproject.local
  20.  
  21. _EOF_
  22. exit 1
  23. }
  24.  
  25.  
  26. #
  27. # Output vHost skeleton, fill with userinput
  28. # To be outputted into new file
  29. #
  30. function create_vhost {
  31. cat <<- _EOF_
  32. <VirtualHost *:80>
  33. ServerAdmin webmaster@localhost
  34. ServerName $ServerName
  35. ServerAlias www.$ServerName
  36.  
  37. DocumentRoot $DocumentRoot
  38.  
  39. <Directory $DocumentRoot>
  40. Options -Indexes +FollowSymLinks +MultiViews
  41. AllowOverride All
  42. Order allow,deny
  43. Allow from all
  44. Require all granted
  45. </Directory>
  46.  
  47. ErrorLog \${APACHE_LOG_DIR}/$ServerName-error.log
  48.  
  49. # Possible values include: debug, info, notice, warn, error, crit,
  50. # alert, emerg.
  51. LogLevel warn
  52.  
  53. CustomLog \${APACHE_LOG_DIR}/$ServerName-access.log combined
  54.  
  55.  
  56. </VirtualHost>
  57. _EOF_
  58. }
  59.  
  60. #Sanity Check - are there two arguments with 2 values?
  61. if [ $# -ne 4 ]; then
  62. show_usage
  63. fi
  64.  
  65. #Parse flags
  66. while getopts "d:s:" OPTION; do
  67. case $OPTION in
  68. h)
  69. show_usage
  70. ;;
  71. d)
  72. DocumentRoot=$OPTARG
  73. ;;
  74. s)
  75. ServerName=$OPTARG
  76. ;;
  77. *)
  78. show_usage
  79. ;;
  80. esac
  81. done
  82.  
  83. if [ ! -d $DocumentRoot ]; then
  84. mkdir -p $DocumentRoot
  85. #chown USER:USER $DocumentRoot #POSSIBLE IMPLEMENTATION, new flag -u ?
  86. fi
  87.  
  88. if [ -f "$DocumentRoot/$ServerName.conf" ]; then
  89. echo 'vHost already exists. Aborting'
  90. show_usage
  91. else
  92. sudo sh -c "echo \"127.0.0.0 ${ServerName}\" >> \/tmp\/hosts"
  93. create_vhost > /etc/apache2/sites-available/${ServerName}.conf
  94. cd /etc/apache2/sites-available/ && a2ensite ${ServerName}.conf #Enable site
  95. service apache2 reload #Optional implementation
  96. fi
Add Comment
Please, Sign In to add comment