Set the document root to /data/var/www/html (static files
[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.10 2007/02/05 19:56:26 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 document root to /data/var/www/html (static files
39         # only, no Drupal or admin pages) if the web server should not
40         # run on this machine.
41         if [ "$PLC_WWW_ENABLED" != "1" ] ; then
42             sed -i -e "s@^DocumentRoot.*@DocumentRoot \"/data$DocumentRoot\"@" $httpd_conf
43         else
44             sed -i -e "s@^DocumentRoot.*@DocumentRoot \"$DocumentRoot\"@" $httpd_conf
45         fi
46
47         # Set the default include path
48         include_path=".:$DocumentRoot/planetlab/includes:$DocumentRoot/generated:/etc/planetlab/php:/usr/share/plc_api/php"
49         sed -i -e "s@[;]*include_path = \"\.:.*\"@include_path = \"$include_path\"@" $php_ini
50
51         # Disable default Listen directive
52         sed -i -e '/^Listen/d' $httpd_conf
53
54         # Set the port numbers
55         for server in WWW API BOOT ; do
56             enabled=PLC_${server}_ENABLED
57             if [ "${!enabled}" != "1" ] ; then
58                 continue
59             fi
60             hostname=PLC_${server}_HOST
61             http_port=PLC_${server}_PORT
62             https_port=PLC_${server}_SSL_PORT
63
64             # API should always be accessed via SSL
65             if [ "$server" = "API" ] ; then
66                 https_port=${!http_port}
67                 http_port=
68             fi
69
70             # Check if we are already listening on these ports
71             skip_http=0
72             skip_https=0
73             for previous_server in WWW API BOOT ; do
74                 if [ "$server" = "$previous_server" ] ; then
75                     break
76                 fi
77                 previous_enabled=PLC_${previous_server}_ENABLED
78                 if [ "${!previous_enabled}" != "1" ] ; then
79                     continue
80                 fi
81                 previous_http_port=PLC_${previous_server}_PORT
82                 previous_https_port=PLC_${previous_server}_SSL_PORT
83
84                 if [ "${!http_port}" = "${!previous_http_port}" ] ; then
85                     skip_http=1
86                 fi
87                 if [ "${!https_port}" = "${!previous_https_port}" ] ; then
88                     skip_https=1
89                 fi
90             done
91
92             # HTTP configuration
93             if [ $skip_http -eq 0 -a -n "${!http_port}" ] ; then
94                 cat <<EOF
95 Listen ${!http_port}
96 # Make sure that the admin web pages and API are always accessed via SSL
97 <VirtualHost *:${!http_port}>
98     Redirect /db https://$PLC_WWW_HOST:$PLC_WWW_SSL_PORT/db
99     Redirect /planetlab https://$PLC_WWW_HOST:$PLC_WWW_SSL_PORT/planetlab
100     Redirect /$PLC_API_PATH https://$PLC_API_HOST:$PLC_API_PORT/$PLC_API_PATH
101 </VirtualHost>
102 EOF
103             fi
104
105             # HTTPS configuration
106             if [ $skip_https -eq 0 -a -n "${!https_port}" ] ; then
107                 # XXX Cannot support NameVirtualHost over SSL. If
108                 # the API, boot, and web servers are all running
109                 # on the same machine, the web server certificate
110                 # takes precedence.
111                 sed -i \
112                     -e "s/^Listen .*/Listen ${!https_port}/" \
113                     -e "s/<VirtualHost _default_:.*>/<VirtualHost _default_:${!https_port}>/" \
114                     $ssl_conf
115             fi
116         done >$plc_conf
117
118         # Set custom Apache directives
119         (
120             if [ "$PLC_API_ENABLED" = "1" ] ; then
121                 cat <<EOF
122 <Location $PLC_API_PATH>
123     SetHandler mod_python
124     PythonPath "sys.path + ['/usr/share/plc_api']"
125     PythonHandler ModPython
126 </Location>
127 EOF
128             else
129                 cat <<EOF
130 <Location $PLC_API_PATH>
131     Deny from all
132 </Location>
133 EOF
134             fi
135
136             if [ "$PLC_WWW_ENABLED" != "1" ] ; then
137                 cat <<EOF
138 <Location /db>
139     Deny from all
140 </Location>
141 <Location /planetlab>
142     Deny from all
143 </Location>
144 Redirect /index.php http://$PLC_WWW_HOST:$PLC_WWW_PORT/
145 EOF
146             fi
147         ) >>$plc_conf
148
149         # Make alpina-logs directory writable for bootmanager log upload
150         chown apache:apache $DocumentRoot/alpina-logs/nodes
151
152         # Make the Drupal files upload directory owned by Apache
153         mkdir -p $DocumentRoot/files
154         chown apache:apache $DocumentRoot/files
155
156         # Symlink any (real) files or directories in
157         # /data/var/www/html/* to /var/www/html/. We could descend
158         # into subdirectories, but the code to do so properly would be
159         # madness.
160         for file in /data/$DocumentRoot/* ; do
161             if [ -e "$file" -a ! -h "$file" ] ; then
162                 base=$(basename "$file")
163                 if [ ! -e "$DocumentRoot/$base" ] ; then
164                     ln -nsf "$file" "$DocumentRoot/$base"
165                 fi
166             fi
167         done
168
169         # Cleanup broken symlinks
170         for file in $DocumentRoot/* ; do
171             if [ -h "$file" -a ! -e "$file" ] ; then
172                 rm -f "$file"
173             fi
174         done
175
176         # Old style PHP constants
177         mkdir -p /etc/planetlab/php
178         cat >/etc/planetlab/php/site_constants.php <<"EOF"
179 <?php
180 include('plc_config.php');
181
182 define('PL_API_SERVER', PLC_API_HOST);
183 define('PL_API_PATH', PLC_API_PATH);
184 define('PL_API_PORT', PLC_API_PORT);
185 define('PL_API_CAPABILITY_AUTH_METHOD', 'capability');
186 define('PL_API_CAPABILITY_PASS', PLC_API_MAINTENANCE_PASSWORD);
187 define('PL_API_CAPABILITY_USERNAME', PLC_API_MAINTENANCE_USER);
188 define('WWW_BASE', PLC_WWW_HOST);
189 define('BOOT_BASE', PLC_BOOT_HOST);
190 define('DEBUG', PLC_WWW_DEBUG);
191 define('API_CALL_DEBUG', PLC_API_DEBUG);
192 define('SENDMAIL', PLC_MAIL_ENABLED);
193 define('PLANETLAB_SUPPORT_EMAIL', PLC_NAME . ' Support <' . PLC_MAIL_SUPPORT_ADDRESS . '>');
194 define('PLANETLAB_SUPPORT_EMAIL_ONLY', PLC_MAIL_SUPPORT_ADDRESS);
195 ?>
196 EOF
197
198         plc_daemon httpd
199         check
200
201         result "$MESSAGE"
202         ;;
203
204     stop)
205         MESSAGE=$"Stopping web server"
206         dialog "$MESSAGE"
207
208         killproc plc_httpd
209         check
210
211         result "$MESSAGE"
212         ;;
213 esac
214
215 exit $ERRORS