r5: * Prepravljen nacin restartanja Apache2 web servera.
[php5-cn.git] / debian / postinst
1 #!/bin/sh
2
3 set -e
4
5 [ "$DEBIAN_SCRIPT_DEBUG" ] && set -vx
6
7 # Source debconf library.
8 . /usr/share/debconf/confmodule
9
10 case "$1" in
11         configure)
12         # continue below
13         ;;
14
15         abort-upgrade|abort-remove|abort-deconfigure)
16         exit 0
17         ;;
18
19         *)
20         echo "postinst called with unknown argument \`$1'" >&2
21         exit 0
22         ;;
23 esac
24
25
26 # Include CARNet functions
27 . /usr/share/carnet-tools/functions.sh
28
29 PKG="php5-cn"
30 A2MODEDIR="/etc/apache2/mods-enabled"
31 EXTENSIONS="mysql ldap gd"
32 PHP5DIR="/etc/php5"
33 PHP5CONFD="/etc/php5/conf.d"
34
35 need_restart=0
36
37
38 # Disable PHP4 module and enable Apache2 PHP5 module.
39 #
40 if [ -e /etc/apache2/apache2.conf ]; then
41         if [ -e "$A2MODEDIR/php4.load" ]; then
42                 cp_echo "CN: Disabling PHP4 module for Apache2 web server"
43                 a2dismod php4 >/dev/null || true
44                 need_restart=1
45         fi
46         if [ ! -e "$A2MODEDIR/php5.load" ] || [ ! -e "$A2MODEDIR/php5.conf" ]; then
47                 cp_echo "CN: Enabling PHP5 module for Apache2 web server"
48                 a2enmod php5 >/dev/null || true
49                 need_restart=1
50         fi
51 fi
52
53
54 # Check if PHP5 extensions (MySQL, LDAP, GD) are enabled.
55 #
56 for php5ext in $EXTENSIONS; do
57
58         php5ext_re="^[[:space:]]*extension[[:space:]]*=[[:space:]]*$php5ext\.so"
59
60         # Remove extension entry from /etc/php5/(apache|apache2|cgi|cli).ini
61         # configuration files.
62         for SAPI in apache apache2 cgi cli; do
63
64                 ini_file="$PHP5DIR/$SAPI/php.ini"
65
66                 if [ -f "$ini_file" ]; then
67
68                         cp_echo "CN: Removing $php5ext extension from file $ini_file"
69
70                         cp_check_and_sed "$php5ext_re" \
71                                 "/$php5ext_re/d" \
72                                 "$ini_file" && need_restart=1 || true
73                 fi
74         done
75
76         # Check extension configuration in /etc/php5/conf.d/ directory.
77         if [ ! -f "$PHP5CONFD/$php5ext.ini" ]; then
78
79                 cp_echo "CN: Creating configuration file $PHP5CONFD/$php5ext.ini"
80                 
81                 php5ext_up=`echo $php5ext | tr [:lower:] [:upper:] | sed 's/Y/y/'`
82                 INITMP=`mktemp $PHP5CONFD/$php5ext.ini.tmp.XXXXXX`
83
84                 cp_echo "CN: Adding $php5ext extension to file $PHP5CONFD/$php5ext.ini"
85                 
86                 echo -e "# configuration for php $php5ext_up module\nextension=$php5ext.so" >> "$INITMP"
87                 cp_mv "$INITMP" "$PHP5CONFD/$php5ext.ini"
88                 
89                 need_restart=1
90         else
91
92                 if ! grep -q "$php5ext_re" "$PHP5CONFD/$php5ext.ini"; then
93
94                         cp_echo "CN: Adding $php5ext extension to file $PHP5CONFD/$php5ext.ini"
95                         
96                         INITMP=`mktemp $PHP5CONFD/$php5ext.ini.tmp.XXXXXX`
97                         cat "$PHP5CONFD/$php5ext.ini" > "$INITMP"
98                         echo "extension=$php5ext.so" >> "$INITMP"
99                         cp_mv "$INITMP" "$PHP5CONFD/$php5ext.ini"
100                         
101                         need_restart=1
102                 fi
103         fi
104 done
105
106
107 # Enable some PHP5 tweaks for Apache2 web server (/etc/php5/apache2/php.ini).
108 #
109 #   * upload_max_filesize = 256M
110 #   * post_max_size = 256M
111 #   * memory_limit = 256M
112 #
113 for SAPI in apache apache2 cgi cli; do
114
115         if [ ! -d "$PHP5DIR/$SAPI" ]; then
116                 continue
117         fi
118
119         ini_file="$PHP5DIR/$SAPI/php.ini"
120
121         cp_echo "CN: Checking and enabling some CARNet specific parameters in file $ini_file"
122         
123         if [ -f "$ini_file" ]; then
124
125                 cp_check_and_sed "^[[:space:]]*upload_max_filesize[[:space:]]*=[[:space:]]*2M$" \
126                         's/^[[:space:]]*upload_max_filesize[[:space:]]*=.*/upload_max_filesize = 256M/' \
127                         "$ini_file" && need_restart=1 || true
128
129                 cp_check_and_sed "^[[:space:]]*post_max_size[[:space:]]*=[[:space:]]*8M$" \
130                         's/^[[:space:]]*post_max_size[[:space:]]*=.*/post_max_size = 256M/' \
131                         "$ini_file" && need_restart=1 || true
132
133                 cp_check_and_sed "^[[:space:]]*memory_limit[[:space:]]*=[[:space:]]*16M$" \
134                         's/^[[:space:]]*memory_limit[[:space:]]*=.*/memory_limit = 256M/' \
135                         "$ini_file" && need_restart=1 || true
136         fi
137                 
138         ini_file_tmp=`mktemp $ini_file.tmp.XXXXXX`
139         if [ -f "$ini_file" ]; then
140                 cat $ini_file > $ini_file_tmp
141         fi
142
143         if ! egrep -q "^[[:space:]]*(upload_max_filesize|post_max_size|memory_limit)[[:space:]]*=" $ini_file_tmp; then
144
145                 if ! egrep -q "^[[:space:]]*upload_max_filesize[[:space:]]*=" $ini_file_tmp; then
146                         echo "upload_max_filesize = 256M" >> "$ini_file_tmp"
147                 fi
148                 if ! egrep -q "^[[:space:]]*post_max_size[[:space:]]*=" $ini_file_tmp; then
149                         echo "post_max_size = 256MB" >> "$ini_file_tmp"
150                 fi
151                 if ! egrep -q "^[[:space:]]*memory_limit[[:space:]]*=" $ini_file_tmp; then
152                         echo "memory_limit = 256M" >> "$ini_file_tmp"
153                 fi
154                 cp_mv "$ini_file_tmp" "$ini_file"
155         
156                 need_restart=1
157         fi
158         
159         if [ -e "$ini_file_tmp" ]; then
160                 rm -f "$ini_file_tmp"
161         fi
162 done
163
164
165 # Restart Apache2 web server if needed.
166 #
167 if [ $need_restart -eq 1 ]; then
168         
169         # Check Apache2 web server configuration.
170         if /usr/sbin/apache2ctl configtest 2>/dev/null; then
171
172                 # Restart Apache2 web server.
173                 if [ -x "/etc/init.d/apache2" ]; then
174                     if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
175                         invoke-rc.d apache2 force-reload || true
176                     else
177                         /etc/init.d/apache2 force-reload || true
178                     fi
179                 fi
180         else
181         
182                 # Something is broken.
183                 cp_echo "CN: Your Apache2 configuration is broken."
184                 cp_echo "CN: Please, check the service after the installation finishes!"
185         fi
186 fi
187
188
189 # Mail root
190 #
191 cp_mail "$PKG"
192
193 exit 0