Installing Apache, PHP, and MySQL on Mac OS X

Main Thread 5 min read

macOS Update: While these instructions still work, there are new posts for recent versions of macOS, the latest being Install Apache, PHP, and MySQL on macOS Mojave.

I have installed Apache, PHP, and MySQL on Mac OS X since Leopard. Each time doing so by hand. Each version of Mac OS X having some minor difference. This post serves as much for my own record as to outline how to install Apache, MySQL, and PHP for a local development environment on Mac OS X Mountain Lion Mavericks.

I am aware of the several packages available, notably MAMP. These packages help get you started quickly. But they forego the learning experience and, as most developers report, eventually break. Personally, the choice to do it myself has proven invaluable.

It is important to remember Mac OS X runs atop UNIX. So all of these technologies install easily on Mac OS X. Furthermore, Apache and PHP are included by default. In the end, you only install MySQL then simply turn everything on.

First, open Terminal and switch to root to avoid permission issues while running these commands.

1sudo su -

Enable Apache on Mac OS X

1apachectl start

Note: Prior to Mountain Lion this was an option for Web Sharing in System Preferences → Sharing.

Verify It works! by accessing http://localhost

Enable PHP for Apache

OS X Mavericks Update: You will need to rerun the steps in this section after upgrading an existing install to Mac OS X Mavericks.

First, make a backup of the default Apache configuration. This is good practice and serves as a comparison against future versions of Mac OS X.

1cd /etc/apache2/
2cp httpd.conf httpd.conf.bak

Now edit the Apache configuration. Feel free to use TextEdit if you are not familiar with vi.

1vi httpd.conf

Uncomment the following line (remove #):

1LoadModule php5_module libexec/apache2/libphp5.so

Restart Apache:

1apachectl restart

Install MySQL

  1. Download the MySQL DMG for Mac OS X
  2. Install MySQL
  3. Install Preference Pane
  4. Open System Preferences → MySQL
  5. Ensure the MySQL Server is running
  6. Optionally, you can enable MySQL to start automatically. I do.

The README also suggests creating aliases for mysql and mysqladmin. However there are other commands that are helpful such as mysqldump. Instead, I updated my path to include /usr/local/mysql/bin.

1export PATH=/usr/local/mysql/bin:$PATH

Note: You will need to open a new Terminal window or run the command above for your path to update.

I also run mysql_secure_installation. While this isn't necessary, it's good practice.

Connect PHP and MySQL

You need to ensure PHP and MySQL can communicate with one another. There are several options to do so. I do the following:

1cd /var
2mkdir mysql
3cd mysql
4ln -s /tmp/mysql.sock mysql.sock

Creating VirtualHosts

You could stop here. PHP, MySQL, and Apache are all running. However, all of your sites would have URLs like http://localhost/somesite/ pointing to /Library/WebServer/Documents/somesite. Not ideal for a local development environment.

OS X Mavericks Update: You will need to rerun the steps below to uncomment the *vhost* Include after upgrading an existing install to Mac OS X Mavericks.

To run sites individually you need to enable VirtualHosts. To do so, we'll edit the Apache Configuration again.

1vi /etc/apache2/httpd.conf

Uncomment the following line:

1Include /private/etc/apache2/extra/httpd-vhosts.conf

Now Apache will load httpd-vhosts.conf. Let's edit this file.

1vi /etc/apache2/extra/httpd-vhosts.conf

Here is an example of VirtualHosts I've created.

1<VirtualHost *:80>
2 DocumentRoot "/Library/WebServer/Documents"
3</VirtualHost>
4
5<VirtualHost *:80>
6 DocumentRoot "/Users/Jason/Documents/workspace/dev"
7 ServerName jason.local
8 ErrorLog "/private/var/log/apache2/jason.local-error_log"
9 CustomLog "/private/var/log/apache2/jason.local-access_log" common
10
11 <Directory "/Users/Jason/Documents/workspace/dev">
12 AllowOverride All
13 Order allow,deny
14 Allow from all
15 </Directory>
16</VirtualHost>

The first VirtualHost points to /Library/WebServer/Documents. The first VirtualHost is important as it behaves like the default Apache configuration and used when no others match.

The second VirtualHost points to my dev workspace and I can access it directly from http://jason.local. For ease of development, I also configured some custom logs.

Note: I use the extension local. This avoids conflicts with any real extensions and serves as a reminder I'm in my local environment.

Restart Apache:

1apachectl restart

In order to access http://jason.local, you need to edit your hosts file.

1vi /etc/hosts

Add the following line to the bottom:

1127.0.0.1 jason.local

I run the following to clear the local DNS cache:

1dscacheutil -flushcache

Now you can access http://jason.local.

Note: You will need to create a new VirtualHost and edit your hosts file each time you make a new local site.

A note about permissions

You may receive 403 Forbidden when you visit your local site. This is likely a permissions issue. Simply put, the Apache user (_www) needs to have access to read, and sometimes write, your web directory.

If you are not familiar with permissions, read more. For now though, the easiest thing to do is ensure your web directory has permissions of 755. You can change permissions with the command:

1chmod 755 some_directory/

In my case, all my files were under my local ~/Documents directory. Which by default is only readable by me. So I had to change permissions for my web directory all the way up to ~/Documents to resolve the 403 Forbidden issue.

Note: There are many ways to solve permission issues. I have provided this as the easiest solution, not the best.

Install PHPMyAdmin

Unless you want to administer MySQL from the command line, I recommend installing PHPMyAdmin. I won't go into the details. Read the installation guide for more information. I install utility applications in the default directory. That way I can access them under, in this case, http://localhost/phpmyadmin.

1cd /Library/WebServer/Documents/
2tar -xvf ~/Downloads/phpMyAdmin-3.5.2.2-english.tar.gz
3mv phpMyAdmin-3.5.2.2-english/ phpmyadmin
4cd phpmyadmin
5mv config.sample.inc.php config.inc.php

Closing

A local development environment is a mandatory part of the Software Development Process. Given the ease at which you can install Apache, PHP, and MySQL on Mac OS X there really is no excuse.

Find this interesting? Let's continue the conversation on Twitter.