3d21644a06c13443dc26650efc52c82cc58f4bf1
[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
25 function disable_file () {
26     file=$1; shift
27     [ -f $file ] && mv -f $file $file.disabled
28 }
29 function enable_file () {
30     file=$1; shift
31     [ ! -f $file ] && mv -f $file.disabled $file
32 }
33
34 case "$1" in
35     start)
36         if [ "$PLC_API_ENABLED" != "1" -a \
37              "$PLC_BOOT_ENABLED" != "1" -a \
38              "$PLC_WWW_ENABLED" != "1" ] ; then
39             exit 0
40         fi
41
42         MESSAGE=$"Starting web server"
43         dialog "$MESSAGE"
44
45         # set document root - not really useful on fedora but just in case
46         sed -i -e "s@^DocumentRoot.*@DocumentRoot \"$DocumentRoot\"@" $httpd_conf
47         # whether WWW is enabled or not : 
48         if [ "$PLC_WWW_ENABLED" != "1" ] ; then
49             # avoid hitting drupal, that would try to connect to the db and create noise
50             disable_file $DocumentRoot/index.php
51         else
52             enable_file $DocumentRoot/index.php
53         fi
54
55         # Set the default include path
56         include_path=".:$DocumentRoot/planetlab/includes:$DocumentRoot/plekit/php:$DocumentRoot/generated:/etc/planetlab/php:/usr/share/plc_api/php"
57         sed -i -e "s@[;]*include_path = \"\.:.*\"@include_path = \"$include_path\"@" $php_ini
58
59         # Set open_basedir so as to avoid leaks
60         open_basedir="$DocumentRoot:/etc/planetlab/php:/usr/share/plc_api/php:/var/log/myslice:/var/tmp/bootmedium:/tmp"
61         sed -i -e "s@[;]*open_basedir =.*@open_basedir = \"$open_basedir\"@" $php_ini
62         
63         # for php-5.3 under fedora12, otherwise issues tons of warning messages
64         # Set timezone in php.ini if not already there
65         if grep '^;date.timezone' $php_ini >& /dev/null; then
66             dialog 'Setting PHP timezone to GMT'
67             sed -i -e 's,^;date.timezone.*,date.timezone = GMT,' $php_ini
68         fi
69
70         if grep '^short_open_tag = Off' $php_ini >& /dev/null; then
71             sed -i -e 's,^short_open_tag = Off,short_open_tag = On,' $php_ini
72         fi
73
74         # Disable default Listen directive
75         sed -i -e '/^Listen/d' $httpd_conf
76
77         # Set the port numbers
78         for server in WWW API BOOT ; do
79             enabled=PLC_${server}_ENABLED
80             if [ "${!enabled}" != "1" ] ; then
81                 continue
82             fi
83             hostname=PLC_${server}_HOST
84             http_port=PLC_${server}_PORT
85             https_port=PLC_${server}_SSL_PORT
86
87             # API should always be accessed via SSL
88             if [ "$server" = "API" ] ; then
89                 https_port=${!http_port}
90                 http_port=
91             fi
92
93             # Check if we are already listening on these ports
94             skip_http=0
95             skip_https=0
96             for previous_server in WWW API BOOT ; do
97                 if [ "$server" = "$previous_server" ] ; then
98                     break
99                 fi
100                 previous_enabled=PLC_${previous_server}_ENABLED
101                 if [ "${!previous_enabled}" != "1" ] ; then
102                     continue
103                 fi
104                 previous_http_port=PLC_${previous_server}_PORT
105                 previous_https_port=PLC_${previous_server}_SSL_PORT
106
107                 if [ "${!http_port}" = "${!previous_http_port}" ] ; then
108                     skip_http=1
109                 fi
110                 if [ "${!https_port}" = "${!previous_https_port}" ] ; then
111                     skip_https=1
112                 fi
113             done
114
115         # Create a separate path for mod_wsgi until we are ready to replace 
116         # mod_python
117         PLC_API_WSGI_PATH=/PLCAPIWSGI 
118
119             # HTTP configuration
120             if [ $skip_http -eq 0 -a -n "${!http_port}" ] ; then
121                 cat <<EOF
122 Listen ${!http_port}
123 # create wsgi socket where we have the permission
124 ### WSGISocketPrefix run/wsgi
125 # Make sure that the admin web pages and API are always accessed via SSL
126 <VirtualHost *:${!http_port}>
127     Redirect /db https://$PLC_WWW_HOST:$PLC_WWW_SSL_PORT/db
128     Redirect /planetlab https://$PLC_WWW_HOST:$PLC_WWW_SSL_PORT/planetlab
129     Redirect /$PLC_API_PATH https://$PLC_API_HOST:$PLC_API_PORT/$PLC_API_PATH
130 ###     Redirect /$PLC_API_WSGI_PATH/ https://$PLC_API_HOST:$PLC_API_PORT/$PLC_API_WSGI_PATH/
131 ###     WSGIScriptAlias $PLC_API_WSGI_PATH /usr/share/plc_api/ModWSGI.wsgi
132     # XX make processes and threads configurable 
133 ###     WSGIDaemonProcess plcapi-wsgi user=apache group=apache processes=1 threads=25
134 ###     WSGIProcessGroup plcapi-wsgi
135 </VirtualHost>
136 EOF
137             fi
138
139             # HTTPS configuration
140             if [ $skip_https -eq 0 -a -n "${!https_port}" ] ; then
141                 # XXX Cannot support NameVirtualHost over SSL. If
142                 # the API, boot, and web servers are all running
143                 # on the same machine, the web server certificate
144                 # takes precedence.
145                 sed -i \
146                     -e "s/^Listen .*/Listen ${!https_port}/" \
147                     -e "s/<VirtualHost _default_:.*>/<VirtualHost _default_:${!https_port}>/" \
148                     $ssl_conf
149             fi
150         done >$plc_conf
151
152         # Set custom Apache directives
153         (
154             if [ "$PLC_API_ENABLED" = "1" ] ; then
155                 cat <<EOF
156 # mod_python location
157 <Location $PLC_API_PATH>
158     SetHandler mod_python
159     PythonPath "sys.path + ['/usr/share/plc_api']"
160     PythonHandler ModPython
161 </Location>
162
163 # mod_wsgi location
164 ### <Location $PLC_API_WSGI_PATH/>
165 ###     SetHandler mod_wsgi
166 ### </Location>
167 EOF
168             else
169                 cat <<EOF
170 # mod_python location
171 <Location $PLC_API_PATH>
172     Deny from all
173 </Location>
174
175 # mod_wsgi location
176 ### <Location $PLC_API_WSGI_PATH/>
177 ###     Deny from all
178 ### </Location> 
179 EOF
180             fi
181
182             if [ "$PLC_WWW_ENABLED" != "1" ] ; then
183                 cat <<EOF
184 Redirect /index.html http://$PLC_WWW_HOST:$PLC_WWW_PORT/
185 EOF
186             fi
187             cat <<EOF
188 AddType application/octet-stream .iso
189 AddType application/octet-stream .usb
190 EOF
191         ) >>$plc_conf
192
193         # Make alpina-logs directory writable for bootmanager log upload
194         chown apache:apache $DocumentRoot/alpina-logs/nodes
195
196         # Make the Drupal files upload directory owned by Apache
197         mkdir -p $DocumentRoot/files
198         chown apache:apache $DocumentRoot/files
199
200         # Symlink any (real) files or directories in
201         # /data/var/www/html/* to /var/www/html/. We could descend
202         # into subdirectories, but the code to do so properly would be
203         # madness.
204         for file in /data/$DocumentRoot/* ; do
205             if [ -e "$file" -a ! -h "$file" ] ; then
206                 base=$(basename "$file")
207                 if [ ! -e "$DocumentRoot/$base" ] ; then
208                     ln -nsf "$file" "$DocumentRoot/$base"
209                 fi
210             fi
211         done
212
213         # Cleanup broken symlinks
214         for file in $DocumentRoot/* ; do
215             if [ -h "$file" -a ! -e "$file" ] ; then
216                 rm -f "$file"
217             fi
218         done
219
220         # Old style PHP constants
221         mkdir -p /etc/planetlab/php
222         cat >/etc/planetlab/php/site_constants.php <<"EOF"
223 <?php
224 include('plc_config.php');
225
226 define('PL_API_SERVER', PLC_API_HOST);
227 define('PL_API_PATH', PLC_API_PATH);
228 define('PL_API_PORT', PLC_API_PORT);
229 define('PL_API_CAPABILITY_AUTH_METHOD', 'capability');
230 define('PL_API_CAPABILITY_PASS', PLC_API_MAINTENANCE_PASSWORD);
231 define('PL_API_CAPABILITY_USERNAME', PLC_API_MAINTENANCE_USER);
232 define('WWW_BASE', PLC_WWW_HOST);
233 define('BOOT_BASE', PLC_BOOT_HOST);
234 define('DEBUG', PLC_WWW_DEBUG);
235 define('API_CALL_DEBUG', PLC_API_DEBUG);
236 define('SENDMAIL', PLC_MAIL_ENABLED);
237 define('PLANETLAB_SUPPORT_EMAIL', PLC_NAME . ' Support <' . PLC_MAIL_SUPPORT_ADDRESS . '>');
238 define('PLANETLAB_SUPPORT_EMAIL_ONLY', PLC_MAIL_SUPPORT_ADDRESS);
239 ?>
240 EOF
241
242         ## patch php.ini
243         # memory limit
244         sed -i -e 's,^memory_limit = 32M *;,memory_limit = 80M ; patch myplc -- ,' $php_ini 
245         # log_errors : is On by default
246         # error_log
247         if ! grep '^error_log *=' $php_ini > /dev/null ; then
248           echo 'error_log = /var/log/php.log' >> $php_ini
249           touch /var/log/php.log
250           chmod 666 /var/log/php.log
251         fi
252
253         plc_daemon httpd
254         check
255
256         result "$MESSAGE"
257         ;;
258
259     stop)
260         MESSAGE=$"Stopping web server"
261         dialog "$MESSAGE"
262
263         killproc plc_httpd
264         check
265
266         result "$MESSAGE"
267         ;;
268 esac
269
270 exit $ERRORS