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.
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
- 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.
I took these notes on .
OSX 10.7.5 while building
Apache/2.2.26 (Unix) with
PHP 5.5.10 and
(Updated November 10th, 2014): I took these notes on
OSX 10.10 while building
Apache 2.2.29 with
PHP 5.6.2 and
(Updated December 5th, 2014): If you are going to use
Apache 2.2, like I do, then you’ll have to brew that before PHP and then brew PHP with the
--homebrew-apxs option. This is required because Yosemite bundles
Apache 2.4, and this makes PHP link to the wrong symbols, thus, making the module crash. If you are going to use
Apache 2.4, then you don’t need to do this, and also you probably know what you are doing.
#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:
#2- PHP and all its additional recipes
The PHP recipes are maintained in a different repo, but don’t worry, this is not a duct–taped chaos, it’s properly maintained:
$ brew tap homebrew/php
$ brew install php56 --with-intl
If you are brewing Apache 2.2, you’ll have to brew that before PHP and then brew PHP with this option:
You might want to add
--with-pgsql if you want to work with PostgreSQL. MySQL, Apache and CLI support is built by default.
After the installation there are two quick things to do. First, timezone configuration:
date.timezone = America/Lima (or whatever works for you)
And second, PEAR permissions:
$ chmod -R ug+w /usr/local/Cellar/php56/5.6.2/lib/php
#3- Setting up boring MySQL
Now let’s brew MySQL:
$ brew install mysql
With that done, go ahead and start the server so we can initialize the basic tables, and secure the installation defaults:
$ unset TMPDIR
$ mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
$ mysql.server start
Remember: You shouldn’t use
sudo, so be careful with your muscle memory!
#4- Brewing Apache (httpd)
And finally, it’s time to brew Apache. You can do so with:
$ brew tap homebrew/apache
$ brew install httpd22
Note that the accompanying configuration snippets only work with the 2.2.x branch of Apache. In 2.4.x (httpd24) there’s a deprecation you have to take into account (thanks @educatopia).
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.
Remember you need to run this on something other than port 80 because you won’t run httpd as root:
- httpd.conf changes in a GitHub gist (diff)
- httpd-vhosts.conf is in this other GitHub gist (full file)
Note that I’m enabling virtual hosts and using my own paths.
If you want to configure Apache differently, you probably know what you are doing. So, get off my lawn you damn kid.
#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
You can check everything is fine by creating an
index.php with a call to
phpinfo() inside your configured root from
Make sure OSX’s Apache is not running and start (or restart) the servers:
$ apachectl start/stop/graceful(restart)
$ mysql.server start/stop/restart
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.