The Best Way to Restart an Apache Web Server

Restart Apache on Ubuntu, RedHat, Gentoo, and other Linux distros

Apache web server running under Ubuntu.

Lifewire 

When you host your website on an open-source platform, it is very likely that this platform uses Apache as the default web server running on a server-grade Linux distribution. When you make substantive changes to the structure of the web server's configuration, you'll need to restart the web server (not, intrinsically, the operating system). The procedure for restarting Apache depends on the server's operating system.

Prerequisites

To restart the server, you'll need shell access to the operating system that Apache's running on. With a dedicated virtual private server, you'll either log in to a shell prompt through a browser, or using a Secure Shell session initiated from your local computer. If you cannot log into the server and execute commands with elevated privileges (e.g., by using sudo), you cannot restart the Apache server. In that case, you'll need to speak to a system administrator for assistance.

Working with Servers in Linux

Many Linux distributions support a series of shell scripts that control core services. These scripts respond to several arguments that shape how the script functions:

  • Start: Begins a service
  • Stop: Terminates a service
  • Restart: Restarts a service irrespective of current use
  • Reload: Gracefully restarts a service, letting current connections continue if they can

If you can, use reload instead of restart because the former doesn't actually kill the server, it just re-initiatlizes it by re-reading the current configuration files. A full restart disconnects any currently connected clients to the server in addition to re-initializing the configuration files.

You'll generally restart a server service using one of three methods.

The first is to use a script from /etc/init.d/ — typical for servers running Debian prior to version 8.x or Ubuntu prior to 15.04. The second is to use the systemctl command. The third, and failsafe, approach is to use apachectl.

Using Init.d

Linux distributions that rely on /etc/init.d/ scripts may use any of the following three commands to restart Apache:

# /etc/init.d/apache2 restart
$ sudo /etc/init.d/apache2 restart
$ sudo service apache2 restart

To perform a graceful restart, replace restart with reload.

For CentOS and RHEL servers 6.x or older, use the service script, but instead of calling it apache2 you'll call it httpd instead:

service httpd restart

Using Systemctl

Newer operating systems, like Debian 8.x, Ubuntu 15.04, CentOS/RHEL 7.x, and newer versions, use systemd. Execute the following command:

# systemctl restart apache2.service

Using Apachectl

The apachectl script offers a generic approach that aims to be agnostic about your specific server distribution. To execute it, you must run it as the root user:

# apachectl -k restart
#apachectl -k graceful

The graceful method is similar to the reload method.