sodcalculator

10 Tech How-To

How to Restart Apache on CentOS Linux

Guide 10-8366 · Updated · Step by step

Restarting Apache on CentOS is a routine task that applies configuration changes, clears cached modules, or recovers from a stalled web server. This guide covers the standard commands for both SysV init (CentOS 6 and earlier) and systemd (CentOS 7 and later), including graceful reloads, full restarts, and common troubleshooting steps.

What You’ll Need

  • Root or sudo access to the CentOS server
  • An SSH client (like PuTTY or Terminal) if working remotely
  • Knowledge of whether your CentOS version uses systemd or SysV init (check with cat /etc/os-release)

Step-by-Step

  1. Connect to your server via SSH or open a terminal console. Log in as root or use a user with sudo privileges.
  2. Check your CentOS version to determine which command set to use. Run cat /etc/centos-release. If it shows version 7 or higher, use the systemd commands below. For version 6 or earlier, use the service commands.
  3. Test the Apache configuration before restarting to avoid bringing the server down with a syntax error. Run httpd -t (or apachectl configtest). You should see Syntax OK.
  4. Restart Apache using systemd (CentOS 7+): Use sudo systemctl restart httpd. This stops and starts the service completely.
  5. Restart Apache using SysV init (CentOS 6 and older): Use sudo service httpd restart. This is equivalent to running stop then start.
  6. Verify the restart was successful: Check the service status with sudo systemctl status httpd (systemd) or sudo service httpd status (SysV). Look for active (running) in the output.
  7. Confirm your website is responding: Open a web browser and visit your site, or use curl -I http://localhost from the server to see the HTTP response headers (expect 200 OK).

Common Mistakes

  • Using the wrong command for your CentOS version: On CentOS 7+, service httpd restart may still work as a compatibility alias, but it is deprecated. Stick to systemctl for clarity.
  • Forgetting to test the config first: A syntax error in httpd.conf will cause the restart to fail, and Apache will not start at all. Always run httpd -t first.
  • Confusing restart with reload: A full restart drops all active connections momentarily. If you are just changing a config file, use systemctl reload httpd (or service httpd reload) to apply changes without dropping connections.
  • Editing the wrong config file: CentOS uses /etc/httpd/conf/httpd.conf as the main file, but virtual hosts are often in /etc/httpd/conf.d/. Make sure you edit the right one.

Tips

  • Use graceful restart when possible: sudo systemctl reload httpd tells Apache to reload configuration without restarting the process. This is safer for production environments.
  • Check logs after restart: If something goes wrong, look at /var/log/httpd/error_log for detailed error messages. This is the first place to check when Apache fails to start.
  • Enable Apache to start on boot: Use sudo systemctl enable httpd (systemd) or sudo chkconfig httpd on (SysV) to ensure Apache starts automatically after a reboot.
  • Use a maintenance window: Even a graceful reload can cause a brief hiccup. Schedule restarts during low traffic periods.

Troubleshooting

Apache fails to restart with a syntax error: The output of httpd -t will show the exact file and line number. Open that file, fix the typo or missing bracket, then re-test and restart.

Apache restarts but the website is not loading: Check if the firewall is blocking port 80 (or 443). Run sudo firewall-cmd --list-all to see open ports. If needed, add the HTTP service with sudo firewall-cmd --permanent --add-service=http and reload the firewall.

Port 80 is already in use: If you get Address already in use, another process (like nginx or a second Apache instance) is using the port. Find it with sudo netstat -tulpn | grep :80 and stop that process or change Apache’s port in the config.

Apache starts but immediately crashes: Check the error log for segfaults or module issues. Often this is due to a corrupted module or a missing library. Reinstall the httpd package with sudo yum reinstall httpd (or dnf on CentOS 8+).

SELinux blocking access: If you recently changed document root or file permissions, SELinux might block Apache. Check sudo ausearch -m avc -ts recent for denied messages. You can temporarily set SELinux to permissive with sudo setenforce 0 to test, but fix the file contexts properly with restorecon -Rv /var/www.

Frequently Asked Questions

What is the difference between restart and reload?

A restart stops and starts the Apache process, closing all connections. A reload sends a signal to the running process to re-read the configuration files without dropping connections. Use reload for config changes, restart for module loads or major issues.

Why do I get ‘httpd: unrecognized service’?

This usually means Apache is not installed. Install it with sudo yum install httpd (or dnf). If it is installed, check that the service name is correct — it is always httpd on CentOS, not apache2.

Can I restart Apache without root access?

No, restarting a system service requires root privileges. You need to be root or use sudo. If you do not have sudo access, contact your system administrator.

How do I check if Apache is running?

Use sudo systemctl status httpd (systemd) or sudo service httpd status (SysV). You can also check if the process is running with ps aux | grep httpd.

What does ‘Job for httpd.service failed’ mean?

It means systemd could not start Apache. The most common cause is a syntax error in the configuration. Run httpd -t to see the error, and also check /var/log/httpd/error_log for more details.

Is it safe to restart Apache during high traffic?

A full restart will briefly interrupt service. If you must restart, consider using systemctl reload instead, which is non-disruptive. If a full restart is unavoidable, do it during off-peak hours.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →