Using PHP to restart Apache

It can be a little tricky if you need to restart Apache from a PHP script, this is because restarting Apache will kill the running PHP script, so the user will receive no output (or sometimes a “file download” dialog) when apache is restarted.

People may try doing something like this:

<?php
passthru("/path/to/custom_apache_restart");
?>

instead you need to run something in the “background” like this:

<?php
passthru("/path/to/custom_apache_restart &");
?>

that way execution goes straight back to the php script, and our custom apache restart script waits a few seconds before restarting apache.

Another way to do this is call reload, instead of restart.

/etc/init.d/apache2 reload

Leave a Reply

Your email address will not be published. Required fields are marked *