4 # I move this file to /usr/local/bin/vhost and run command 'vhost' from anywhere, using sudo.
7 # Show Usage, Output to STDERR
12 Create a new vHost in Ubuntu Server
13 Assumes /etc/apache2/sites-available and /etc/apache2/sites-enabled setup used
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 -a ServerAlias - i.e. *.example.com or another domain altogether
19 -p File path to the SSL certificate. Directories only, no file name.
20 If using an SSL Certificate, also creates a port :443 vhost as well.
21 This *ASSUMES* a .crt and a .key file exists
22 at file path /provided-file-path/your-server-or-cert-name.[crt|key].
23 Otherwise you can except Apache errors when you reload Apache.
24 Ensure Apache's mod_ssl is enabled via "sudo a2enmod ssl".
25 -c Certificate filename. "xip.io" becomes "xip.io.key" and "xip.io.crt".
27 Example Usage. Serve files from /var/www/xip.io at http(s)://192.168.33.10.xip.io
28 using ssl files from /etc/ssl/xip.io/xip.io.[key|crt]
29 sudo vhost -d /var/www/xip.io -s 192.168.33.10.xip.io -p /etc/ssl/xip.io -c xip.io
37 # Output vHost skeleton, fill with userinput
38 # To be outputted into new file
40 function create_vhost {
43 ServerAdmin webmaster@localhost
44 ServerName $ServerName
47 DocumentRoot $DocumentRoot
50 <Directory $DocumentRoot>
51 Options Indexes FollowSymLinks MultiViews
57 ErrorLog \${APACHE_LOG_DIR}/$ServerName-error.log
59 # Possible values include: debug, info, notice, warn, error, crit,
63 CustomLog \${APACHE_LOG_DIR}/$ServerName-access.log combined
70 function create_ssl_vhost {
73 ServerAdmin webmaster@localhost
74 ServerName $ServerName
77 DocumentRoot $DocumentRoot
79 <Directory $DocumentRoot>
80 Options Indexes FollowSymLinks MultiViews
86 ErrorLog \${APACHE_LOG_DIR}/$ServerName-error.log
88 # Possible values include: debug, info, notice, warn, error, crit,
92 CustomLog \${APACHE_LOG_DIR}/$ServerName-access.log combined
96 SSLCertificateFile $CertPath/$CertName.crt
97 SSLCertificateKeyFile $CertPath/$CertName.key
99 <FilesMatch "\.(cgi|shtml|phtml|php)$">
100 SSLOptions +StdEnvVars
103 BrowserMatch "MSIE [2-6]" \\
104 nokeepalive ssl-unclean-shutdown \\
105 downgrade-1.0 force-response-1.0
106 # MSIE 7 and newer should be able to use keepalive
107 BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
112 #Sanity Check - are there two arguments with 2 values?
113 if [ "$#" -lt 4 ]; then
120 while getopts "d:s:a:p:c:h" OPTION; do
147 if [ "$Alias" != "" ]; then
148 ServerAlias="ServerAlias "$Alias
153 # If CertName doesn't get set, set it to ServerName
154 if [ "$CertName" == "" ]; then
158 if [ ! -d $DocumentRoot ]; then
159 mkdir -p $DocumentRoot
160 #chown USER:USER $DocumentRoot #POSSIBLE IMPLEMENTATION, new flag -u ?
163 if [ -f "$DocumentRoot/$ServerName.conf" ]; then
164 echo 'vHost already exists. Aborting'
167 create_vhost > /etc/apache2/sites-available/${ServerName}.conf
170 if [ "$CertPath" != "" ]; then
171 create_ssl_vhost >> /etc/apache2/sites-available/${ServerName}.conf
175 cd /etc/apache2/sites-available/ && a2ensite ${ServerName}.conf
176 service apache2 reload