5 [ "$DEBIAN_SCRIPT_DEBUG" ] && set -vx
12 abort-upgrade|abort-remove|abort-deconfigure)
17 echo "postinst called with unknown argument \`$1'" >&2
24 . /usr/share/debconf/confmodule
26 # Include CARNet functions
27 . /usr/share/carnet-tools/functions.sh
30 A2MODEDIR="/etc/apache2/mods-enabled"
33 PHP5CONFD="/etc/php5/conf.d"
40 # Calculate memory size for PHP memory_limit option.
44 local memtotal memlimit
45 memtotal=`awk 'tolower($1) == "memtotal:" { printf("%i", $2/1024); }' /proc/meminfo`
47 if [ $memtotal -lt 512 ]; then memlimit=16; fi
48 if [ $memtotal -ge 512 ]; then memlimit=32; fi
49 if [ $memtotal -gt 1024 ]; then memlimit=64; fi
55 # Disable PHP4 module and enable Apache2 PHP5 module.
57 if [ -e /etc/apache2/apache2.conf ]; then
58 if [ -e "$A2MODEDIR/php4.load" ]; then
59 cp_echo "CN: Disabling PHP4 module for Apache2 web server"
60 a2dismod php4 >/dev/null || true
63 if [ ! -e "$A2MODEDIR/php5.load" ] || [ ! -e "$A2MODEDIR/php5.conf" ]; then
64 cp_echo "CN: Enabling PHP5 module for Apache2 web server"
65 a2enmod php5 >/dev/null || true
71 # Check for PHP5 SAPI directories and configuration files.
73 for SAPI in apache2 cli; do
75 if [ ! -d "$PHP5DIR/$SAPI" ]; then
76 cp_echo "CN: Creating configuration directory $PHP5DIR/$SAPI/"
77 mkdir -p $PHP5DIR/$SAPI/
80 ini_file="$PHP5DIR/$SAPI/php.ini"
82 if [ ! -f "$ini_file" ]; then
84 cp_echo "CN: Generating configuration file $ini_file"
86 ini_file_tmp=`mktemp $ini_file.tmp.XXXXXX`
87 if [ "$SAPI" = "cli" ]; then
88 if [ -f "/usr/share/php5/php.ini-dist.cli" ]; then
89 cat /usr/share/php5/php.ini-dist.cli > $ini_file_tmp
92 if [ -f "/usr/share/php5/php.ini-dist" ]; then
93 cat /usr/share/php5/php.ini-dist > $ini_file_tmp
96 cp_mv $ini_file_tmp $ini_file
102 # Check for /etc/php5/conf.d/ directory.
104 if [ ! -d "$PHP5CONFD" ]; then
105 cp_echo "CN: Creating configuration directory $PHP5CONFD"
110 # Check if PHP5 extensions are enabled.
112 for php5ext in $EXTENSIONS; do
114 php5ext_re="^[[:space:]]*extension[[:space:]]*=[[:space:]]*$php5ext\.so"
116 # Remove extension entry from /etc/php5/(apache2|cli).ini
117 # configuration files.
118 for SAPI in apache2 cli; do
120 ini_file="$PHP5DIR/$SAPI/php.ini"
122 if [ -f "$ini_file" ]; then
124 cp_echo "CN: Removing $php5ext extension from file $ini_file"
126 cp_check_and_sed "$php5ext_re" \
128 "$ini_file" && need_restart=1 || true
132 # Check extension configuration in /etc/php5/conf.d/ directory.
133 if [ ! -f "$PHP5CONFD/$php5ext.ini" ]; then
135 cp_echo "CN: Creating configuration file $PHP5CONFD/$php5ext.ini"
137 php5ext_up=`echo $php5ext | tr [:lower:] [:upper:] | sed 's/Y/y/'`
138 INITMP=`mktemp $PHP5CONFD/$php5ext.ini.tmp.XXXXXX`
140 cp_echo "CN: Adding $php5ext extension to file $PHP5CONFD/$php5ext.ini"
142 echo -e "# configuration for php $php5ext_up module\nextension=$php5ext.so" >> "$INITMP"
143 cp_mv "$INITMP" "$PHP5CONFD/$php5ext.ini"
148 if ! grep -q "$php5ext_re" "$PHP5CONFD/$php5ext.ini"; then
150 cp_echo "CN: Adding $php5ext extension to file $PHP5CONFD/$php5ext.ini"
152 INITMP=`mktemp $PHP5CONFD/$php5ext.ini.tmp.XXXXXX`
153 cat "$PHP5CONFD/$php5ext.ini" > "$INITMP"
154 echo "extension=$php5ext.so" >> "$INITMP"
155 cp_mv "$INITMP" "$PHP5CONFD/$php5ext.ini"
163 # Enable some PHP5 tweaks for Apache2 web server (/etc/php5/apache2/php.ini).
165 # * upload_max_filesize = 256M
166 # * post_max_size, memory_limit = depends on system memory, we are using
167 # phpmemlimit() function.
169 for SAPI in apache2 cli; do
171 if [ ! -d "$PHP5DIR/$SAPI" ]; then
175 db_get php5-cn/${SAPI} || true
176 if [ "$RET" = "true" ]; then
178 ini_file="$PHP5DIR/$SAPI/php.ini"
180 cp_echo "CN: Checking and enabling some specific parameters in file $ini_file"
182 phplimit="$(phpmemlimit)M"
184 if [ -f "$ini_file" ]; then
186 cp_check_and_sed "^[[:space:]]*upload_max_filesize[[:space:]]*=" \
187 's/^[[:space:]]*upload_max_filesize[[:space:]]*=.*/upload_max_filesize = 256M/' \
188 "$ini_file" && need_restart=1 || true
190 cp_check_and_sed "^[[:space:]]*post_max_size[[:space:]]*=" \
191 "s/^[[:space:]]*post_max_size[[:space:]]*=.*/post_max_size = ${phplimit}/" \
192 "$ini_file" && need_restart=1 || true
194 cp_check_and_sed "^[[:space:]]*memory_limit[[:space:]]*=" \
195 "s/^[[:space:]]*memory_limit[[:space:]]*=.*/memory_limit = ${phplimit}/" \
196 "$ini_file" && need_restart=1 || true
199 ini_file_tmp=`mktemp $ini_file.tmp.XXXXXX`
200 if [ -f "$ini_file" ]; then
201 cat $ini_file > $ini_file_tmp
204 if ! egrep -q "^[[:space:]]*upload_max_filesize[[:space:]]*=" $ini_file_tmp; then
205 echo "upload_max_filesize = 256M" >> "$ini_file_tmp"
208 if ! egrep -q "^[[:space:]]*post_max_size[[:space:]]*=" $ini_file_tmp; then
209 echo "post_max_size = ${phplimit}" >> "$ini_file_tmp"
212 if ! egrep -q "^[[:space:]]*memory_limit[[:space:]]*=" $ini_file_tmp; then
213 echo "memory_limit = ${phplimit}" >> "$ini_file_tmp"
216 cp_mv "$ini_file_tmp" "$ini_file"
219 if [ -e "$ini_file_tmp" ]; then
220 rm -f "$ini_file_tmp"
229 # Restart Apache2 web server if needed.
231 if [ $need_restart -eq 1 ]; then
233 # Check Apache2 web server configuration.
234 if /usr/sbin/apache2ctl configtest 2>/dev/null; then
236 # Restart Apache2 web server.
237 if [ -x "/etc/init.d/apache2" ]; then
238 if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
239 invoke-rc.d apache2 force-reload || true
241 /etc/init.d/apache2 force-reload || true
246 # Something is broken.
247 cp_echo "CN: Your Apache2 configuration is broken."
248 cp_echo "CN: Please, check the service after the installation finishes!"