If you a maintain a modest website, one of the most fundamental techniques you’ll learn is restarting the Apache httpd Web server. This commonly comes into play in the following scenarios:
- When updating the Web server’s configuration file.
- When httpd itself becomes hopelessly confused and unresponsive.
- To test other services in isolation.
The trick is restarting Apache without having to reboot the hosted CentOS Linux server. To get started, you’ll need a dedicated or hosted virtual private server (VPS). From there, you can employ the service command. Read on for specifics.
Why the service command is superior
Although there are at least a half-dozen different ways to restart httpd, I suggest using the service command. The main benefit when it comes to a CentOS server: service will continue to work for you if you migrate to a more specialized http server such as nginx.
Interestingly, the Apache httpd documentation uses the apachectl command. However, apachectl also confuses some system administrators because the executable’s location is more variable than /sbin/service.
The CentOS documentation offers both apachectl and service as good options.
How to restart Apache on a dedicated or hosted VPS
There’s not a lot to rebooting Apache httpd on a dedicated or hosted virtual private server (VPS) based on the popular CentOS distribution. But I do suggest following these specific steps to avoid future fumbles.
1. Obtain root access using sudo.
sudo means “superuser do” and it allows access to commands require superuser (root) privileges. Because you’re not logging in with root (please don’t!), you need sudo to give root-level privileges to service, which requires root access.
sudo /sbin/service httpd graceful
The service command is used to start, stop or modify services, such as httpd.
2. Use graceful to begin closing child processes.
The aforementioned graceful parameter tells httpd to begin the restarting process by first signaling all of its child processes to complete their current tasks. After all the child processes (the ones that are serving up http requests) have finished their tasks, they will close; if any child processes aren’t currently serving requests, they will close immediately.
3. Restart the Apache httpd server.
Once all of the child processes have closed, the Apache httpd server will restart, including rereading its configuration file to ensure a clean and fully updated start. In nearly all cases, the entire shutdown/restart process will cause only a few milliseconds of downtime.
How to use the stop command instead of graceful
graceful is not the only way to restart httpd with service; you also have the option to use the stop command:
sudo /sbin/service httpd stop
Keep in mind that stop attempts to terminate all child processes immediately, which could leave end-users hanging. My preference is graceful — a better choice for your business-class server — because it’s friendlier than abruptly terminating child processes that are serving customers. Many admins and especially programmers use stop to stop their server, often because they don’t know that graceful even exists.
1. Restart Apache httpd with the stop command.
If you do use stop, follow it with the following to restart the httpd server.
sudo /sbin/service httpd start
2. Consider combining commands.
You can combine both those commands into one; where Apache httpd will relaunch once all the child processes have terminated.
sudo /sbin/service httpd restart
Whichever route you choose, you should find the process pretty clear cut. If not, feel free to submit questions and feedback in the comments section below.