include generated/ subdir in php include_path
[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: httpd,v 1.7 2006/10/30 22:38:22 mlhuang Exp $
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 case "$1" in
28     start)
29         if [ "$PLC_API_ENABLED" != "1" -a \
30              "$PLC_BOOT_ENABLED" != "1" -a \
31              "$PLC_WWW_ENABLED" != "1" ] ; then
32             exit 0
33         fi
34
35         MESSAGE=$"Starting web server"
36         dialog "$MESSAGE"
37
38         # Set the default include path
39         include_path=".:$DocumentRoot/planetlab/includes:$DocumentRoot/generated:/etc/planetlab/php:/usr/share/plc_api/php"
40         sed -i -e "s@[;]*include_path = \"\.:.*\"@include_path = \"$include_path\"@" $php_ini
41
42         # Disable default Listen directive
43         sed -i -e '/^Listen/d' $httpd_conf
44
45         # Set the port numbers
46         for server in WWW API BOOT ; do
47             enabled=PLC_${server}_ENABLED
48             if [ "${!enabled}" != "1" ] ; then
49                 continue
50             fi
51             hostname=PLC_${server}_HOST
52             http_port=PLC_${server}_PORT
53             https_port=PLC_${server}_SSL_PORT
54
55             # API should always be accessed via SSL
56             if [ "$server" = "API" ] ; then
57                 https_port=${!http_port}
58                 http_port=
59             fi
60
61             # Check if we are already listening on these ports
62             skip_http=0
63             skip_https=0
64             for previous_server in WWW API BOOT ; do
65                 if [ "$server" = "$previous_server" ] ; then
66                     break
67                 fi
68                 previous_hostname=PLC_${previous_server}_HOST
69                 previous_http_port=PLC_${previous_server}_PORT
70                 previous_https_port=PLC_${previous_server}_SSL_PORT
71
72                 if [ "${!http_port}" = "${!previous_http_port}" ] ; then
73                     skip_http=1
74                 fi
75                 if [ "${!https_port}" = "${!previous_https_port}" ] ; then
76                     skip_https=1
77                 fi
78             done
79
80             # Listen on these ports
81             if [ $skip_http -eq 0 -a -n "${!http_port}" ] ; then
82                 cat <<EOF
83 Listen ${!http_port}
84 <VirtualHost *:${!http_port}>
85     Redirect /db https://$PLC_WWW_HOST:$PLC_WWW_SSL_PORT/db
86     Redirect /$PLC_API_PATH https://$PLC_API_HOST:$PLC_API_PORT/$PLC_API_PATH
87 </VirtualHost>
88 EOF
89             fi
90             if [ $skip_https -eq 0 -a -n "${!https_port}" ] ; then
91                 # XXX Cannot support NameVirtualHost over SSL. If
92                 # the API, boot, and web servers are all running
93                 # on the same machine, the web server certificate
94                 # takes precedence.
95                 sed -i \
96                     -e "s/^Listen .*/Listen ${!https_port}/" \
97                     -e "s/<VirtualHost _default_:.*>/<VirtualHost _default_:${!https_port}>/" \
98                     $ssl_conf
99             fi
100         done >$plc_conf
101
102         # Set custom Apache directives
103         (
104             if [ "$PLC_API_ENABLED" = "1" ] ; then
105                 cat <<EOF
106 <Location $PLC_API_PATH>
107     SetHandler mod_python
108     PythonPath "sys.path + ['/usr/share/plc_api']"
109     PythonHandler ModPython
110 </Location>
111 EOF
112             else
113                 cat <<EOF
114 <Location $PLC_API_PATH>
115     Deny from all
116 </Location>
117 EOF
118             fi
119
120             if [ "$PLC_WWW_ENABLED" != "1" ] ; then
121                 cat <<EOF
122 <Location /db>
123     Deny from all
124 </Location>
125 EOF
126             fi
127         ) >>$plc_conf
128
129         # Make alpina-logs directory writable for bootmanager log upload
130         chown apache:apache $DocumentRoot/alpina-logs/nodes
131
132         # Make the Drupal files upload directory owned by Apache
133         mkdir -p $DocumentRoot/files
134         chown apache:apache $DocumentRoot/files
135
136         # Old style PHP constants
137         mkdir -p /etc/planetlab/php
138         cat >/etc/planetlab/php/site_constants.php <<"EOF"
139 <?php
140 include('plc_config.php');
141
142 define('PL_API_SERVER', PLC_API_HOST);
143 define('PL_API_PATH', PLC_API_PATH);
144 define('PL_API_PORT', PLC_API_PORT);
145 define('PL_API_CAPABILITY_AUTH_METHOD', 'capability');
146 define('PL_API_CAPABILITY_PASS', PLC_API_MAINTENANCE_PASSWORD);
147 define('PL_API_CAPABILITY_USERNAME', PLC_API_MAINTENANCE_USER);
148 define('WWW_BASE', PLC_WWW_HOST);
149 define('BOOT_BASE', PLC_BOOT_HOST);
150 define('DEBUG', PLC_WWW_DEBUG);
151 define('API_CALL_DEBUG', PLC_API_DEBUG);
152 define('SENDMAIL', PLC_MAIL_ENABLED);
153 define('PLANETLAB_SUPPORT_EMAIL', PLC_NAME . ' Support <' . PLC_MAIL_SUPPORT_ADDRESS . '>');
154 define('PLANETLAB_SUPPORT_EMAIL_ONLY', PLC_MAIL_SUPPORT_ADDRESS);
155 ?>
156 EOF
157
158         plc_daemon httpd
159         check
160
161         result "$MESSAGE"
162         ;;
163
164     stop)
165         MESSAGE=$"Stopping web server"
166         dialog "$MESSAGE"
167
168         killproc plc_httpd
169         check
170
171         result "$MESSAGE"
172         ;;
173 esac
174
175 exit $ERRORS