Installing Apache 2.4 on Mac - rajivkanaujia/alphaworks GitHub Wiki

Background

Applicable for MacOS Sierra (version 10.12). MacOS comes with its own out of the box installation of Apache HTTP Server. There are several pros and cons on using that installation. As you continue developing some serious code for Web Development, the need to further tweak the HTTP Server will become stronger and at that point This wiki lets users install Apache 2.4 via Homebrew

Prerequisite

  • You have reviewed and configured out of the box installation of Apache HTTP Server
  • See Configure Apache for Web Development
  • Learn about LaunchDaemons - which are processes managed by the launchd service in OS X that are loaded when the system boots

Update homebrew

    $ brew tap homebrew/dupes
    $ brew tap homebrew/versions
    $ brew tap homebrew/php
    $ brew tap homebrew/apache
    $ brew update

Install Apache 2.4 via Homebrew

  • After installation, configure it to run on the standard ports (80/443)
  • Stop Apache and unload the launch agent / daemon
    $ sudo apachectl stop
    $ sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null

Build and install Apache from source

$ brew install httpd24 --with-privileged-ports --with-http2

    Apache Portable Runtime (APR) message about the path -->
        ==> Pouring apr-util-1.5.4_4.sierra.bottle.2.tar.gz
        ==> Caveats
        This formula is keg-only, which means it was not symlinked into /usr/local,
        because Apple's CLT package contains apr.

        If you need to have this software first in your PATH run:
          echo 'export PATH="/usr/local/opt/apr-util/bin:$PATH"' >> ~/.bash_profile

    Another message is for Apache; we will ignore this for now
        To have launchd start homebrew/apache/httpd24 now and restart at startup:
          sudo brew services start homebrew/apache/httpd24
            Or, if you don't want/need a background service you can just run:
                apachectl start

    Important: Summary of installation is important, as it gives the path to the installed folder/directory
	==> Summary
	🍺  /usr/local/Cellar/httpd24/2.4.25: 214 files, 4.5MB, built in 2 minutes 49 seconds

Create a LaunchDaemons – Run at Boot

LaunchDaemons are processes managed by the launchd service in OS X that are loaded when the system boots. Find the associated plist

    $ ls -l /usr/local/Cellar/httpd24/2.4.25/homebrew.mxcl.httpd24.plist 
    -rw-r--r--  1 rajiv  admin  451 May 21 21:17
    /usr/local/Cellar/httpd24/2.4.25/homebrew.mxcl.httpd24.plist

Change permissions so that OS-X can launch Apache at startup using standard ports

    $ sudo chown -v root:wheel /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist 
    /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist
    $ sudo chmod -v 644 /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist 
    $ sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist

Check for server running by the command below. Observe the location of the folder

    $ ps -aef | grep httpd
    0 30053     1   0 10:05PM ??  0:00.09 /usr/local/opt/httpd24/bin/httpd -D FOREGROUND

Check: Is Apache listening to port 80?

    $ cat /usr/local/etc/apache2/2.4/httpd.conf | grep Listen
    # Listen: Allows you to bind Apache to specific IP addresses and/or
    # Change this to Listen on specific IP addresses as shown below to 
    #Listen 12.34.56.78:80
    Listen 80

Check Go to browser and type http://localhost Find the success message

Configure Document Root

Needed for any PHP / Web Development Assuming you already have the ~/Sites/ as a location for root Create a directory under User Root called Sites where all web development artifacts will be deployed. This step will create a User Root and Sites under it.

Create Document Root

Goal is to create a folder /Users/<login name>/Sites If the login name is "rajiv" then the folder will be /Users/rajiv/Sites

Note that System Root is at /Library/WebServer/Documents/

Create a folder "Sites" under ~/ (or user root)

    $ cd ~/
    $ mkdir Sites

Or use Mac's Finder to create the folder /Users/rajiv/Sites

Creating username.conf

Under the install directory, create a folder user and a file username.conf /usr/local/etc/apache2/2.4/users/username.conf Edit the file to add following lines -

    $ sudo nano username.conf
    <Directory "/Users/rajiv/Sites">
        AllowOverride All
        Options Indexes MultiViews FollowSymLinks
        Require all granted
    </Directory>

Check the permission of the file

    $ ls -lh username.conf 
    -rw-r--r--  1 root  wheel   138B Oct  4 14:47 username.conf

Needed permission for the file is "644" --> I can change it, everyone else can read it. Fix it if the above permission by chmod if needed

    $ sudo chmod 644 username.conf

Modify httpd.conf and httpd-userdir.conf

Make changes to httpd.conf and httpd-userdir.conf. Don't forget to keep a copy of original file

    $ cd /usr/local/etc/apache2/2.4
    $ cp httpd.conf.original httpd.conf
    $ cd extra/
    $ sudo cp httpd-userdir.conf httpd-userdir.conf.original

I have Brackets installed on my Mac, so I will use Brackets to edit the files. You can use any other editor to accomplish that

    $ open -a Brackets /usr/local/etc/apache2/2.4/httpd.conf

Out-of the-box, the lines below should already be uncommented

    LoadModule authz_core_module libexec/apache2/mod_authz_core.so
    LoadModule authz_host_module libexec/apache2/mod_authz_host.so

Uncomment following lines

    LoadModule userdir_module libexec/apache2/mod_userdir.so
    Include /usr/local/etc/apache2/2.4/extra/httpd-userdir.conf

NOTE: PHP configs like one mentioned below will be added later

    LoadModule php5_module libexec/apache2/libphp5.so
    $ open -a Brackets /usr/local/etc/apache2/2.4/extra/httpd-userdir.conf

Modify the user directory

    # UserDir public_html
    UserDir Sites

    # <Directory "/home/*/public_html">
    <Directory "/Users/rajiv/Sites">

Restart Apache

    $ sudo apachectl restart

Test

TEST using inprivate mode or inprivate browsing, assuming your login user name as rajiv. Visit http://localhost/~rajiv/. You should see

apache_003.png

TEST if PHP is working via browser. Create a file name it “phpinfo.php” and file it in your document root with the contents below, then view it in a browser.

    <?php phpinfo(); ?>

Use terminal to verify the version too

    $ php -version
    PHP 5.5.36 (cli) (built: May 29 2016 01:07:06) 
    Copyright (c) 1997-2015 The PHP Group
    Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

References


Note: If you like the instructions here, please refer it on your posts/documentation. Contact me if there are corrections needed.

⚠️ **GitHub.com Fallback** ⚠️