How to Start, Stop, or Restart Apache Server on CentOS 7

January 23, 2019

Introduction

Apache is part of the popular LAMP (Linux, Apache, MySQL, PHP) stack of software. It’s responsible for the function of most of the internet.

This guide will show you how to restart Apache service on Linux CentOS 7.

how to start, stop, or restart apache server on centos7

Prerequisites

  • Access to a user account with sudo privileges
  • An installed and configured Apache installation
  • Access to a command line / terminal window (Menu > Applications > Utilities > Terminal)

Restarting Apache on CentOS 7

Method 1: Restart Apache Server Using Systemctl Command

Open a terminal window and enter the following:

sudo systemctl restart httpd.service

The service should restart.

The restart command can take several moments to complete, depending on the complexity of your server configuration. If you’re running a large or complex server configuration, this can cause disruptions for users who rely on the server.

Method 2: Restart HTTPD Server Using Apachectl Command Script

Apache recommends using a control script to pass commands to the httpd process.

To restart Apache in this manner, enter the following:

sudo apachectl -k restart

To  instruct the Apache service to terminate all child processes and itself, run the following command:

apachectl -k stop

Use the below-mentioned command to exit child processes after they finish a task and then launch new instances. The service will reload configuration files as well.

apachectl -k graceful

Use -k restart to force child processes to exit. The parent process stays running, and reloads configuration files.

apachectl -k restart

Use -k graceful-stop to force parent process to stop child processes as they complete their tasks. Once all child processes are stopped, the parent process exits.

apachectl -k graceful–stop

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

Other Commands to Use with Systemctl

To start the Apache service:

sudo systemctl start httpd.service

Stop the Apache service with:

sudo systemctl stop httpd.service

Force Apache to refresh the configuration files:

sudo systemctl reload httpd.service

Set Apache to run when the system boots:

sudo systemctl enable httpd.service

Prevent Apache from loading when the system boots:

sudo systemctl disable httpd.service

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

One good practice is to weigh the benefits against the costs of each process. If you have several clients depending on access to your server, try to refresh first. If that doesn’t work, or if the disruption is minimal, use restart.

Apache Best Practices

Like many Linux services, Apache’s functionality can be modified using configuration files and modules. Configuration files should all be stored in the /etc/httpd/ directory.

In that directory, look for the /httpd.conf file – this is the main configuration file for Apache’s global settings. You can edit this file with any text editor to change your Apache configuration.

Apache’s functionality can be enhanced using 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 can be found in the /etc/httpd/mods-available directory.

Use the following commands to manage modules:

  • To enable a module:
sudo a2enconf mod_name

To disable a module:

sudo a2disconf mod_name

You can configure Apache to listen on specific ports by editing the /etc/apache2/ports.conf file.

Apache creates log files during usage. The error log reports any problems, misconfigurations, or other issues. You can use the error log to find issues with your configuration.

The access log tracks every client that connects to your Apache server. This can be used to view resources being used or how users interact with your website.

Apache access and error log files can grow large as they record so much data. Most users will use a tool like tail or cat to display a portion of the log file.

As Apache runs, the log files will use more disk space. To keep them manageable, Apache recommends rotating log files and using the graceful restart option above.

Apache can manage multiple websites, called 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

Starting, stopping, & restarting the Apache service is relatively straightforward. You can use additional commands to customize your particular configuration.

For example, you might choose to reload Apache instead of a full restart. Or, you might use the apachectl command for more control over your server.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
How to use apt Package Manager on Ubuntu Linux
January 7, 2019

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
October 22, 2018

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
October 22, 2018

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
September 26, 2018

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