macOS – find, start, stop, and restart the Apache web server
Learn macOS commands for managing Apache: locate, start, stop, and restart both default and Homebrew versions.
Trying to find out if the Apache server is running on macOS? How do I find it? And once you find it, why are there several methods to start, stop, and restart it?
Managing the Apache web server on macOS involves using specific commands in the Terminal. The commands differ slightly depending on whether you are using the built-in Apache or one installed via Homebrew. Here’s a comprehensive guide to starting, stopping, and restarting both versions.
Determine which Apache server is currently being used on macOS
To determine which Apache server is currently being used on macOS via the terminal, you can use several methods:
Check running Apache processes
Use the command ps aux | grep httpd
to list all processes related to Apache. This command displays only processes with the command line httpd
, which is the name of the Apache HTTP server. This function helps you determine whether Apache is operating and can provide information about the version or configuration being used.
Using which
and whereis
commands
The which -a httpd
command shows all instances of httpd in your system’s PATH
. The whereis httpd
command locates the binary, source, and manual page files for httpd
.
Check Apache configuration
If you have multiple Apache installations, you can check their configuration files to see which one is being used. The default Apache configuration file on macOS is typically located at /etc/apache2/httpd.conf
, while a Homebrew-installed Apache might have its configuration files in /usr/local/etc/httpd/
. Comparing these files can help you determine which Apache server is currently active.
You can check which configuration file Apache is using by looking at the output of apachectl -t -D DUMP_INCLUDES
.
By using these commands, you can identify which version of Apache is currently running on your macOS system, whether it’s the default version provided by Apple or a version installed via Homebrew or another method.
Internal Apache
Let’s look into internal, built-in macOS, Apache details, and commands.
- Binary:
/usr/sbin/httpd
- Start Apache web server:
sudo /usr/sbin/apachectl start
- Stop Apache web server:
sudo /usr/sbin/apachectl stop
- Forcefully stop Apache and unload it from launch control:
sudo killall httpd && sudo launchctl unload /System/Library/LaunchDaemons/org.apache.httpd.plist
Reload Apache after editing the config file:
sudo apachectl graceful
This
graceful
command is equivalent to a restart but waits for no open connections to be broken, making it a more graceful way to reload the configuration. It’s important to note that while thereload
command is often used to apply changes without dropping existing connections, it technically performs a restart of the Apache server. The method it does so minimizes interference with ongoing connections, which is where it differs. This is especially helpful in production settings where it’s important to minimize downtime.Determine version:
/usr/sbin/apachectl -v
The config: usually
/etc/apache2/httpd.conf
, but you can run/usr/sbin/apachectl -t -D DUMP_INCLUDES
to determine exact path.Note that
/private/etc/apache2
is another location where thehttpd.conf
file can be found, and/etc
is a symbolic link to this directory.
Homebrew Apache
Let’s look into Homebrew’s Apache version details and commands.
- Binary:
/usr/local/bin/httpd
- Start Apache:
brew services start httpd
- Stop Apache:
brew services stop httpd
Restart Apache:
brew services restart httpd
Determine version:
/usr/local/bin/apachectl -v
The config: Usually
/usr/local/etc/httpd/httpd.conf
, but you can run/usr/local/bin/apachectl -t -D DUMP_INCLUDES
to determine exact path.
Conflict between internal Apache and Apache installed via Homebrew
To resolve the conflict between the internal Apache server provided by macOS and the Apache server installed via Homebrew, you can stop internal Apache and prevent from auto-starting.
How to stop and prevent auto-starting the internal Apache server on macOS
To stop the internal Apache server on a Mac, including preventing the Apache server from auto-starting again when you restart your Mac, you can use the following commands in the Terminal:
- Find internal version: use command
which -a apachectl
. The default location for internal Apple Apache server is/usr/sbin/apachectl
. - Stop internal Apache:
sudo /usr/sbin/apachectl stop
orsudo apachectl stop
- Preventing the Apache server from auto-starting:
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null
. The-w
argument in the command is crucial to prevent the Apache job from loading after macOS restarts.
apachectl vs httpd
The last thing to clarify is the difference between apachectl
and httpd
.
When it comes to managing and controlling the Apache HTTP Server, two commonly used commands are apachectl
and httpd
. These commands serve similar purposes but have some differences in their functionality and usage.
apachectl
apachectl
is a command-line utility that provides a convenient interface for controlling the Apache HTTP Server. It is often used for starting, stopping, and restarting the server, as well as performing other administrative tasks.
httpd
On the other hand, httpd
is the actual executable file for the Apache HTTP Server. It is the program that runs as the server daemon and handles incoming HTTP requests.
While httpd
can be used directly to start, stop, and restart the server, it does not provide the additional functionality and convenience that apachectl
offers. However, it can be used as a synonym for certain apachectl
options, such as httpd -S
being equivalent to apachectl -D DUMP_VHOSTS
.
Comments