5 [ "$DEBIAN_SCRIPT_DEBUG" ] && set -vx
7 # Source debconf library.
8 . /usr/share/debconf/confmodule
15 abort-upgrade|abort-remove|abort-deconfigure)
20 echo "postinst called with unknown argument \`$1'" >&2
26 # Include CARNet functions.
27 . /usr/share/carnet-tools/functions.sh
31 CONFDIR="/etc/apache2"
32 CONFDIROLD="/etc/apache"
33 CONF="$CONFDIR/apache2.conf"
34 CONFOLD="$CONFDIROLD/httpd.conf"
35 A2MODEDIR="$CONFDIR/mods-enabled"
36 PORTCONF="$CONFDIR/ports.conf"
37 A2CNDIR=/usr/share/apache2-cn
38 TMPLDIR=$A2CNDIR/templates
39 CERTDIR=/etc/ssl/certs
40 A2PHPINI="/etc/php4/apache2/php.ini"
43 FQDN=$(hostname --fqdn)
44 WEBMASTER="webmaster@$FQDN"
46 BACKUPDIR="/var/backups/apache2-cn"
62 # Cleanup all temp files.
66 if [ -n "$temp_files" ]; then
67 for item in $temp_files; do
68 if [ -e "$item" ]; then
77 # Add CARNet package info lines to config's header.
84 if [ -e "$conf_file" ]; then
86 cat >> $conf_file <<EOF
87 ## Begin - Generated by CARNet package apache2-cn
89 # REMOVE this whole block if you DON'T WANT apache2-cn
90 # to edit your configuration file.
92 ## End - Generated by CARNet package apache2-cn
99 # Check if configuration file has CARNet package info lines.
100 # return: $RET => 0 - tagged
101 # 1 - not tagged or file does not exists
102 # 2 - file exists, but it is not tagged
110 if [ -f "$conf_file" ]; then
111 if egrep -q "^## Begin - Generated by CARNet package apache2-cn$" "$conf_file"; then
121 # Check CustomLog, ErrorLog and TransferLog paths - /var/log/apache/ is replaced
122 # with /var/log/apache2/.
129 if [ -f "$conf_file" ]; then
131 if egrep -iq '^[[:space:]]*(Error|Custom|Transfer)Log[[:space:]]*\/var\/log\/apache\/' "$conf_file"; then
133 out=$(mktemp ${conf_file}.XXXXXX)
134 temp_files="${temp_files} ${out}"
136 sed 's/\(^[[:space:]]*\(Error\|Custom\|Transfer\)Log[[:space:]]*\)\/var\/log\/apache\//\1\/var\/log\/apache2\//I' \
145 # Generate Apache2 web server SSL certificate.
149 generate_ssl_output=$($A2CNDIR/carnet-generate-ssl ignore "$FQDN" "$WEBMASTER" "$DOMAIN" 2> /dev/null)
150 cp_echo "$generate_ssl_output"
156 # Check if port 443 is configured in ports.conf file.
160 if ! egrep -iq "^[[:space:]]*Listen[[:space:]]*.*443$" "$PORTCONF"; then
162 cp_echo "CN: Enabling SSL port (443) for Apache2 web server."
164 out=$(mktemp ${PORTCONF}.XXXXXX)
166 echo "Listen 443" >> $out
170 temp_files="${temp_files} ${out}"
176 # Install specified Apache2 configuration file.
180 conftmpl="$A2CNDIR/$1.conf"
181 conf="$CONFDIR/conf.d/$2.conf"
183 if [ ! -e "$conf" ]; then
185 cp_echo "CN: Enabling CARNet specific configuration."
186 cp "$conftmpl" "$conf"
190 cp_echo "CN: $conf already exists, left untouched." 1>&2
196 # Install specified VirtualHost for Apache2 web server.
200 # install_vhost [-nvh] [-d] [-s docroot_symlink_dest] template site site-enabled-symlink
202 # -nvh - add NameVirtualHost
203 # -d - mkdir DocumentRoot
204 # -r - set DocumentRoot
205 # -n - set ServerName
206 # -s X - symlink DocumentRoot to X (all in /var/www)
208 # site - name of file in sites-available, host part of ServerName unless -r or -n is used
209 # site-enabled-symlink - name of symlink in sites-enabled
219 while echo "x$1" | grep -q '^x-'; do
237 if ! echo "$docroot" | grep -q /; then
238 docroot="/var/www/$docroot"
250 vhosttmpl="$1.template"
253 [ -z "$vhostname" ] && vhostname=$(echo "$vhost"| awk -F. '{print $1}')
256 vhostdir=$CONFDIR/sites-available
257 venabledir=$CONFDIR/sites-enabled
259 if [ ! -e "$TMPLDIR/${vhosttmpl}" ]; then
260 echo "E: vhost template ${vhosttmpl} not found in $TMPLDIR!" 1>&2
264 [ -z "$docroot" ] && docroot="/var/www/$vhostname.$DOMAIN"
266 # if we were broken mid-installation, force
267 if [ ! -e "$docroot" -a \( -n "$mkdir_docroot" -o -n "$symlink_docroot" \) ]; then
271 # add vhost if either of these is true
272 # - adding is forced OR
275 if [ -n "$force_vhost" -o \( ! -e "$vhostdir/$vhost" -a ! -e "$venabledir/$venabled" \) ]; then
277 cp_echo "CN: Adding $vhost VirtualHost."
278 out=$(mktemp $vhostdir/$vhost.XXXXXX)
279 temp_files="${temp_files} ${out}"
284 if [ "$add_namevirthost" ]; then
285 nvh=$(awk -F'[ >]' '/^<VirtualHost/ {print $2}' $TMPLDIR/$vhosttmpl |\
286 sed "s/IPADDR/$MYIP/g")
287 echo "NameVirtualHost $nvh" >> $out
290 sed "s/HOST/$vhostname/g; s/DOMAIN/$DOMAIN/g;
291 s#DOCROOT#$docroot#g; s/IPADDR/$MYIP/g" < $TMPLDIR/$vhosttmpl >> $out
292 cp_mv $out $vhostdir/$vhost
293 chmod 644 $vhostdir/$vhost
294 ln -fs ../sites-available/$vhost $venabledir/$venabled
296 if [ -n "$mkdir_docroot" -a ! -d "$docroot" ]; then
298 echo '<html><body><h1>Radi!</h1></body></html>' > "$docroot/index.html"
299 elif [ -n "$symlink_docroot" ]; then
300 ln -fs "$symlink_docroot" "$docroot"
308 # Set trap for deleting all temp files.
310 trap cleanup 0 1 2 15
313 # Make sure that monit conf for Apache is disabled.
314 if [ -f "/etc/monit.d/apache1.conf" ]; then
315 mv /etc/monit.d/apache1.conf /etc/monit.d/apache1.conf.disabled
316 pkill -9 -f /usr/sbin/monit || true
320 # First of all - stop Apache web server, make sure Apache is NOT running.
322 if [ -x /usr/sbin/invoke-rc.d ]; then
323 [ -x /usr/sbin/apache ] && invoke-rc.d apache stop || true
324 pkill -9 -f /usr/sbin/apache || true
326 [ -x /etc/init.d/apache ] && /etc/init.d/apache stop || true
330 # Backup all configuration located in /etc/apache2/conf.d/ and
331 # /etc/apache2/sites-available/ directories.
333 if [ -e "$CONF" ]; then
334 cp_echo "CN: Doing backup for $CONF"
335 cp_backup_conffile -d $BACKUPDIR -p $CONF
338 if [ -d "$CONFDIR/conf.d" ] && [ -n "$(ls ${CONFDIR}/conf.d/)" ]; then
339 cp_echo "CN: Doing backup for all files in /etc/apache2/conf.d/"
340 for file in /etc/apache2/conf.d/*; do
341 if [ -z "$(echo $file | egrep '^/.*~')" ]; then
342 cp_backup_conffile -d $BACKUPDIR -p $file
347 if [ -d "$CONFDIR/sites-available" ] && [ -n "$(ls ${CONFDIR}/sites-available/)" ]; then
348 cp_echo "CN: Doing backup for all files in /etc/apache2/sites-available/"
349 for file in /etc/apache2/sites-available/*; do
350 if [ -z "$(echo $file | egrep '^/.*~')" ]; then
351 cp_backup_conffile -d $BACKUPDIR -p $file
356 if [ $backup_done -eq 1 ]; then
357 cp_echo "CN: Backup is located in directory: $BACKUPDIR/"
361 # Enable Apache2 web server modules (cgi, rewrite, userdir, suexec, php4, ssl).
363 if [ -e "$CONF" ]; then
365 if [ ! -e "$A2MODEDIR/cgi.load" ]; then
366 cp_echo "CN: Enabling CGI module for Apache2 web server."
367 a2enmod cgi >/dev/null || true
371 if [ ! -e "$A2MODEDIR/rewrite.load" ]; then
372 cp_echo "CN: Enabling rewrite module for Apache2 web server."
373 a2enmod rewrite >/dev/null || true
377 if [ ! -e "$A2MODEDIR/userdir.load" ] || [ ! -e "$A2MODEDIR/userdir.conf" ]; then
378 cp_echo "CN: Enabling userdir module for Apache2 web server."
379 a2enmod userdir >/dev/null || true
383 if [ ! -e "$A2MODEDIR/suexec.load" ]; then
384 cp_echo "CN: Enabling SUEXEC module for Apache2 web server."
385 a2enmod suexec >/dev/null || true
389 if [ ! -e "$A2MODEDIR/php4.load" ] || [ ! -e "$A2MODEDIR/php4.conf" ]; then
390 cp_echo "CN: Enabling PHP4 module for Apache2 web server."
391 a2enmod php4 >/dev/null || true
395 if [ ! -e "$A2MODEDIR/ssl.load" ] || [ ! -e "$A2MODEDIR/ssl.conf" ]; then
396 cp_echo "CN: Enabling SSL module for Apache2 web server."
397 a2enmod ssl >/dev/null || true
403 # Install CARNet specific configuration file.
405 install_conf carnet 000-carnet
407 # Enable SSL port (443).
411 # Disable default site configuration.
413 if [ -e "$CONF" ]; then
414 if [ -e "$CONFDIR/sites-enabled/000-default" ]; then
415 cp_echo "CN: Disabling 000-default site configuration."
416 a2dissite 000-default >/dev/null || true
423 # Apache2 SSL certificate.
427 if [ -d "$CONFDIR/conf.d" ] && [ -n "$(ls $CONFDIR/conf.d)" ]; then
428 listen_ssl_mask=$CONFDIR/conf.d/*
430 if [ -d "$CONFDIR/sites-enabled" ] && [ -n "$(ls $CONFDIR/sites-enabled)" ]; then
431 listen_ssl_mask=$listen_ssl_mask" "$CONFDIR/sites-enabled/*
434 for file in $CONF $listen_ssl_mask; do
435 if [ -f "$file" ]; then
436 if egrep -iq '^[[:space:]]*<VirtualHost .*443[[:space:]]*>' $file; then
444 if [ $apache2_sslcert -eq 0 ]; then
446 db_get apache2-cn/sslcf || true
449 if [ -n "$apache2_sslcf" ]; then
451 db_get apache2-cn/sslckf || true
452 apache2_sslckf="$RET"
454 db_get apache2-cn/sslccf || true
455 apache2_sslccf="$RET"
460 # Generate new SSL certificate files.
472 db_get apache2-cn/wwwhost || true
473 if [ "$RET" = "true" ]; then
475 # Add WWW VirtualHost.
476 if [ -f "$CONFDIR/sites-available/$FQDN" ]; then
477 cp_backup_conffile -d $BACKUPDIR -p $CONFDIR/sites-available/$FQDN
479 if [ -f "$CONFDIR/sites-available/www.$DOMAIN" ]; then
480 cp_backup_conffile -d $BACKUPDIR -p $CONFDIR/sites-available/www.$DOMAIN
483 chk_conf_tag "$CONFDIR/sites-available/$FQDN"
484 if [ ! -f "$CONFDIR/sites-available/$FQDN" ] || [ $RET -eq 0 -a -f "$CONFOLD" ]; then
485 install_vhost -nvh -d -r www.$DOMAIN default $FQDN 000-$FQDN
489 chk_conf_tag "$CONFDIR/sites-available/www.$DOMAIN"
490 if [ ! -f "$CONFDIR/sites-available/www.$DOMAIN" ] || [ $RET -eq 0 -a -f "$CONFOLD" ]; then
491 install_vhost default www.$DOMAIN www.$DOMAIN
496 # No WWW VirtualHost.
497 if [ -f "$CONFDIR/sites-available/$FQDN" ]; then
498 cp_backup_conffile -d $BACKUPDIR -p $CONFDIR/sites-available/$FQDN
501 chk_conf_tag "$CONFDIR/sites-available/$FQDN"
502 if [ ! -f "$CONFDIR/sites-available/$FQDN" ] || [ $RET -eq 0 -a -f "$CONFOLD" ]; then
503 install_vhost -nvh -d -r $FQDN default $FQDN 000-$FQDN
509 # Add VirtualHost for SSL?
511 if [ $apache2_sslcert -eq 0 ]; then
513 if [ -f "$CONFDIR/sites-available/ssl" ]; then
514 cp_backup_conffile -d $BACKUPDIR -p $CONFDIR/sites-available/ssl
517 # No active SSL VirtualHosts found - add new one.
518 chk_conf_tag "$CONFDIR/sites-available/ssl"
519 if [ ! -f "$CONFDIR/sites-available/ssl" ] || [ $RET -eq 0 -a -f "$CONFOLD" ]; then
520 install_vhost -r $FQDN -n $HOST ssl ssl 001-ssl
526 # Check SSL certificates location for VirtualHosts.
528 if [ $apache2_sslcert -eq 0 ]; then
530 chk_conf_tag "${CONFDIR}/sites-available/ssl"
531 if [ $RET -eq 0 ] && [ -n "$apache2_sslcf" ]; then
533 SSLTMP=$(mktemp ${CONFDIR}/ssltmp.XXXXXX)
534 temp_files="${temp_files} ${SSLTMP}"
535 cp ${CONFDIR}/sites-available/ssl $SSLTMP
538 cp_check_and_sed "^[[:space:]]*SSLCertificateFile \/etc\/ssl\/certs\/apache2\.pem" \
539 "s#SSLCertificateFile /etc/ssl/certs/apache2.pem#SSLCertificateFile $apache2_sslcf #g" \
542 # SSLCertificateKeyFile
543 cp_check_and_sed "^[[:space:]]*SSLCertificateKeyFile \/etc\/ssl\/private\/apache2\.key" \
544 "s#SSLCertificateKeyFile /etc/ssl/private/apache2.key#SSLCertificateKeyFile $apache2_sslckf #g" \
547 # SSLCertificateChainFile
548 if [ -n "$apache2_sslccf" ]; then
549 cp_check_and_sed "^# SSLCertificateChainFile \/etc\/ssl\/certs/sureserverEDU\.pem" \
550 "s#\# SSLCertificateChainFile /etc/ssl/certs/sureserverEDU.pem#SSLCertificateChainFile $apache2_sslccf #g" \
554 cp_mv $SSLTMP ${CONFDIR}/sites-available/ssl
559 if [ -e "$SSLTMP" ]; then
566 # Check for CustomLog, ErrorLog and TransferLog in Apache2 configuration.
568 cp_echo "CN: Checking Apache2 CustomLog, ErrorLog and TransferLog directives."
569 if [ -d "$CONFDIR/conf.d" ] && [ -n "$(ls $CONFDIR/conf.d)" ]; then
570 log_mask=$CONFDIR/conf.d/*
572 if [ -d "$CONFDIR/sites-available" ] && [ -n "$(ls $CONFDIR/sites-available)" ]; then
573 log_mask=$log_mask" "$CONFDIR/sites-available/*
575 for file in $CONF $log_mask; do
577 if [ $RET -eq 0 ]; then
583 # Start Apache2 web server on boot?
584 # This will enable Apache2 in /etc/default/apache2 file.
586 if egrep -q "^[[:space:]]*NO_START=1" /etc/default/apache2; then
587 cp_check_and_sed NO_START=1 s/NO_START=1/NO_START=0/ /etc/default/apache2 || true
595 # Remove old AOSI configuration for Apache: aosi-www.conf, aosi.conf.
597 if [ -e "$CONFDIR/conf.d/aosi-www.conf" ] || [ -e "$CONFDIR/conf.d/aosi.conf" ]; then
598 cp_echo "CN: Removing old AOSI configuration files for Apache2."
601 [ -e "$CONFDIR/conf.d/aosi-www.conf" ] && rm -f $CONFDIR/conf.d/aosi-www.conf
602 [ -e "$CONFDIR/conf.d/aosi.conf" ] && rm -f $CONFDIR/conf.d/aosi.conf
605 # Stop Apache web server and disable Apache automatic start on boot.
607 if [ -x "/etc/init.d/apache" ]; then
610 if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
611 invoke-rc.d apache stop || true
613 /etc/init.d/apache stop || true
616 # Disable automatic start on boot.
617 if [ -x "`which update-rc.d 2>/dev/null`" ]; then
618 update-rc.d -f apache remove > /dev/null 2>&1 || true
619 update-rc.d apache stop 90 6 . > /dev/null 2>&1 || true
623 # Also check for Apache-SSL web server.
625 if [ -x "/etc/init.d/apache-ssl" ]; then
628 if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
629 invoke-rc.d apache-ssl stop || true
631 /etc/init.d/apache-ssl stop || true
634 # Disable automatic start on boot.
635 if [ -x "`which update-rc.d 2>/dev/null`" ]; then
636 update-rc.d -f apache-ssl remove > /dev/null 2>&1 || true
637 update-rc.d apache-ssl stop 90 6 . > /dev/null 2>&1 || true
642 # Restart Apache2 web server if needed.
644 if [ $need_restart -eq 1 ]; then
646 # Check Apache2 web server configuration.
647 if apache2ctl configtest 2>/dev/null; then
649 # Restart Apache2 web server.
650 if [ -x "/etc/init.d/apache2" ]; then
651 if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
652 invoke-rc.d apache2 force-reload || true
654 /etc/init.d/apache2 force-reload || true
659 # Something is broken.
660 cp_echo "CN: Your Apache2 configuration seem to be broken."
661 cp_echo "CN: Please, check the service after the installation finishes!"
671 # (re)generate monit.d files if monit-cn is installed.
673 if [ -x "/usr/sbin/update-monit.d" ]; then
674 update-monit.d || true