An Apache, MySQL, PHP stack is really easy to setup on a Mac. MAMP is a very easy solution to achieve this. Apache can be replaced with the lightweight Nginx server and an NMP stack can be created, but may take a little more time to set it up.

Following configuration is tested with Mac OS X v10.10 Yosemite

Before we start, make sure that you install Homebrew – dubbed as the missing package manager for OS X. Its a CLI tool and is used in a way similar to YUM or APT. You can install,  update packages easily via the command line. It uses Ruby and git under the hood. Install it using

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

More details about Homebrew.

After you install Homebrew, run the below commands

brew doctor
brew update

Install Nginx

Install Nginx using the below commands

brew install nginx

Now start Nginx

sudo nginx

This should start Nginx and will use the default port 8080. Try to access http://localhost:8080/ and you should see the welcome page. Nginx configuration can be changed to use a different port and we will explore that shortly.

Install MySQL

Use the below commands to install MySQL.

brew install mysql

After installing MySQL, set the root password and other options using

mysql_secure_installation

 Install PHP-FPM

The default PHP in Homebrew is different from the one that we need. We will tap a special channel to obtain this formula.

brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php
brew install --without-apache --with-fpm --with-mysql php56

Install phpMyAdmin

Before installing phpMyAdmin, check the value of $PHP_AUTOCONF by echoing it.

echo $PHP_AUTOCONF
/usr/local/bin/autoconf

If PHP_AUTOCONF is not setup, check if autoconf is installed.

which autoconf

If that does not return a value, install autoconf.

brew install autoconf

Find the value of autoconf path and set the PHP_AUTOCONF variable, then reload bash profile.

which autoconf
/usr/local/bin/autoconf
echo export PHP_AUTOCONF='/usr/local/bin/autoconf' >> ~/.bash_profile
. ~/.bash_profile
echo $PHP_AUTOCONF
/usr/local/bin/autoconf

Now, install phpMyAdmin

brew install phpmyadmin

 Configurations

Now, lets configure the installed components and make sure they work as expected.

To make the services run at startup, provide a link to the .plist files of the components in ~/Library/LaunchAgents. Mac will use the information in the plist files to launch the services at startup.

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents/
ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents/
ln -sfv /usr/local/Cellar/php56/5.6.2/*.plist ~/Library/LaunchAgents/
sudo nginx
sudo nginx -s quit
mysql.server start
mysql.server stop
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

Using the commands above, start Nginx(if not already started), MySQL and PHP-FPM

To make sure that  PHP-FPM started correctly, use the command below.

lsof -Pni4 | grep LISTEN | grep php

The final piece is to add virtual host configurations to access phpMyAdmin and your other websites using Nginx.

Nginx config file is usually found in the location below

/usr/local/etc/nginx

Create 3 directories in the location to hold the configuration, virtual host definitions and the log files.

cd /usr/local/etc/nginx
mkdir conf.d
mkdir logs
mkdir sites-enabled

Now create the configuration to use PHP-FPM for the php pages in conf.d directory. Create a file named php in conf.d folder

cd conf.d
nano php

Add the following contents to the file.

location ~ \.php$ {
    try_files   $uri = 404;
    include     fastcgi_params;
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_index   index.php;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Lets create a virtual host for phpMyAdmin that uses the config file that we just created. We will create the definition in sites-enabled directory and lets name the file phpmyadmin(or any name of your choice).

cd sites-enabled/
nano nano phpmyadmin

The contents of the file should be similar to the one below

server {
  root    /usr/local/share/phpmyadmin;
  index   index.php;
  listen    8060;
  error_log /usr/local/etc/nginx/logs/phpmyadmin.error.log;
  access_log  /usr/local/etc/nginx/logs/phpmyadmin.access.log;
  server_name localhost;

  include   /usr/local/etc/nginx/conf.d/php;
}

The listen port is set as 8060. You can change that to use your preferred port.

Lets create another virtual host for your php web application. I use Sites folder under my home directory as the root folder.

Create another file named webapp in sites-enabled directory and use the below content

server {
  root    /Users/your_username/Sites;
  index   index.php;
  listen    8070;
  error_log /usr/local/etc/nginx/logs/webapp.error.log;
  access_log  /usr/local/etc/nginx/logs/webapp.access.log;
  server_name localhost;

  include   /usr/local/etc/nginx/conf.d/php;
}

I used 8070 as the port here and the root folder is set as the Sites directory.

Lets create a test page named index.php in the webapp root folder

cd /Users/your_username/Sites
nano index.php

Set the contents as below.

<?php
phpinfo();
?>

Now, configure nginx.conf to include all virtual hosts and set log folder.

Edit nginx.conf in any text editor and make the highlighted changes.

worker_processes  1;

error_log   /usr/local/etc/nginx/logs/error.log;
pid   /usr/local/var/run/nginx/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /usr/local/etc/nginx/logs/access.log;

    sendfile        on;

    keepalive_timeout  65;

    gzip  on;
    include   /usr/local/etc/nginx/sites-enabled/*;

    server {
        listen       8080;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }       
    }

Reload Nginx configuration.

sudo nginx -s reload

Now access the phpMyAdmin url – http://localhost:8060

You should see the login screen and should be able to login with the root password that you set.

Access your webapp – http://localhost:8070

You should see the PHP configuration(output of phpinfo()).

Enjoy!