Yeah.

Setting up Apache + PHP + MySQL on OSX with Homebrew

Recently I’ve been doing some hacking for Chamilo LMS, a Free Software E-Learning system. For various reasons I had to do this on OSX, which meant setting up a web development environment.

This lead me to MAMP, an OSX bundle with Apache + MySQL + PHP. However MAMP had many weird quirks that forced me to maintain a local set of patches just to get things working.

I finally had the time to look into a saner option, take some notes in the process, and write a blog post.

Important advice. This post enables it.
Important life advice. This post enables it.

A popular alternative was using OSX’s installed Apache with a newer PHP version from Homebrew. This sounded dangerous because you can’t just apt-get install --reinstall apache if you break something.

I preferred a pure Homebrew installation also because:

  • No need for root. Everything is user–writable in /usr/local.
  • Vanilla versions for everything, and easy to keep up–to–date with upstream.
  • Easy to destroy and replicate.
  • Usable, non–root, PEAR.
  • You would brew most of this for a hybrid setup anyway.

I couldn’t find a simple and straightforward resource for this specific setup, so I present you with my tutorial/recipe to get Apache + MySQL + PHP, via Homebrew, running on OSX.

#1- A healthy Homebrew install

First of all you need to install Homebrew. Head over to Homebrew’s homepage for instructions. Even if you already have it, I recommend a health check and an update+upgrade:

$ brew doctor
$ brew update && brew upgrade

Pay attention to whatever that says and follow the instructions if there’s any.

You also want to add Homebrew binary paths to your $PATH, even if the directories don’t exist yet. Add this to .bash_profile or whatever your shell uses:
PATH=/usr/local/opt/php56/bin/:/usr/local/sbin:/usr/local/bin:$PATH

#2- Brewing Apache (httpd)

If you are going to use Homebrew’s Apache (recommended) then you have to brew Apache before PHP. You can do so with:

$ brew tap homebrew/apache
$ brew install httpd24

When that’s done burning your lap, you can start configuring Apache. I chose to keep httpd.conf as pristine as possible and do everything in a VirtualHost.

Now make sure that httpd.conf contains this two important lines, and they are not commented out:
Include /usr/local/etc/apache2/2.4/extra/httpd-vhosts.conf
LoadModule php7_module        /usr/local/Cellar/php70/7.0.10_1/libexec/apache2/libphp7.so

And then make sure httpd-vhosts.conf contains something useful: example gist in GitHub.

#3- PHP and all its additional recipes

The PHP recipes are maintained in a different tap too, but don’t worry, this is not a duct–taped chaos, it’s properly maintained:

$ brew tap homebrew/php
$ brew install php70 --with-homebrew-apxs
$ brew install php70-intl

Note: You might need to create a /usr/local/Cellar/apr/1.5.2 symlink to /usr/local/Cellar/apr/1.5.2_1, because at the time of writing this there’s a bug breaking apr, which PHP requires to build.

Also note that you don’t need php70-intl, but I suggest you brew for internationalization’s sake.

After the installation there are two quick things to do. First, timezone configuration:
(editing /usr/local/etc/php/7.0/php.ini)
date.timezone = America/Lima (or whatever works for you)

Note: If you ever get any PEAR permissions error or warning, see the info for the php recipe:

$ brew info php70

#4- Setting up boring MySQL

Now let’s brew MySQL:
$ brew install mysql

With that done, go ahead and secure the installation defaults:
$ mysql.server start
$ mysql_secure_installation

Remember: You shouldn’t use sudo, so be careful with your muscle memory!

Clint Eastwood... Yeah.
Mr. Eastwood acknowledges the warning.

#5- Brownie points: PHPMyAdmin

In my virtual hosts configuration I included some lines for PHPMyAdmin. That’s because I’m old school and I love PHPMyAdmin. You can follow into my wrinkled foot steps with:
$ brew install phpmyadmin

#6- Profit

You can check everything is fine by creating an index.php with a call to phpinfo() inside your configured root from httpd-vhosts.conf.

Make sure OSX’s Apache is not running and start (or restart) the servers:
$ apachectl start/stop/graceful(restart)
$ mysql.server start/stop/restart

Now open go to http://localhost:8080/ and http://localhost:8080/phpmyadmin.

Enjoy your success. Maybe do a victory dance:

This might seem like a lot of work, but it’s fairly quick with this cheat sheet. It’s also very safe and easy to keep updated with brew update && upgrade.

A final note: All the suggestions and instructions that are printed after installing each package can be read again with brew info <recipe>. No need to write them down.

Happy hacking!


Last updated on 2016-08-22: Apache 2.4.18, PHP 7.0.10, MySQL 5.7.14 and OSX 10.11.6.

9 thoughts on “Setting up Apache + PHP + MySQL on OSX with Homebrew

  1. So, I’ve followed all these steps and everything looks to be in place. However, when I enter “apachectl start” as best I can tell, its still the native OS X copy of apache thats running. Is this a PATH issue? Greatful for any help you can offer.

    1. Paul you are probably missing something like this in your .bash_profile:
      PATH=/usr/local/opt/php55/bin:/usr/local/sbin:/usr/local/bin:$PATH

      Notice the added /usr/local paths. I don’t remember if brew does this automatically, specially the sbin path.

      Restart your terminal and try again, you can see which version is being used with:
      which apachectl

  2. Thanks for this!

    I had to install php with `–homebrew-apxs` to avoid the following cryptic error when starting apache:

    “`
    httpd: Syntax error on line 117 of /usr/local/etc/apache2/2.2/httpd.conf: Cannot load /usr/local/Cellar/php56/5.6.3/libexec/apache2/libphp5.so into server: dlopen(/usr/local/Cellar/php56/5.6.3/libexec/apache2/libphp5.so, 10): Symbol not found: _ap_unixd_config\n Referenced from: /usr/local/Cellar/php56/5.6.3/libexec/apache2/libphp5.so\n Expected in: /usr/local/Cellar/httpd22/2.2.29/bin/httpd\n in /usr/local/Cellar/php56/5.6.3/libexec/apache2/libphp5.so
    “`

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s