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 startOr, 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 8080Change the
8080port 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 _wwwChange the
_wwwuser into your username, and the_wwwgroup 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/wwwand replace the folder to your
Sitesfolder in your home, example:DocumentRoot /Users/your_username/Sites -
Change
Directorytag folder pointer
Search for<Directory /usr/local/var/www>and replace the folder to your
Sitesfolder in your home, example:<Directory /Users/your_username/Sites -
Enable the use of
.htaccessfile in Apache. In theDirectoryblock searched in No 5, search forAllowOverrideKey, 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
Sitesfolder. Let’s create theSitesfolder in your directory, and add a sample index.html file using this command on your terminal.mkdir ~/Sites echo "Hello World!" > ~/Sites/index.htmlNow, go to your browser and type
http://localhost. This should display a page which shows something like this:Hello World!All done!