572176d711980295c1a6ba566c527b683a7e8aed
[myplc.git] / plc.d / httpd
1 #!/bin/bash
2 #
3 # priority: 600
4 #
5 # Configure Apache web server
6 #
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
9 #
10
11 # Source function library and configuration
12 . /etc/plc.d/functions
13 . /etc/planetlab/plc_config
14
15 # Be verbose
16 set -x
17
18 # Default locations
19 DocumentRoot=/var/www/html
20 php_ini=/etc/php.ini
21 httpd_conf=/etc/httpd/conf/httpd.conf
22 ssl_conf=/etc/httpd/conf.d/ssl.conf
23 plc_conf=/etc/httpd/conf.d/plc.conf
24 php_fpm_conf=/etc/php-fpm.d/plc.conf
25 php_fpm_dir=$(dirname $php_fpm_conf)
26
27 function disable_file () {
28     file=$1; shift
29     [ -f $file ] && mv -f $file $file.disabled
30 }
31 function enable_file () {
32     file=$1; shift
33     [ ! -f $file ] && mv -f $file.disabled $file
34 }
35
36 case "$1" in
37     start)
38         if [ "$PLC_API_ENABLED" != "1" -a \
39              "$PLC_BOOT_ENABLED" != "1" -a \
40              "$PLC_WWW_ENABLED" != "1" ] ; then
41             exit 0
42         fi
43
44         MESSAGE=$"Starting web server"
45         dialog "$MESSAGE"
46
47         # set document root - not really useful on fedora but just in case
48         sed -i -e "s@^DocumentRoot.*@DocumentRoot \"$DocumentRoot\"@" $httpd_conf
49         # whether WWW is enabled or not : 
50         if [ "$PLC_WWW_ENABLED" != "1" ] ; then
51             # avoid hitting drupal, that would try to connect to the db and create noise
52             disable_file $DocumentRoot/index.php
53         else
54             enable_file $DocumentRoot/index.php
55         fi
56
57         # Set the default include path
58         include_path=".:$DocumentRoot/planetlab/includes:$DocumentRoot/plekit/php:$DocumentRoot/generated:/etc/planetlab/php:/usr/share/plc_api/php"
59         sed -i -e "s@[;]*include_path = \"\.:.*\"@include_path = \"$include_path\"@" $php_ini
60
61         # Set open_basedir so as to avoid leaks
62         open_basedir="$DocumentRoot:/etc/planetlab/php:/usr/share/plc_api/php:/var/log/myslice:/var/tmp/bootmedium:/var/log/bm:/tmp"
63         sed -i -e "s@[;]*open_basedir =.*@open_basedir = \"$open_basedir\"@" $php_ini
64         
65         # for php-5.3 under fedora12, otherwise issues tons of warning messages
66         # Set timezone in php.ini if not already there
67         if grep '^;date.timezone' $php_ini >& /dev/null; then
68             dialog 'Setting PHP timezone to GMT'
69             sed -i -e 's,^;date.timezone.*,date.timezone = GMT,' $php_ini
70         fi
71
72         if grep '^short_open_tag = Off' $php_ini >& /dev/null; then
73             sed -i -e 's,^short_open_tag = Off,short_open_tag = On,' $php_ini
74         fi
75
76         ## patch php.ini
77         # memory limit
78         sed -i -e 's,^memory_limit = 32M *;,memory_limit = 80M ; patch myplc -- ,' $php_ini 
79         # log_errors : is On by default
80         # error_log
81         if ! grep '^error_log *=' $php_ini > /dev/null ; then
82           echo 'error_log = /var/log/php.log' >> $php_ini
83           touch /var/log/php.log
84           chmod 666 /var/log/php.log
85         fi
86
87         # configure php-fpm as well if present (starting with f27)
88         [ -d $php_fpm_dir ] && cat > $php_fpm_conf << EOF
89 [www]
90 php_value[include_path] = $include_path
91 php_value[open_basedir] = $open_basedir
92 php_value[date.timezone] = GMT
93 php_value[short_open_tag] = On
94 php_value[memory_limit] = 80M
95 EOF
96
97         # Disable default Listen directive
98         sed -i -e '/^Listen/d' $httpd_conf
99
100         plc_api_path_noslash=$(echo $PLC_API_PATH | sed -e s,/,,g)
101         # Set the port numbers
102         for server in WWW API BOOT ; do
103             enabled=PLC_${server}_ENABLED
104             if [ "${!enabled}" != "1" ] ; then
105                 continue
106             fi
107             hostname=PLC_${server}_HOST
108             http_port=PLC_${server}_PORT
109             https_port=PLC_${server}_SSL_PORT
110
111             # API should always be accessed via SSL
112             if [ "$server" = "API" ] ; then
113                 https_port=${!http_port}
114                 http_port=
115             fi
116
117             # Check if we are already listening on these ports
118             skip_http=0
119             skip_https=0
120             for previous_server in WWW API BOOT ; do
121                 if [ "$server" = "$previous_server" ] ; then
122                     break
123                 fi
124                 previous_enabled=PLC_${previous_server}_ENABLED
125                 if [ "${!previous_enabled}" != "1" ] ; then
126                     continue
127                 fi
128                 previous_http_port=PLC_${previous_server}_PORT
129                 previous_https_port=PLC_${previous_server}_SSL_PORT
130
131                 if [ -z "${http_port}" ]; then
132                     skip_http=1;
133                 elif [ -z "${!http_port}" ]; then
134                     skip_http=1;                    
135                 elif [ "${!http_port}" = "${!previous_http_port}" ] ; then
136                     skip_http=1
137                 fi
138                 if [ -z "${https_port}" ]; then
139                     skip_https=1
140                 elif [ -z "${!https_port}" ]; then
141                     skip_https=1
142                 elif [ "${!https_port}" = "${!previous_https_port}" ] ; then
143                     skip_https=1
144                 fi
145             done
146
147             # HTTP configuration
148             if [ $skip_http -eq 0 ] ; then
149                 cat <<EOF
150 Listen ${!http_port}
151 <VirtualHost *:${!http_port}>
152     # Make sure that the admin web pages are always accessed via SSL
153     Redirect /db https://$PLC_WWW_HOST:$PLC_WWW_SSL_PORT/db
154     Redirect /planetlab https://$PLC_WWW_HOST:$PLC_WWW_SSL_PORT/planetlab
155 # as a matter of fact most xmlrpc clients won't follow the redirection
156 # so this is mostly rethorical, but just in case...
157    Redirect /$plc_api_path_noslash https://$PLC_WWW_HOST:$PLC_WWW_SSL_PORT/$plc_api_path_noslash
158 </VirtualHost>
159
160 EOF
161             fi
162
163             # HTTPS configuration
164             if [ $skip_https -eq 0 ] ; then
165                 # XXX Cannot support NameVirtualHost over SSL. If
166                 # the API, boot, and web servers are all running
167                 # on the same machine, the web server certificate
168                 # takes precedence.
169                 sed -i \
170                     -e "s/^Listen .*/Listen ${!https_port}/" \
171                     -e "s/<VirtualHost _default_:.*>/<VirtualHost _default_:${!https_port}>/" \
172                     $ssl_conf
173                 # this is used to locate the right certificates
174                 server_lower=$(echo $server | tr 'A-Z' 'a-z')
175
176                 # which one is used is currently configured in myplc.spec,
177                 # with mod_python preferred
178                 if rpm -q mod_python >& /dev/null ; then
179                     configure_for_mod_python=true
180                 elif rpm -q mod_wsgi >& /dev/null ; then
181                     configure_for_mod_wsgi=true
182                 else
183                     echo "Requires mod_python or mod_wsgi.... exiting"
184                     exit 1
185                 fi
186
187                 # It would be tempting to use <IfModule> here 
188                 # but early tests showed this could be tricky/fragile
189                 # So let's hard-wire it for one module
190                 # A lot of trial-and -error was involved in getting this that way...
191
192                 if [ -n "$configure_for_mod_python" ] ; then
193 #################### for mod_python
194                     cat <<EOF
195 # mod_python location
196 <Location /PLCAPI/>
197     SetHandler mod_python
198     PythonPath "sys.path + ['/usr/share/plc_api']"
199     PythonHandler apache.ModPython
200 </Location>
201 EOF
202
203                 elif [ -n "$configure_for_mod_wsgi" ] ; then
204 #################### for mod_wsgi
205                     cat <<EOF
206 # create wsgi socket where we have the permission
207 WSGISocketPrefix run/wsgi
208
209 <VirtualHost *:${!https_port}>
210
211    # SSL
212    SSLEngine On
213    SSLCertificateFile /etc/planetlab/${server_lower}_ssl.crt
214    SSLCertificateKeyFile /etc/planetlab/${server_lower}_ssl.key
215    SSLCertificateChainFile /etc/planetlab/${server_lower}_ca_ssl.crt
216
217    WSGIScriptAlias /$plc_api_path_noslash /usr/share/plc_api/apache/plc.wsgi
218 # xxx would be cool to be able to tweak this through config
219    WSGIDaemonProcess plcapi-wsgi-ssl user=apache group=apache processes=1 threads=25
220    WSGIProcessGroup plcapi-wsgi-ssl
221
222    <Directory "/usr/share/plc_api/apache">
223       Options +ExecCGI
224       $(apache_allow)
225    </Directory>
226
227 </VirtualHost>
228 EOF
229                 fi
230             fi
231         done >$plc_conf
232
233         # Set custom Apache directives
234         (
235             # could be restricted to boot boxes but harmless..
236             cat <<EOF
237 AddType application/octet-stream .iso
238 AddType application/octet-stream .usb
239 EOF
240             # make sure /PLCAPI can't get accessed if API not enabled here
241             if [ "$PLC_API_ENABLED" != "1" ] ; then
242                 cat <<EOF
243 # mod_wsgi location
244 <Location $PLC_API_PATH>
245     $(apache_forbid)
246 </Location> 
247 EOF
248             fi
249
250             # redirect www requests if not on the right server
251             if [ "$PLC_WWW_ENABLED" != "1" ] ; then
252                 cat <<EOF
253 Redirect /index.html http://$PLC_WWW_HOST:$PLC_WWW_PORT/
254 EOF
255             fi
256         ) >>$plc_conf
257
258         # Make alpina-logs directory writable for bootmanager log upload
259         chown apache:apache $DocumentRoot/alpina-logs/nodes
260
261         # Make the Drupal files upload directory owned by Apache
262         mkdir -p $DocumentRoot/files
263         chown apache:apache $DocumentRoot/files
264
265         # Symlink any (real) files or directories in
266         # /data/var/www/html/* to /var/www/html/. We could descend
267         # into subdirectories, but the code to do so properly would be
268         # madness.
269         for file in /data/$DocumentRoot/* ; do
270             if [ -e "$file" -a ! -h "$file" ] ; then
271                 base=$(basename "$file")
272                 if [ ! -e "$DocumentRoot/$base" ] ; then
273                     ln -nsf "$file" "$DocumentRoot/$base"
274                 fi
275             fi
276         done
277
278         # Cleanup broken symlinks
279         for file in $DocumentRoot/* ; do
280             if [ -h "$file" -a ! -e "$file" ] ; then
281                 rm -f "$file"
282             fi
283         done
284
285         # Old style PHP constants
286         mkdir -p /etc/planetlab/php
287         cat >/etc/planetlab/php/site_constants.php <<"EOF"
288 <?php
289 include('plc_config.php');
290
291 define('PL_API_SERVER', PLC_API_HOST);
292 define('PL_API_PATH', PLC_API_PATH);
293 define('PL_API_PORT', PLC_API_PORT);
294 define('PL_API_CAPABILITY_AUTH_METHOD', 'capability');
295 define('PL_API_CAPABILITY_PASS', PLC_API_MAINTENANCE_PASSWORD);
296 define('PL_API_CAPABILITY_USERNAME', PLC_API_MAINTENANCE_USER);
297 define('WWW_BASE', PLC_WWW_HOST);
298 define('BOOT_BASE', PLC_BOOT_HOST);
299 define('DEBUG', PLC_WWW_DEBUG);
300 define('API_CALL_DEBUG', PLC_API_DEBUG);
301 define('SENDMAIL', PLC_MAIL_ENABLED);
302 define('PLANETLAB_SUPPORT_EMAIL', PLC_NAME . ' Support <' . PLC_MAIL_SUPPORT_ADDRESS . '>');
303 define('PLANETLAB_SUPPORT_EMAIL_ONLY', PLC_MAIL_SUPPORT_ADDRESS);
304 ?>
305 EOF
306
307         ## make room for logs
308         touch /var/log/plcapi.log
309         chmod 666 /var/log/plcapi.log
310
311         plc_daemon httpd
312         check
313
314         result "$MESSAGE"
315         ;;
316
317     stop)
318         MESSAGE=$"Stopping web server"
319         dialog "$MESSAGE"
320
321         killproc plc_httpd
322         check
323
324         result "$MESSAGE"
325         ;;
326 esac
327
328 exit $ERRORS