How to Restart Apache on CentOS and Rocky Linux

December 16, 2024

Introduction

Apache is part of the popular LAMP (Linux, Apache, MySQL, PHP) stack of software. Restarting the Apache web server is a common administrative task for applying configuration changes, troubleshooting, or improving server performance.

This guide will explain how to restart the Apache service on CentOS and Rocky Linux.

How to Restart Apache on CentOS and Rocky Linux

Prerequisites

Restarting Apache on CentOS or Rocky Linux

There are two main ways to restart Apache on CentOS or Rocky Linux: using the systemctl command or the apachectl command.

The following text elaborates on both of them.

Method 1: Restart Apache Server Using systemctl Command

The systemctl command is a powerful utility for managing services on systemd-based Linux distributions. To restart the Apache server, enter the following command:

sudo systemctl restart httpd.service
sudo systemctl restart httpd.service terminal output

The command shows no output but stops the Apache service and starts it again. This action ensures any changes made to the configuration files are applied.

The restart takes several moments to complete, depending on the complexity of your server configuration. If you're running a large or complex server configuration, this causes disruptions for users who rely on the server.

Method 2: Restart HTTPD Server Using apachectl Command Script

The apachectl command is a control interface for managing the Apache HTTP server. It offers a convenient way to start, stop, and restart the service. It directly interacts with the Apache binary, making it a flexible alternative to systemd commands.

To restart Apache in this way, enter the following:

sudo apachectl -k restart
sudo apachectl -k restart terminal output

The command also doesn't provide any output.

This script also allows you to stop, restart, or reload Apache processes with specific options. Below are some commonly used commands and their purposes:

  • To terminate all child processes and the main Apache service immediately, run:
sudo apachectl -k stop
  • To gracefully restart by allowing child processes to finish their current tasks before shutting down and relaunching, enter:
sudo apachectl -k graceful

This approach reloads configuration files with minimal service interruption.

  • To forcefully restart the service, where child processes exit immediately, but the parent process remains running and reloads configurations, run:
sudo apachectl -k restart
  • To gracefully stop the service, allowing all child processes to complete their tasks before the parent process shuts down, run:
apachectl -k graceful-stop

None of the previous commands provides any output.

For more information on the apachectl command, see the Apache documentation.

Other Commands to Use with Systemctl

In addition to restarting services, the systemctl command offers many options for managing and monitoring systemd services. These commands provide functionality to check status, enable or disable services at boot, and perform other administrative tasks efficiently.

Most of the following commands don't provide output in the terminal:

  • To start the Apache service, use:
sudo systemctl start httpd.service
  • To stop the Apache service, enter:
sudo systemctl stop httpd.service
  • To force Apache to refresh the configuration files, run:
sudo systemctl reload httpd.service

The reload command is faster and creates much less disruption than restart. However, reloading only performs a soft refresh of the configuration files. Some services and dependencies are not always included in the refresh.

  • To set Apache to run when the system boots, execute:
sudo systemctl enable httpd.service
sudo systemctl enable httpd.service terminal output
  • To prevent Apache from loading when the system boots, type:
sudo systemctl disable httpd.service
sudo systemctl disable httpd.service terminal output

Apache Best Practices

Proper management of Apache ensures reliable performance and enhanced functionality. Some important areas for the best Apache performance are:

  • Configuration files. Apache's configuration files are in the /etc/httpd/ directory, with the main configuration file, httpd.conf, which controls global settings. Edit this file with any text editor to change your Apache configuration.
  • Modules. A module is an application that works in conjunction with the main Apache application. For example, the mod_bandwidth module allows you to set a bandwidth limit on each connection. Available modules are in the /etc/httpd/mods-available directory. Enable or disable modules using the commands sudo a2enconf mod_name and sudo a2disconf mod_name.
  • Logs. Apache generates logs to track its activity. The error log reports problems or misconfigurations, while the access log tracks client interactions and resource usage. These logs, stored in /var/log/httpd/, sometimes become large, so it's important to rotate them periodically and use tools like tail or cat to display a log file portion.
  • Virtual hosts. Apache can manage multiple websites, called Apache virtual hosts, on the same system. Content is stored in the /var/www/ directory. Available sites are stored in /etc/httpd/sites-available, and enabled sites are stored in /etc/httpd/sites-enabled.

Conclusion

This tutorial explained how to restart the Apache server on CentOS or Rocky Linux. It also elaborated on different systemctl functions and described Apache's best practices.

Next, check out this comparison between Apache and Nginx.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How to use apt Package Manager on Ubuntu Linux
December 16, 2024

In Linux, special tools were developed for managing applications. Application software for Linux typically...
Read more
How to Restart or Reboot Linux Server from the Command Line
January 15, 2024

There's a reason that tech support asks you if you've rebooted your Linux server.  It's cliched but true...
Read more
How to Reset or Change the Root Password in Linux
April 26, 2024

In Linux, root privileges (or root access) refer to a user account that has full access to all files.
Read more
How to Install and Configure Nginx on CentOS 7
November 13, 2024

If you are new to the hosting business and just acquired your first self-managed server, it might be...
Read more