Here's how to install nginx, PHP, and MySQL using Homebrew on your Mac. It's actually quite easy. I did it on Mountain Lion (10.8) but it probably works for Lion too. I followed the tutorial here on EZUnix.org, but my version fixes some typos and explains some steps along the way.
Disclaimer: This blog is a memo to self for when I need to do this again. If you encounter any errors, I may or may not be able to help...
Estimated Time: 10-20 minutes
Got Command Line Developer Tools?
You're gonna need them. Finally, Apple provides the command line tools without needing to install the nearly-2GB-Xcode from the App Store. Go to their Developers Downloads page and download the latest "Command Line Tools" for your version of OS X, then install them.
In case you haven't already, install Homebrew by following the instructions at the bottom of this page.
Homebrew's most legit PHP "tap" (package source) is by Jose Gonzalez. Make sure to install it:
$ brew tap josegonzalez/homebrew-php
We also need a tap for a PHP 5.4 dependency, zlib:
$ brew tap homebrew/dupes
$ brew install mysql
It'll chew on that for a few minutes, then we need to get it to run as our user account:
$ unset TMPDIR
$ mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
I got a "Warning" during this operation, and while I don't think it's critical, I did this and things have seemed to work fine... if you got a warning during the last step, then you could do this:
$ sudo mv /usr/local/opt/mysql/my-new.cnf /usr/local/opt/mysql/my.cnf
Then, to launch MySQL at startup:
$ cp `brew --prefix mysql`/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Done! Next: the web server.
$ brew install nginx
Let that stew, then run this commands to have nginx run as root at startup (so we can listen on port 80, the default, instead of 8080 which is less convenient for development):
$ sudo cp `brew --prefix nginx`/homebrew.mxcl.nginx.plist /Library/LaunchDaemons/
That takes care of the startup part; make your .plist file look like this to run as root (if it doesn't work for you, then the path in ProgramArguments may need adjustment):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
Then create a log file... this allows us to view server logs in the Mac Console, which is really convenient, but isn't required:
$ sudo mkdir /var/log/nginx/
(Don't forget to tell nginx to put the log file there in nginx.conf: error_log /var/log/nginx/error.log;)
Done! Next up: PHP.
$ brew install --without-apache --with-fpm --with-mysql php54
Make sure to change "php54" to whatever version you want. At time of writing, PHP 5.4 is the latest stable, but PHP 5.5 is in alpha. I assume 5.5 would be php55, etc. Be sure to adjust any following commands with the proper version number.
Quick note: Yes, OS X does come with PHP pre-installed. But we don't want to use that. We need an install we can use with nginx and FastCGI Process Manager (fpm). Plus, we want the latest version, and I'm just not that into compiling from source.
To run php-fpm at startup:
$ sudo cp `brew --prefix php54`/homebrew-php.josegonzalez.php54.plist /Library/LaunchAgents/
$ sudo launchctl load -w /Library/LaunchAgents/homebrew-php.josegonzalez.php54.plist
Done! Next up: configuration.
I want all php commands to be using the latest version, not the default PHP binary. So I use this little trick to create a symlink from the default PHP binary to the new one... I do this for both php and php-fpm. If you're confused about which versions are where, use the "whereis" command, like: "whereis php".
$ php-fpm -v
$ sudo mv /usr/sbin/php-fpm /usr/sbin/php-fpm.bak
$ sudo ln -s /usr/local/Cellar/php54/5.4.11/sbin/php-fpm /usr/sbin/php-fpm
$ php-fpm -v
Notice that the version went from 5.3 to 5.4 (in my case). Now for the php binary:
$ php -v
$ sudo mv /usr/bin/php /usr/bin/php.bak
$ sudo ln -s /usr/local/bin/php /usr/bin/php
$ php -v
I also added /usr/local/sbin to the PATH by adding that directory to the /etc/paths file, then restarting Terminal. You can see your current PATH by typing echo $PATH.
Important config files:
You'll probably want to change these for your own system.
The nice thing about Homebrew installations is that you generally don't need sudo to use or manage these services, since they're in /usr/local.
Alright. Well that did it for me. Enjoy your new dev environment!
You can stop nginx with nginx -s stop, and start it again with just nginx. You can also just reload the conf file with nginx -s reload.
I installed MySQL Workbench and was able to make a connection to the localhost MySQL server by adding a connection to host "localhost" with no password. The only thing I typed was that hostname and everything worked like a charm.
I did use my nginx.conf file from my previous install; you can view a sample conf file if you need it, in my other post about using Macports to do this (link at top of this post).