webSIGHTdesigns - Web Design, Web Development, Web Hosting

How to Set up a Local Web Server with Homebrew on Mac OS X 10.9

Posted on Friday, August 1st, 2014 at 3:39 pm
by webSIGHTdesigns

How to Set up a Local Web Server on Mac OS X 10.9 Mavericks, using the Homebrew package manager.

Mac OS X 10.9 Mavericks ships with Apache version 2.2.24 and PHP version 5.4.17. One way to gain more control over which versions of software you run is to use the Homebrew package manager to install and configure them. Homebrew is "the missing package manager" for Mac OS X. To install Homebrew on your system, run the following command at the command line in Terminal:

$ ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

Installing Apache 2.4

Before you install Apache 2.4 from Homebrew, you'll want to install a couple of prerequisites it will need:

$ brew install pcre libtool

Next, to install Apache 2.4 run the following command:

$ brew install homebrew/apache/httpd24

To verify your Apache version, run:

$ httpd -v

You should see output such as the following:

Server version: Apache/2.4.9 (Unix)
Server built:   Jun  3 2014 16:11:30

Configuring Apache

To configure the version of Apache you've now installed with Homebrew, you can edit the config file:

$ sudo vi /usr/local/etc/apache2/2.4/httpd.conf

If you'll be installing PHP 5.4 you can go ahead and add the line to load the PHP module into Apache. In the httpd.conf file search for the other LoadModule lines and add a new one below them:

LoadModule php5_module /usr/local/opt/php54/libexec/apache2/libphp5.so

Next, to map the .php file extension to the PHP MIME type in Apache, search for the other AddType lines and add a new line below them:

AddType application/x-httpd-php .php

To enable loading index.php as a directory index, replace DirectoryIndex index.html in httpd.conf with the following and restart Apache:

DirectoryIndex index.html index.php

Next remove the comment (the # symbol)  from the beginning of the following lines to enable a few commonly used modules:

LoadModule expires_module libexec/mod_expires.so
LoadModule deflate_module libexec/mod_deflate.so
LoadModule rewrite_module libexec/mod_rewrite.so

By default, Apache is configured to store your website directories in /usr/local/var/www/htdocs. To change this to another directory change the following lines:

DocumentRoot "/usr/local/var/www/htdocs"
<Directory "/usr/local/var/www/htdocs">

When making changes to the httpd.conf file, in order for the changes to take effect you'll need to restart Apache with the following command:

$ sudo /usr/local/Cellar/httpd24/2.4.9/bin/apachectl restart

Starting Apache

Before starting Homebrew's Apache, it may be necessary to stop Mac OS's default Apache:

$ sudo /usr/sbin/apachectl stop

To autostart Apache on boot, copy the Apache plist file to the LaunchAgents directory:

$ ln -sfv $(brew --prefix httpd24)/*.plist ~/Library/LaunchAgents

To start Apache now:

$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.httpd24.plist

on your system, use the command sudo visudo to edit the sudoers file, and add the following line, replacing `yourusername` with your username:

yourusername    ALL= NOPASSWD: /usr/sbin/apachectl

Then edit your /etc/rc.common file:

$ sudo vi /etc/rc.common

And add the following lines to the bottom of the file:

sudo /usr/sbin/apachectl stop
sudo /usr/local/Cellar/httpd24/2.4.9/bin/apachectl start

Configuring Apache Virtual Hosts

To create the virtual hosts directory, run:

$ mkdir /usr/local/etc/apache2/2.4/vhosts/

Edit the httpd.conf file:

$ sudo vi /usr/local/etc/apache2/2.4/httpd.conf

Search for the Virtual Hosts section and add the following line just below there:

Include /usr/local/etc/apache2/2.4/vhosts/*.conf

Installing PHP

To install PHP, run:

$ brew tap homebrew/dupes
$ brew tap homebrew/versions
$ brew tap homebrew/homebrew-php
$ brew install php54 --homebrew-apxs

Install PHP modules:

$ brew install php54-mcrypt

Configuring PHP

To edit the php.ini file:

$ sudo vi /usr/local/etc/php/5.4/php.ini

To have launchd start php54 at login:

$ ln -sfv /usr/local/opt/php54/*.plist ~/Library/LaunchAgents

To load php54 now:

 $ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php54.plist 

Installing MySQL

To install MySQL:

$ brew install mysql55

To have launchd start mysql55 at login:

$ ln -sfv /usr/local/opt/mysql55/*.plist ~/Library/LaunchAgents

To start mysql55 now:

$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql55.plist

Secure your MySQL install and set the root password:

$ /usr/local/Cellar/mysql55/5.5.30/bin/mysql_secure_installation

Configuring MySQL

Edit your Bash profile:

$ vi ~/.bash_profile

Add the following line:

export PATH=$PATH:/usr/local/Cellar/mysql55/5.5.30/bin

Execute the contents of .bash_profile:

$ source ~/.bash_profile

To start MySQL:

$ /usr/local/Cellar/mysql55/5.5.30/bin/mysql.server start

Please Sign In

Please sign in to post a comment.

Web Development

View details »

Web Hosting

View details »

Our Portfolio

View portfolio »

WebSight Designs webSIGHTdesigns preferred email webSIGHTdesigns United States United States