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