How to Restart Apache Server on CentOS 7 [ 2 Methods Inside!]

Try this guide with our instant dedicated server for as low as 40 Euros

restart apache

Whether you are a sysadmin or a developer, it is very probable that you will work with Apache as your web or application server. 

Apache is a vital component of the widely used LAMP (Linux, Apache, MySQL, PHP) software stack that plays a crucial role in the operation of much of the Internet.

If you’ve made edits to Apache’s configuration files, encountered an issue, or applied security patches to an Apache server, you might require a restart to ensure the changes are implemented successfully, and the new settings are applied in action.

In this tutorial, we will discuss how to restart the Apache service on CentOS 7.

Table Of Contents

  1. How to Restart Apache on CentOS 7
    1. The Prerequisites
    2. Method #1: Restart Apache with the systemctl Command
    3. Method #2: Restart the httpd server using the apachectl command
    4. Alternatives for the restart Command
  2. Additional Apache Related systemctl Commands
  3. Managing Apache Settings and Modules
  4. Conclusion
  5. FAQs

How to Restart Apache on CentOS 7

Restarting Apache is a necessary step after configuring or resolving service issues. Let’s first take a quick look at the prerequisites for the methods.

The Prerequisites

Before diving into how to restart the Apache service, ensure you have the following.

  • Access to an Apache server
  • A user account with sudo or administrative privileges

There are two main methods for restarting Apache.

Method #1: Restart Apache with the systemctl Command

To restart Apache with the systemctl command, follow these steps.

Open a terminal window and enter this command:

# sudo systemctl restart httpd.service

Here,

sudo: Grants temporary root privileges for the command.

systemctl: Manages system services.

restart: Action to be performed (restart).

httpd.service: The specific service to restart (Apache).

Once the command is executed, the Apache will restart.

Note: The restart command may take some time to finish, particularly with complex server configurations. This could potentially disrupt users connected to the server. This issue becomes critical when the server hosts business applications.

Method #2: Restart the httpd server using the apachectl command

Important: This method is not preferred for restarting Apache in CentOS 7 environments. 

To initiate Apache restart using apachectl command, run the following command:

# sudo apachectl -k restart

Here, 

sudo: Grants temporary root privileges for the command.

apachectl: Script for controlling Apache.

-k: Option flag for specific actions.

restart: Action to be performed (restart).

Alternatives for the restart Command

Some of the additional options to replace restart are:

stop

The stop option has a slightly different behavior than the restart option. During the stop process, the Apache service first terminates all child processes and then the core process. The command in this scenario would be as follows:

# apachectl -k stop

graceful

Gracefully exits child processes and restarts Apache with new configurations (recommended for minimal disruption).

This approach ensures a smooth transition for Apache by letting it complete its current tasks before applying new configurations. As a result, the process results in minimum downtime for website visitors. 

We recommend the following command to exit child processes after task completion and subsequently launch the Apache server with new configurations:

# apachectl -k graceful

This command will also reload configuration files for the service.

graceful-stop

This command stops child processes as they finish tasks and then stops the main Apache process.

This approach minimizes downtime for users as it allows them to complete their ongoing tasks before the server becomes unavailable. 

The following command instructs the parent process to stop child processes as they complete tasks:

# apachectl -k graceful–stop

Additional Apache Related systemctl Commands 

We will now discuss several additional commands you can use for Apache server management. These commands cover common scenarios in server management. 

Start the Apache Service

Run the following command to initiate the Apache service:

# sudo systemctl start httpd.service

Stop the Apache Service

Yourcan halt the active Apache service with this command:

# sudo systemctl stop httpd.service

Load Updated Apache Configurations

You can force reload the Apache to update Apache configuration files forcefully:

# sudo systemctl reload httpd.service

Enable System Boot

To configure Apache to start on system boot:

# sudo systemctl enable httpd.service

Disable Automatic Startup

To disable the automatic startup of Apache:

# sudo systemctl disable httpd.service

The reload command is quick and causes less disruption compared to restart. However, It only performs a soft refresh of the configuration files, potentially excluding some services and dependencies.

It’s best to assess the pros and cons of each process. If multiple clients rely on server access, attempting a refresh is advisable. If unsuccessful or if disruption is minimal, opt for a restart.

Managing Apache Settings and Modules

Like many other Linux services, Apache’s functionality can be adjusted via configuration files and modules, typically located in the /etc/httpd/ directory.

