How do I safely restart apache on ubuntu after changing configuration?

  • Page Owner: Not Set
  • Last Reviewed: 2018-08-30

If I change the apache config or vhost config as part of a deployment, or when I'm editing on the server (because I'm naughty), a restart is required to apply config. If I restart but the config is bad, the server will not come back up, bringing down the live site. How do I prevent this?


Answer

On Debian installs, apache comes with apachectl command. Use the configtest action to make sure your config is good BEFORE you restart. Once you confirm config is good, you can safely restart.

Run:

apachectl configtest

sudo may or may not be required, depending on your server's config. If the server itself uses SSL (and it's not done only on the load balancer), sudo may be required. Also, on some systems,the command might be apache2ctl configtest.

Fun fact: Some change can be applied without a full apache restart, so that existing connections aren't disrupted. After you run your config test, you can also do sudo systemctl reload apache2.service or sudo service apache2 reload or sudo apache2ctl -k graceful (prefer the systemctl or service commands if available)

Comments

  • It's rare that sudo access is given to this command (as far as I know). I know that sometimes sudo is required for the test to pass, so this test may or may not work as described.
  • I modified my answer to reflect that sudo is generally not required.  On MOST lamp servers (the only exception I can think of is cscom), sudo is not required to run configtest by any user.  How's that?
  • Is it worth mentioning apachectl -k graceful as a way of reloading configuration without dropping existing connections (after configtest of course)?  Maybe if you're changing configuration you should already know that?  Signed, a guy with very limited LAMP experience.
  • We usually use the service wrapper (unit.d, systctl, or upstart) to do our service reloads, but it's a good idea.  I've added it.