Picture of Brian Love wearing black against a dark wall in Portland, OR.

Brian Love

OS X Mavericks and Apache

Web Development with Apache + PHP on OS X Mavericks

Getting your web development environment set up on OS X Mavericks is very similar to setting it up on Mountain Lion or other previous releases of Apple’s OS. If you already had your environment set up previously and upgraded to OS X 10.9 (Mavericks) then you will notice that your httpd.conf configuration has been replaced. But, don’t fret. You can find your old configuration located at: /private/etc/apache2/httpd.conf.pre-update. Let’s go ahead and get started!

Apache

OS X comes with Apache already installed, so it’s as simple as starting up the server. Previously, you could start this using the System Preferences > Sharing panel but it has been removed from there in 10.9. To start the server first start up Terminal app via Utilities > Terminal and then enter the command below. You can read the full documentation on the apachectl command.

Start Apache:

$ sudo apachectl start

Stop Apache:

$ sudo apachectl stop

Restart Apache:

$ sudo apachectl restart

Another useful command is to verify your httpd.conf configuration via the -t test switch.

$ sudo apachectl -t

Once you have started the Apache server open your favorite browser to: http://localhost and you should see the screen below.

Localhost message displaying 'it works'

Customizing Apache

After getting the initial localhost working you may want to make some changes. I suggest:

First, let’s create the /www directory:

$ cd /
$ mkdir www

Second, let’s create our local web site local.example.com (replace with your own web site):

$ mkdir local.example.com

Open up the httpd.conf file located in /private/etc/acpache2. I am using TextMate, so I will use the “mate” command.

$ mate /private/etc/apache2/httpd.conf

Make the following changes to the httdp.conf file.

We will also add a new virtual hosts to the httpd-vhosts.conf file located at /private/etc/apache2/extra/httpd-vhosts.conf. If you open this file you will notice that there is already some dummy-host.example.com entries. You can just modify these as necessary for your web site. My virtual host entry looks like this:

<VirtualHost *:80>
	DocumentRoot "/www/local.example.com"
	ServerName local.example.com
	ErrorLog "/private/var/log/apache2/local.example.com-error_log"
	CustomLog "/private/var/log/apache2/local.example.com-access_log" common
</VirtualHost>

The last thing we need to do is modify our hosts file so that when we type in local.example.com into our browser we use the local Apache web server rather than trying to go out find this fictitious domain on the Internet. So, let’s open up the hosts file for editing in TextMate.

$ mate /private/etc/hosts

Add a new entry for your web site:

127.0.0.1   local.example.com

PHP

PHP is already installed for you in OS X 10.9 Mavericks. It’s as simple as modifying the httpd.conf file again.

Lastly, we need to tell Apache to use the PHP handler when requesting files that end in .php or .phps. Append the following code to the end of your httpd.conf file.

#PHP Settings
<IfModule php5_module>
	AddType application/x-httpd-php .php
	AddType application/x-httpd-php-source .phps
</IfModule>

Don’t forget to restart the Apache server after making any changes to your httpd.conf file.

MySQL

This is an update to the original blog article as I ran into some issues with my MySQL connection after posting this article.

Upgrading also replaces your php.ini file located at /private/etc/php.ini with a new **php.ini.default* file located at **/private/etc/php.ini.default**. To get MySQL working (along with error reporting), I needed to update some settings:

If you are running into the same issue you probably need to enable the pdo_mysql extension by uncommenting the line: extension=php\_pdo\_mysql.dll.

That’s it. Happy web developing!