In the /etc/httpd/ directory, we recommend starting with the /httpd.conf file, which serves as Apache’s primary configuration for global settings. You can modify this file using any text editor, including Vim or Nano, to adjust Apache configuration and operations.

Similarly, if you need to configure Apache to listen on specific ports, edit the /etc/apache2/ports.conf file. Similarly, the available sites reside in /etc/httpd/sites-available, while enabled ones are in /etc/httpd/sites-enabled.

Working With Modules

You can extend Apache’s core capabilities through modules and helper applications that work alongside the main Apache application. A module is an application that works in conjunction with the main Apache application. The modules are located in the /etc/httpd/mods-available directory.

For instance, the mod_bandwidth module allows admins to set bandwidth limits on individual connections. 

Let’s quickly run through some module management commands.

Activate a Module

Run the following command to activate a module:

# sudo a2enconf mod_name

Deactivate a Module

Alternatively, run the following command to deactivate a module:

# sudo a2disconf mod_name

Conclusion

Managing the Apache service through start, stop, or restart is a streamlined task. Beyond these fundamental actions, users can delve into additional commands to tailor configurations to their specific needs.

For instance, opting for a reload rather than a full restart can minimize disruption while updating configurations. Leveraging the apachectl command offers finer control over server operations, enabling precise adjustments and optimizations. Understanding these nuances empowers users to optimize server performance effectively.

FAQs

Q. How can I find the IP address of my CentOS 7 server before restarting Apache? 

You can obtain the IP address of your CentOS 7 server by using the ifconfig command or by checking your network settings.

Q. What is the significance of the root user when restarting the Apache server on CentOS 7? 

The root user privileges are required to execute commands such as service apache2 restart or sudo systemctl status apache2. These commands manipulate system services like Apache2, necessitating elevated permissions to ensure proper execution.

Q. How can I perform a status check on the Apache2 service after restarting it on CentOS 7? 

To check the status of the Apache2 service post-restart, you can use the sudo systemctl status apache2 command. This command provides detailed information about the current state of the Apache2 service, including whether it is active or inactive.

Q. How can I successfully install Apache on my CentOS server? 

To ensure a successful installation of Apache on your CentOS server, follow these steps: 

Update your package index using sudo yum update

Install Apache by executing sudo yum install httpd

After installation, start the Apache service sudo systemctl start httpd and verify its status using sudo systemctl status httpd.

Q. What does Apache use the default port after installation on CentOS?

The default port for Apache on CentOS is port 80. This port is commonly used for HTTP traffic and is the standard port for web servers.

Q. How can I open the firewall port for Apache on my CentOS server?

You can use the command to allow HTTP traffic through the firewall on your CentOS server. For example, to open port 80 for HTTP traffic, run: sudo firewall-cmd –zone=public –add-port=80/tcp –permanent. Following it, reload the firewall for the changes to take effect: sudo firewall-cmd –reload.

Q. What is the significance of the symbolic link in the context of Apache installation on CentOS? 

Symbolic links are shortcuts that point to another file or directory. In the case of Apache installation on CentOS, symbolic links are often used to link configuration files located into the main configuration file (/etc/httpd/conf/httpd.conf). This allows for easier management of configuration files and enables modular configuration options.

Q. How do I use the service command to manage the Apache HTTP Server on my CentOS system? 

To manage the Apache HTTP Server using the service command, you can start, stop, or restart the service. For example, you would execute sudo service httpd restart to restart Apache. This command ensures seamless operation and allows for quick adjustments to the Apache service.

Q. Where can I find the log directory for Apache on my CentOS server? 

The log directory for Apache on CentOS is typically located at /var/log/httpd/. This directory contains various log files, including access logs, error logs, and other diagnostic information, crucial for troubleshooting and monitoring Apache server activity.

Q. How do I configure virtual hosts for multiple websites on Apache installed on CentOS? 

To configure virtual hosts on Apache installed on CentOS, you need to create individual configuration files for each website in the /etc/httpd/conf.d/ directory or include them in the main configuration file (/etc/httpd/conf/httpd.conf). These virtual host configuration files specify the document root, server name, and other directives specific to each website.

Q. How can I view the status of Apache, and what does the command output signify?

You can use the systemctl status apache2 command to view the status of the Apache service on your CentOS server. This command provides detailed information about the current state of the Apache service, including whether it is active or inactive, any errors encountered, and the path to the config files being used. Analyzing this command output helps administrators quickly identify issues and monitor the health of the Apache service.

Try this guide with our instant dedicated server for as low as 40 Euros