Install PHP 5.4 and nginx on Mac OS X

It's exquisitely difficult to get PHP and a web server running on Mac OS X. Read on for how I got a minimally viable nginx and PHP 5.4 install set up.

By Matthew Malinowski, 2013-02-08


I have tried several times to get a PHP 5.4 development environment running on my local machine and it's never gone well. Today I finally got the basics figured out.

PHP, Apache, and nginx have so many configuration options. It is very bewildering to look at them all. I found many tutorials didn't explain the choices they made. For the thoroughgoing, it's best to simply read the man pages and documentation.

As such, this is a "minimum viable" installation. The goal is to simply get a functioning webserver and functioning PHP. Fiddling is for another post.

Mac OS X 10.8.2 comes with an older version of PHP, and a fairly recent version of Apache. It's fairly straightforward to simply use these. If you don't need newer versions of PHP or Apache and don't mind using sudo, Damiano Ferrari's instructions will start you down the right path. You'll have a functioning development environment in about 10 minutes.

However, if you need a non-default version of PHP, you don't want to use Apache, or you don't want to / can't use sudo, read on. We're going to get PHP 5.4 and nginx working alright together.

You need homebrew. Installing homebrew and getting things like $PATH to work is beyond the scope of this post. Tutorials abound!

Then, you need PHP. Jose Gonzalez faithfully maintains a repo of brews for PHP stuff here. Follow the directions in his README.md! Actually do this.

You should now have everything updated and have dupes and homebrew-php repos tapped.

You can review the options available to you for php54 with brew options php54, but we're just going to install php54 FPM. Run brew install php54 --with-fpm. It'll take a while to download and compile. brew will tell you a bunch of stuff when php54 is finished compiling. You might like to read it.

Run which php-fpm. It should return /usr/local/sbin/php-fpm. If it doesn't, close and reopen your Terminal window, and try again. If it still doesn't, something's wrong with your $PATH. Tutorials abound for this as well.

You can actually launch php-fpm now with php-fpm -F. (The -F flag keeps the php-fpm process foregrounded.) You shouldn't see any significant errors. Kill it with Ctrl-C.

Install nginx now with brew install nginx. This will take a few moments to compile, then it will also tell you things. Read them.

which nginx should return /usr/local/sbin/nginx. You can run nginx now. Open a browser and go to localhost:8080, you should see a welcome page. Stop nginx with nginx -s stop.

nginx's default HTML root should be at /usr/local/Cellar/nginx/$your-version-number/html. Go there. The index.html file should match the one you saw at localhost:8080. Make a new file in there called info.php or something with the contents <?php phpinfo(); ?>.

You now need to get nginx to send PHP requests to php-fpm. Execute nginx -t and open the configuration file returned. (Note that you should generally back up these default config files, but nginx comes with a "nginx.conf.default" file in that dir.)

Find the line #location ~ \.php$ {. Uncomment that section (to the '}').

Change the line fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; to fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;.

Start nginx (nginx) and php-fpm (php-fpm -F). Open your browser. Go to localhost:8080/info.php, and you should see the output of phpinfo()!

So: we've accomplished our goal here. You now have a functional -- though very rudimentary -- PHP environment. The next steps in your internet travels will be things like setting up virtual hosts so you can have different sites (sites not located 10 directories deep in /usr, either), and making php-fpm more efficient.


Back to Blog Index...