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