How to start, stop and restart apache

Table of Contents

Apache is primarily used to serve both static content and dynamic Web pages on the World Wide Web. Many web applications are designed expecting the environment and features that Apache provides. It can be started or restarted using any one of the following methods.

Why I Keep This Reference Around

I have managed Apache on at least three different operating systems over the years — a macOS dev machine, CentOS production servers, and Ubuntu staging boxes. The frustrating thing is that the commands are almost the same but not quite: macOS uses the full /usr/sbin/apachectl path, CentOS uses httpd via /sbin/service or apachectl, and Ubuntu (Debian-based) uses apache2 as the service name.

I have been caught enough times running service apache restart on Ubuntu and getting “unrecognised service” that I finally wrote all three variants down in one place. The other trip-up is that on CentOS, sudo apachectl restart and sudo /sbin/service httpd restart are not completely interchangeable — apachectl reads environment variables from /etc/sysconfig/httpd, while the service command does not. For most restarts that difference does not matter, but if you have custom environment variables set for your PHP application, it does.

This post is intentionally a short reference, not a deep dive. Bookmark it for the next time you blank on which OS uses which command.

Apache on MacOS

Use following commands to start/stop/restart apache

$ sudo /usr/sbin/apachectl start
$ sudo /usr/sbin/apachectl stop
$ sudo /usr/sbin/apachectl restart

On CentOS

The httpd RPM installs the /etc/init.d/httpd script, which can be accessed using the /sbin/service command.

Starting httpd using the apachectl control script sets the environmental variables in /etc/sysconfig/httpd and starts httpd. You can also set the environment variables using the init script.

To start the server using the apachectl control script as root type

$ sudo apachectl start

You can also start httpd using /sbin/service httpd start. This starts httpd but does not set the environment variables. If you are using the default Listen directive in httpd.conf, which is port 80, you will need to have root privileges to start the apache server.

To stop the server, as root type

$ sudo apachectl stop

You can also stop httpd using /sbin/service httpd stop. The restart option is a shorthand way of stopping and then starting the Apache HTTP Server.

You can restart the server as root by typing

$ apachectl restart # or:
$ /sbin/service httpd restart

Apache will display a message on the console or in the ErrorLog if it encounters an error while starting.

Debian/Ubuntu Specific commands

$ sudo service apache2 start
$ sudo service apache2 stop
$ sudo service apache2 restart