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 terminal

    sudo Apachectl stop
    sudo launchctl unload -w /System/Library/LaunchDaemons/org.Apache.httpd.plist
    
  • Install Apache (httpd) using Homebrew.
    Run this command on your terminal

    brew 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:

    1. Set port.
      Search this part on the configuration.

      Listen 8080
      

      Change the 8080 port to your liking (in this case it is 80), example:

      Listen 80
      
    2. 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 into staff, so it will be like this:

      User your_username
      Group staff
      
    3. 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
      
    4. Change DocumentRoot folder.
      Search for

      DocumentRoot /usr/local/var/www
      

      and replace the folder to your Sites folder in your home, example:

      DocumentRoot /Users/your_username/Sites
      
    5. 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
      
    6. Enable the use of .htaccess file in Apache. In the Directory block searched in No 5, search for AllowOverride Key, and set it into All, 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 the Sites 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!

Share on: