Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Nginx, PHP-FPM, MySQL and phpMyAdmin on OS X

This is my take on how to get up and running with NGINX, PHP-FPM, MySQL and phpMyAdmin on OSX Yosemite.

This article is adapted from the original by Jonas Friedmann. Who I just discovered is from Würzburg in Germany. A stonesthrow from where I was born ;)


Make sure you have the latest version of XCode installed. Available from the Mac App Store.

Install the Xcode Command Line Tools:

xcode-select --install


Homebrew is the missing package manager for OSX.

Download and install using the following command:

ruby -e "$(curl -fsSL"

Check for any problems or conflicts:

brew doctor

Update and Upgrade brew formulas:

brew update && brew upgrade


We will need to add some extra php formulas to brew so that we can install PHP and PHP-FPM:

brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php

tip: to uninstall a tap do

brew untap xyz

We can now install php:

brew install --without-apache --with-fpm --with-mysql php56

This will compile PHP on your machine and may take a few minutes.

Setup auto start

Make sure you use the version number that got installed previously:

mkdir -p ~/Library/LaunchAgents
cp /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/

Start PHP-FPM:

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

Check that PHP-FPM is listening on port 9000:

lsof -Pni4 | grep LISTEN | grep php


brew install mysql

Setup auto start

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents

And start the database server:

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

Secure the installation


Test the connection

mysql -uroot -p


brew install phpmyadmin


brew install nginx

Setup auto start

Since we want to use port 80 have to start the Nginx process as root:

sudo cp /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/
sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

Test web server

Start Nginx for the first time:

sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

The default configuration is set that it will listen on port 8080 instead of the HTTP standard 80. Ignore that for now:

curl -IL http://localhost:8080

The output should look like:

HTTP/1.1 200 OK
Server: nginx/1.6.0
Date: Tue, 08 Jul 2014 21:40:38 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 08 Jul 2014 21:35:25 GMT
Connection: keep-alive
ETag: "53bc641d-264"
Accept-Ranges: bytes

Stop Nginx again:

sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist



Create some folders which we are going to use in the configuration files:

mkdir -p /usr/local/etc/nginx/logs
mkdir -p /usr/local/etc/nginx/sites-available
mkdir -p /usr/local/etc/nginx/sites-enabled
mkdir -p /usr/local/etc/nginx/conf.d
mkdir -p /usr/local/etc/nginx/ssl
sudo mkdir -p /var/www
sudo chown :staff /var/www
sudo chmod 775 /var/www

Remove the current default nginx.conf (also available as /usr/local/etc/nginx/nginx.conf.default in case you want to take a look) and download this custom one via curl from GitHub:

rm /usr/local/etc/nginx/nginx.conf

curl -L -o /usr/local/etc/nginx/nginx.conf

Download the following PHP-FPM configuration from GitHub:

curl -L -o /usr/local/etc/nginx/conf.d/php-fpm

Create default virtual hosts

curl -L -o /usr/local/etc/nginx/sites-available/default

curl -L -o /usr/local/etc/nginx/sites-available/default-ssl

curl -L -o /usr/local/etc/nginx/sites-available/phpmyadmin

Clone my example virtual host (including 404, 403, and phpinfo() rewrite) using git

git clone /var/www

rm -rf /var/www/.git

Setup SSL

Create folder for our SSL certificates and private keys:

mkdir -p /usr/local/etc/nginx/ssl

Generate 4096bit RSA keys and the self-sign the certificates in one command:

openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj '/C=US/ST=State/L=Town/O=Office/CN=localhost' -keyout /usr/local/etc/nginx/ssl/localhost.key -out /usr/local/etc/nginx/ssl/localhost.crt

openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj '/C=US/ST=State/L=Town/O=Office/CN=phpmyadmin' -keyout /usr/local/etc/nginx/ssl/phpmyadmin.key -out /usr/local/etc/nginx/ssl/phpmyadmin.crt

Enable virtual hosts

Now we need to symlink the virtual hosts that we want to enable into the sites-enabled folder:

ln -sfv /usr/local/etc/nginx/sites-available/default /usr/local/etc/nginx/sites-enabled/default

ln -sfv /usr/local/etc/nginx/sites-available/default-ssl /usr/local/etc/nginx/sites-enabled/default-ssl

ln -sfv /usr/local/etc/nginx/sites-available/phpmyadmin /usr/local/etc/nginx/sites-enabled/phpmyadmin

Start Nginx again:

sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

Final tests

Thats it, everything should be up and running. Click on the links below to ensure that:

Control your services like a boss

Because you probably need to restart the one or other service sooner or later, you probably want to set up some alias. Download aliases:

curl -L -o /tmp/.aliases

For bash:

cat /tmp/.aliases >> ~/.profile

For zsh:

cat /tmp/.aliases >> ~/.zshrc 

Close and open the terminal or type source ~/.profile or source ~/.zshrc to reload the profile.

The following commands are now available







Nginx Logs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment