enable short_open_tag
[myplc.git] / plc.d / httpd
1 #!/bin/bash
2 #
3 # priority: 700
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 # $Id$
11 #
12
13 # Source function library and configuration
14 . /etc/plc.d/functions
15 . /etc/planetlab/plc_config
16
17 # Be verbose
18 set -x
19
20 # Default locations
21 DocumentRoot=/var/www/html
22 php_ini=/etc/php.ini
23 httpd_conf=/etc/httpd/conf/httpd.conf
24 ssl_conf=/etc/httpd/conf.d/ssl.conf
25 plc_conf=/etc/httpd/conf.d/plc.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         # for php-5.3 under fedora12, otherwise issues tons of warning messages
62         # Set timezone in php.ini if not already there
63         if grep '^;date.timezone' $php_ini >& /dev/null; then
64             dialog 'Setting PHP timezone to GMT'
65             sed -i -e 's,^;date.timezone.*,date.timezone = GMT,' $php_ini
66         fi
67
68         if grep '^short_open_tag = Off' $php_ini >& /dev/null; then
69             sed -i -e 's,^short_open_tag = Off,short_open_tag = On,' $php_ini
70         fi
71
72         # Disable default Listen directive
73         sed -i -e '/^Listen/d' $httpd_conf
74
75         # Set the port numbers
76         for server in WWW API BOOT ; do
77             enabled=PLC_${server}_ENABLED
78             if [ "${!enabled}" != "1" ] ; then
79                 continue
80             fi
81             hostname=PLC_${server}_HOST
82             http_port=PLC_${server}_PORT
83             https_port=PLC_${server}_SSL_PORT
84
85             # API should always be accessed via SSL
86             if [ "$server" = "API" ] ; then
87                 https_port=${!http_port}
88                 http_port=
89             fi
90
91             # Check if we are already listening on these ports
92             skip_http=0
93             skip_https=0
94             for previous_server in WWW API BOOT ; do
95                 if [ "$server" = "$previous_server" ] ; then
96                     break
97                 fi
98                 previous_enabled=PLC_${previous_server}_ENABLED
99                 if [ "${!previous_enabled}" != "1" ] ; then
100                     continue
101                 fi
102                 previous_http_port=PLC_${previous_server}_PORT
103                 previous_https_port=PLC_${previous_server}_SSL_PORT
104
105                 if [ "${!http_port}" = "${!previous_http_port}" ] ; then
106                     skip_http=1
107                 fi
108                 if [ "${!https_port}" = "${!previous_https_port}" ] ; then
109                     skip_https=1
110                 fi
111             done
112
113             # HTTP configuration
114             if [ $skip_http -eq 0 -a -n "${!http_port}" ] ; then
115                 cat <<EOF
116 Listen ${!http_port}
117 # Make sure that the admin web pages and API are always accessed via SSL
118 <VirtualHost *:${!http_port}>
119     Redirect /db https://$PLC_WWW_HOST:$PLC_WWW_SSL_PORT/db
120     Redirect /planetlab https://$PLC_WWW_HOST:$PLC_WWW_SSL_PORT/planetlab
121     Redirect /$PLC_API_PATH https://$PLC_API_HOST:$PLC_API_PORT/$PLC_API_PATH
122 </VirtualHost>
123 EOF
124             fi
125
126             # HTTPS configuration
127             if [ $skip_https -eq 0 -a -n "${!https_port}" ] ; then
128                 # XXX Cannot support NameVirtualHost over SSL. If
129                 # the API, boot, and web servers are all running
130                 # on the same machine, the web server certificate
131                 # takes precedence.
132                 sed -i \
133                     -e "s/^Listen .*/Listen ${!https_port}/" \
134                     -e "s/<VirtualHost _default_:.*>/<VirtualHost _default_:${!https_port}>/" \
135                     $ssl_conf
136             fi
137         done >$plc_conf
138
139         # Set custom Apache directives
140         (
141             if [ "$PLC_API_ENABLED" = "1" ] ; then
142                 cat <<EOF
143 <Location $PLC_API_PATH>
144     SetHandler mod_python
145     PythonPath "sys.path + ['/usr/share/plc_api']"
146     PythonHandler ModPython
147 </Location>
148 EOF
149             else
150                 cat <<EOF
151 <Location $PLC_API_PATH>
152     Deny from all
153 </Location>
154 EOF
155             fi
156
157             if [ "$PLC_WWW_ENABLED" != "1" ] ; then
158                 cat <<EOF
159 Redirect /index.html http://$PLC_WWW_HOST:$PLC_WWW_PORT/
160 EOF
161             fi
162             cat <<EOF
163 AddType application/octet-stream .iso
164 AddType application/octet-stream .usb
165 EOF
166         ) >>$plc_conf
167
168         # Make alpina-logs directory writable for bootmanager log upload
169         chown apache:apache $DocumentRoot/alpina-logs/nodes
170
171         # Make the Drupal files upload directory owned by Apache
172         mkdir -p $DocumentRoot/files
173         chown apache:apache $DocumentRoot/files
174
175         # Symlink any (real) files or directories in
176         # /data/var/www/html/* to /var/www/html/. We could descend
177         # into subdirectories, but the code to do so properly would be
178         # madness.
179         for file in /data/$DocumentRoot/* ; do
180             if [ -e "$file" -a ! -h "$file" ] ; then
181                 base=$(basename "$file")
182                 if [ ! -e "$DocumentRoot/$base" ] ; then
183                     ln -nsf "$file" "$DocumentRoot/$base"
184                 fi
185             fi
186         done
187
188         # Cleanup broken symlinks
189         for file in $DocumentRoot/* ; do
190             if [ -h "$file" -a ! -e "$file" ] ; then
191                 rm -f "$file"
192             fi
193         done
194
195         # Old style PHP constants
196         mkdir -p /etc/planetlab/php
197         cat >/etc/planetlab/php/site_constants.php <<"EOF"
198 <?php
199 include('plc_config.php');
200
201 define('PL_API_SERVER', PLC_API_HOST);
202 define('PL_API_PATH', PLC_API_PATH);
203 define('PL_API_PORT', PLC_API_PORT);
204 define('PL_API_CAPABILITY_AUTH_METHOD', 'capability');
205 define('PL_API_CAPABILITY_PASS', PLC_API_MAINTENANCE_PASSWORD);
206 define('PL_API_CAPABILITY_USERNAME', PLC_API_MAINTENANCE_USER);
207 define('WWW_BASE', PLC_WWW_HOST);
208 define('BOOT_BASE', PLC_BOOT_HOST);
209 define('DEBUG', PLC_WWW_DEBUG);
210 define('API_CALL_DEBUG', PLC_API_DEBUG);
211 define('SENDMAIL', PLC_MAIL_ENABLED);
212 define('PLANETLAB_SUPPORT_EMAIL', PLC_NAME . ' Support <' . PLC_MAIL_SUPPORT_ADDRESS . '>');
213 define('PLANETLAB_SUPPORT_EMAIL_ONLY', PLC_MAIL_SUPPORT_ADDRESS);
214 ?>
215 EOF
216
217         ## patch php.ini
218         # memory limit
219         sed -i -e 's,^memory_limit = 32M *;,memory_limit = 80M ; patch myplc -- ,' $php_ini 
220         # log_errors : is On by default
221         # error_log
222         if ! grep '^error_log *=' $php_ini > /dev/null ; then
223           echo 'error_log = /var/log/php.log' >> $php_ini
224           touch /var/log/php.log
225           chmod 666 /var/log/php.log
226         fi
227
228         plc_daemon httpd
229         check
230
231         result "$MESSAGE"
232         ;;
233
234     stop)
235         MESSAGE=$"Stopping web server"
236         dialog "$MESSAGE"
237
238         killproc plc_httpd
239         check
240
241         result "$MESSAGE"
242         ;;
243 esac
244
245 exit $ERRORS