Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Other Posts

Safe auto restart for httpd after updating httpd.conf for apache

Posted by plattapuss on June 18th, 2008

If you modify your apache /etc/httpd/conf/httpd.conf configs as often as I do, you will know it is a bit tedious to constantly test for valid syntax in the httpd.config and then restart apache if all is good. With the help of a couple of friends I came up with this bash function:

CODE:
  1. httpdreload () {
  2.   configtestresults=`/etc/init.d/httpd configtest 2>&1`
  3.   if [ "$configtestresults" == "Syntax OK" ]; then
  4.     echo 'HTTPD config valid... reloading now.'
  5.     /etc/init.d/httpd reload
  6.     echo 'HTTPD reloaded'
  7.   else
  8.     echo $configtestresults
  9.   fi
  10. }

Place this in your own users .bashrc file, then either log out of your shell and back in, or run source .bashrc.

In a nutshell, you simply type httpdreload at the command prompt in your bash shell. The function will get called and will first test if the Syntax is okay or not. If it is not okay, the script will exit with the error. If the Syntax of your httpd.conf file is okay, it will then proceed to restart apache.



Reader Comments

The title should have read “Safe auto reload” and not restart.

Very handy.

If your on a debian based system:

apache_reload () {
configtestresults=`/usr/sbin/apache2ctl configtest 2>&1`
if [ "$configtestresults" == "Syntax OK" ]; then
echo ‘Apache2 config valid… reloading now.’
/etc/init.d/apache2 reload
echo ‘Apache2 reloaded’
else
echo $configtestresults
fi
}

Thanks for the alternate version.