Homebrew: How to Install Apache on MacOS
Prerequisites
- Homebrew: Homebrew is a free and open-source software package management system that simplifies the installation of software on Apple’s macOS operating system. Please install Homebrew based on the official site
The installation steps.
-
Uninstall pre-installed Apache (if any).
Run this command on your terminalsudo Apachectl stop sudo launchctl unload -w /System/Library/LaunchDaemons/org.Apache.httpd.plist
-
Install Apache (httpd) using Homebrew.
Run this command on your terminalbrew install httpd
-
Start the Apache. If you don’t want/need a background service, you can just run:
Apachectl start
Or, to have launchd start httpd now and restart at login:
brew services start httpd
-
Configure the Apache settings / configuration. Open the Apache configuration that should be generated on
/usr/local/etc/httpd/httpd.conf
. This is what I usually do:-
Set port.
Search this part on the configuration.Listen 8080
Change the
8080
port to your liking (in this case it is80
), example:Listen 80
-
Set Apache’s user and group.
Search this part on the configuration.User _www Group _www
Change the
_www
user into your username, and the_www
group intostaff
, so it will be like this:User your_username Group staff
-
Enable rewrite module.
Search for# LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so`
and remove the leading # to be
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
-
Change DocumentRoot folder.
Search forDocumentRoot /usr/local/var/www
and replace the folder to your
Sites
folder in your home, example:DocumentRoot /Users/your_username/Sites
-
Change
Directory
tag folder pointer
Search for<Directory /usr/local/var/www>
and replace the folder to your
Sites
folder in your home, example:<Directory /Users/your_username/Sites
-
Enable the use of
.htaccess
file in Apache. In theDirectory
block searched in No 5, search forAllowOverride
Key, and set it intoAll
, it should shows as something like this.AllowOverride all
-
-
Restart Apache.
Let’s restart Apache to ensure the configuration have taken effect. To do that, run using this command on your terminal.sudo apachectl -k restart
-
Create the
Sites
folder. Let’s create theSites
folder in your directory, and add a sample index.html file using this command on your terminal.mkdir ~/Sites echo "Hello World!" > ~/Sites/index.html
Now, go to your browser and type
http://localhost
. This should display a page which shows something like this:Hello World!
All done!