]> git.mxchange.org Git - friendica.git/blob - util/vagrant_vhost.sh
typo
[friendica.git] / util / vagrant_vhost.sh
1 #!/usr/bin/env bash
2
3 # Run this as sudo!
4 # I move this file to /usr/local/bin/vhost 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     -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".
26
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
30
31 _EOF_
32 exit 1
33 }
34
35
36 #
37 #   Output vHost skeleton, fill with userinput
38 #   To be outputted into new file
39 #
40 function create_vhost {
41 cat <<- _EOF_
42 <VirtualHost *:80>
43     ServerAdmin webmaster@localhost
44     ServerName $ServerName
45     $ServerAlias
46
47     DocumentRoot $DocumentRoot
48
49
50     <Directory $DocumentRoot>
51        Options Indexes FollowSymLinks MultiViews
52        AllowOverride All 
53        Order allow,deny
54        allow from all
55     </Directory>
56
57     ErrorLog \${APACHE_LOG_DIR}/$ServerName-error.log
58
59     # Possible values include: debug, info, notice, warn, error, crit,
60     # alert, emerg.
61     LogLevel warn
62
63     CustomLog \${APACHE_LOG_DIR}/$ServerName-access.log combined
64
65
66 </VirtualHost>
67 _EOF_
68 }
69
70 function create_ssl_vhost {
71 cat <<- _EOF_
72 <VirtualHost *:443>
73     ServerAdmin webmaster@localhost
74     ServerName $ServerName
75     $ServerAlias
76
77     DocumentRoot $DocumentRoot
78
79     <Directory $DocumentRoot>
80        Options Indexes FollowSymLinks MultiViews
81        AllowOverride All 
82        Order allow,deny
83        allow from all
84     </Directory>
85
86     ErrorLog \${APACHE_LOG_DIR}/$ServerName-error.log
87
88     # Possible values include: debug, info, notice, warn, error, crit,
89     # alert, emerg.
90     LogLevel warn
91
92     CustomLog \${APACHE_LOG_DIR}/$ServerName-access.log combined
93
94     SSLEngine on
95
96     SSLCertificateFile  $CertPath/$CertName.crt
97     SSLCertificateKeyFile $CertPath/$CertName.key
98
99     <FilesMatch "\.(cgi|shtml|phtml|php)$">
100         SSLOptions +StdEnvVars
101     </FilesMatch>
102
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
108 </VirtualHost>
109 _EOF_
110 }
111
112 #Sanity Check - are there two arguments with 2 values?
113 if [ "$#" -lt 4 ]; then
114     show_usage
115 fi
116
117 CertPath=""
118
119 #Parse flags
120 while getopts "d:s:a:p:c:h" OPTION; do
121     case $OPTION in
122         h)
123             show_usage
124             ;;
125         d)
126             DocumentRoot=$OPTARG
127             ;;
128         s)
129             ServerName=$OPTARG
130             ;;
131         a)
132             Alias=$OPTARG
133             ;;
134         p)
135             CertPath=$OPTARG
136             ;;
137         c)
138             CertName=$OPTARG
139             ;;
140         *)
141             show_usage
142             ;;
143     esac
144 done
145
146 # If alias is set:
147 if [ "$Alias" != "" ]; then
148     ServerAlias="ServerAlias "$Alias
149 else
150     ServerAlias=""
151 fi
152
153 # If CertName doesn't get set, set it to ServerName
154 if [ "$CertName" == "" ]; then
155     CertName=$ServerName
156 fi
157
158 if [ ! -d $DocumentRoot ]; then
159     mkdir -p $DocumentRoot
160     #chown USER:USER $DocumentRoot #POSSIBLE IMPLEMENTATION, new flag -u ?
161 fi
162
163 if [ -f "$DocumentRoot/$ServerName.conf" ]; then
164     echo 'vHost already exists. Aborting'
165     show_usage
166 else
167     create_vhost > /etc/apache2/sites-available/${ServerName}.conf
168
169     # Add :443 handling
170     if [ "$CertPath" != "" ]; then
171         create_ssl_vhost >> /etc/apache2/sites-available/${ServerName}.conf
172     fi
173
174     # Enable Site
175     cd /etc/apache2/sites-available/ && a2ensite ${ServerName}.conf
176     service apache2 reload
177 fi