Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
4.3 KiB

  1. #!/usr/bin/env bash
  2. # Run this as sudo!
  3. # I move this file to /usr/local/bin/vhost and run command 'vhost' from anywhere, using sudo.
  4. #
  5. # Show Usage, Output to STDERR
  6. #
  7. function show_usage {
  8. cat <<- _EOF_
  9. Create a new vHost in Ubuntu Server
  10. Assumes /etc/apache2/sites-available and /etc/apache2/sites-enabled setup used
  11. -d DocumentRoot - i.e. /var/www/yoursite
  12. -h Help - Show this menu.
  13. -s ServerName - i.e. example.com or sub.example.com
  14. -a ServerAlias - i.e. *.example.com or another domain altogether
  15. -p File path to the SSL certificate. Directories only, no file name.
  16. If using an SSL Certificate, also creates a port :443 vhost as well.
  17. This *ASSUMES* a .crt and a .key file exists
  18. at file path /provided-file-path/your-server-or-cert-name.[crt|key].
  19. Otherwise you can except Apache errors when you reload Apache.
  20. Ensure Apache's mod_ssl is enabled via "sudo a2enmod ssl".
  21. -c Certificate filename. "xip.io" becomes "xip.io.key" and "xip.io.crt".
  22. Example Usage. Serve files from /var/www/xip.io at http(s)://192.168.33.10.xip.io
  23. using ssl files from /etc/ssl/xip.io/xip.io.[key|crt]
  24. sudo vhost -d /var/www/xip.io -s 192.168.33.10.xip.io -p /etc/ssl/xip.io -c xip.io
  25. _EOF_
  26. exit 1
  27. }
  28. #
  29. # Output vHost skeleton, fill with userinput
  30. # To be outputted into new file
  31. #
  32. function create_vhost {
  33. cat <<- _EOF_
  34. <VirtualHost *:80>
  35. ServerAdmin webmaster@localhost
  36. ServerName $ServerName
  37. $ServerAlias
  38. DocumentRoot $DocumentRoot
  39. <Directory $DocumentRoot>
  40. Options Indexes FollowSymLinks MultiViews
  41. AllowOverride All
  42. Order allow,deny
  43. allow from all
  44. </Directory>
  45. ErrorLog \${APACHE_LOG_DIR}/$ServerName-error.log
  46. # Possible values include: debug, info, notice, warn, error, crit,
  47. # alert, emerg.
  48. LogLevel warn
  49. CustomLog \${APACHE_LOG_DIR}/$ServerName-access.log combined
  50. </VirtualHost>
  51. _EOF_
  52. }
  53. function create_ssl_vhost {
  54. cat <<- _EOF_
  55. <VirtualHost *:443>
  56. ServerAdmin webmaster@localhost
  57. ServerName $ServerName
  58. $ServerAlias
  59. DocumentRoot $DocumentRoot
  60. <Directory $DocumentRoot>
  61. Options Indexes FollowSymLinks MultiViews
  62. AllowOverride All
  63. Order allow,deny
  64. allow from all
  65. </Directory>
  66. ErrorLog \${APACHE_LOG_DIR}/$ServerName-error.log
  67. # Possible values include: debug, info, notice, warn, error, crit,
  68. # alert, emerg.
  69. LogLevel warn
  70. CustomLog \${APACHE_LOG_DIR}/$ServerName-access.log combined
  71. SSLEngine on
  72. SSLCertificateFile $CertPath/$CertName.crt
  73. SSLCertificateKeyFile $CertPath/$CertName.key
  74. <FilesMatch "\.(cgi|shtml|phtml|php)$">
  75. SSLOptions +StdEnvVars
  76. </FilesMatch>
  77. BrowserMatch "MSIE [2-6]" \\
  78. nokeepalive ssl-unclean-shutdown \\
  79. downgrade-1.0 force-response-1.0
  80. # MSIE 7 and newer should be able to use keepalive
  81. BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
  82. </VirtualHost>
  83. _EOF_
  84. }
  85. #Sanity Check - are there two arguments with 2 values?
  86. if [ "$#" -lt 4 ]; then
  87. show_usage
  88. fi
  89. CertPath=""
  90. #Parse flags
  91. while getopts "d:s:a:p:c:h" OPTION; do
  92. case $OPTION in
  93. h)
  94. show_usage
  95. ;;
  96. d)
  97. DocumentRoot=$OPTARG
  98. ;;
  99. s)
  100. ServerName=$OPTARG
  101. ;;
  102. a)
  103. Alias=$OPTARG
  104. ;;
  105. p)
  106. CertPath=$OPTARG
  107. ;;
  108. c)
  109. CertName=$OPTARG
  110. ;;
  111. *)
  112. show_usage
  113. ;;
  114. esac
  115. done
  116. # If alias is set:
  117. if [ "$Alias" != "" ]; then
  118. ServerAlias="ServerAlias "$Alias
  119. else
  120. ServerAlias=""
  121. fi
  122. # If CertName doesn't get set, set it to ServerName
  123. if [ "$CertName" == "" ]; then
  124. CertName=$ServerName
  125. fi
  126. if [ ! -d $DocumentRoot ]; then
  127. mkdir -p $DocumentRoot
  128. #chown USER:USER $DocumentRoot #POSSIBLE IMPLEMENTATION, new flag -u ?
  129. fi
  130. if [ -f "$DocumentRoot/$ServerName.conf" ]; then
  131. echo 'vHost already exists. Aborting'
  132. show_usage
  133. else
  134. create_vhost > /etc/apache2/sites-available/${ServerName}.conf
  135. # Add :443 handling
  136. if [ "$CertPath" != "" ]; then
  137. create_ssl_vhost >> /etc/apache2/sites-available/${ServerName}.conf
  138. fi
  139. # Enable Site
  140. cd /etc/apache2/sites-available/ && a2ensite ${ServerName}.conf
  141. service apache2 reload
  142. fi