Google Ad

CakePHP 2.0 + Nginx + PHP-CGI + phpMyAdmin on Mac OS X 10.7 Lion

Date: Mon, Nov 7th 2011, 15:34 Author: nick Views: 42171 Comments share
Setup CakePHP 2.0 nginx + php-cgi + mysql + phpmyadmin on OS X Lion

My company just bought me a new macbook and while it shouldn't have taken me as long as it did to setup my development environment for CakePHP, there were a few gotchas that I thought maybe you could learn from my mistake or perhaps correct my wrongs. So without futher adue, lets jump right in.

Mac Ports is our friend!

Coming to mac from Ubuntu, I'm really (read extremely) thankful macports exists. Apple's App Store is nice, but as of now I couldn't find any decent development tools in the app store to easily download and setup. I'm comfortable with the command line so as soon as I discovered macports I was right at home.

To install macports you'll first need to install xcode from the Apple App Store. Once you've installed xcode, simply hop over to to download the latest dmg for lion.

Once you've successfully installed macports you're ready to rock and roll. I then install nginx, php-cgi, php5, php5-gd, php5-mysql, php5-mcrypt using the command line.

Mac comes with PHP installed already, but instead of trying to get that version to work with everything I instead just used macports to install a clean version of PHP for me to configure to how I like things to run completely separate from the mac's default setup.

Nginx Install

Enough foreplay, lets get to it, install nginx first.

  1. sudo port install nginx

Copy working configuration default into its own configuration
  1. sudo cp /opt/local/etc/nginx/nginx.conf.default /opt/local/etc/nginx/nginx.conf

If you want to launch nginx on system startup simply run the plist insalled by the port:
  1. sudo launchctl load -w /Library/LaunchDaemons/org.mackports.nginx.plist

TIP: start and stop nginx on demand from the command line, but don't do it yet! We have to configure it more first.
Start Nginx on demand:
  1. sudo nginx
Stop Nginx on demand:
  1. sudo nginx -s stop

Installing MySQL

EDIT: I've gone back and re-installed mysql via macports for easy package management. Skip to the next section to install that way, or continue reading on how to install MySQL from the official package from

I could have gone with macports, and in retrospect, perhaps I should have, but I actually went ahead and installed the official package from here:

I selected the 64bit arch Mac OS ver 10.6, which works fine under Lion (10.7). This version comes with a nice little system preferences pane so you can stop and start it directly from your mac system preferences -- slick. Once you've installed that lets move onto the rest.

The default install of mysql doesn't include a password for your root login, this won't play nice with phpMyAdmin when we finally install it, so go ahead and add a root password by running this command:

  1. sudo mysqladmin -u root password NEWPASSWORD

If you get a mysqladmin command not found, make sure to add /usr/local/mysql/bin path in your ~/.profile and try again.

  1. # .profile
  2. export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/mysql/bin:$PATH

To reload your profile type:
  1. . .profile

Installing MySQL via MacPorts

I had a change of heart and decided to install mysql5 via MacPorts to keep it all under one package manager. If you've already installed MySQL using the package provided by then just skip ahead to Installing PHP with PHP-CGI. Otherwise keep reading.

Install MySQL via macports
  1. sudo port install mysql5-server

Once complete you'll need to start the mysql daemon and set a root password

  1. #start mysql daemon
  2. sudo mysqld_safe5 &

  1. sudo mysqladmin5 -u root password NEWPASSWORD

You will need to start mysql daemon whenever you wish to use it. I'd sugget making a shell command or an alias in your ~/.profile file.

  1. #~/.profile
  2. alias mysql='/opt/local/bin/mysql5'
  3. alias start_mysql='sudo mysqld_safe5 &'
  4. alias stop_mysql='mysqladmin5 --user=root --password=NEWPASSWORD shutdown'

Now you're all set with MySQL from MacPorts.

Installing PHP with PHP-CGI (FastCGI)

Like I said before, Lion comes with its own PHP install, but I much prefer to work with the package manager whenever possible, so I'm installing a fresh copy of PHP in /opt/local. Let's also install php5 with fastcgi (we need for nginx to talk to PHP) along with some of the nicer libraries we want access to for phpMyAdmin and mysql.

  1. sudo port install php5 +fastcgi fcgi php5-gd php5-mysql php5-mcrypt

Starting php-cgi:
  1. php-cgi -q -b &

Stopping PHP-CGI:
  1. sudo killall php-cgi

You'll need to start the php-cgi whenever you want nginx to talk to PHP via CGI daemon. I ended up writting a little bash to start this for me with a simple command and saved it to ~/bin/

  1. #!/bin/bash
  2. php-cgi -q -b &

Configure Nginx

Open up /opt/local/etc/nginx/nginx.conf in your favorite editor and configure it:

[geshi=code] #user nobody;
worker_processes 1;

error_log /opt/local/logs/error.log;
pid /opt/local/logs/;

events {
worker_connections 1024;

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

access_log /opt/local/logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;

server {
listen 80;
server_name localhost;
root /var/www;
index index.html index.htm index.php;

# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www;

# pass the PHP scripts to FastCGI server listening on
location ~ \.php$ {
#root /var/www;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;

This is a basic nginx setup to run with PHP-CGI nicely. You'll have to add a little more to get it to work nicely with a CakePHP install, but we'll get to that soon enough. As you can see, I like to root things in /var/www, make sure this directory exists for you

  1. sudo mkdir /var/www

Lets write a testfile to see what we've got so far!
  1. sudo echo "<?php echo phpinfo(); ?>" > /var/www/index.php

Start nginx, php-cgi and then navigate to localhost/index.php to see your hard work in all its glory.

Installing phpMyAdmin

Grab the latest version of phpMyAdmin from: (version 3.4.7 when writing) and unpack it wherever you'd like (I put mine in /opt/local/phpMyAdmin).

Since I like to keep my route in /var/www as my default for nginx, I then created a symlink between where I installed phpMyAdmin and /var/www/phpMyAdmin

  1. sudo ln -s /opt/local/phpMyAdmin /var/www/phpMyAdmin

Now navigate to http://localhost/phpMyAdmin and you should be greeted with a nice phpMyAdmin screen.

Install CakePHP 2.0

If you've gotten this far, GOOD JOB! To get to this point it took me nearly an entire day. We're on the home stretch, just a few more things to get CakePHP 2.0 to bake and work on your new sparkly mac!

Grab the latest version of CakePHP from github, you may first have to install git. I grabbed a copy from but you could install it using macports if you want. After you have git up and running run:

[geshi=code] git clone

Once you've cloned the repository note the location where you cloned it and update your .profile with an alias so you can run cake commands from anywhere

  1. # .profile
  2. alias cake="/path/to/cakephp/lib/Cake/Console/cake"

Reload your profile
  1. . .profile

Now lets bake our first app!
  1. cd path/to/where/you/want/app
  2. cake bake app

Now you'll need to configure a new nginx virtual host for your cakePHP app. I personally like to create new site files and name them for each virtual site I want to run.

For this example I would save a site in /opt/local/etc/nginx/testapp.conf with the following:
[geshi=code] server {
listen 80;
server_name dev.testapp.devlocal;
access_log /var/log/nginx/dev.testapp.devlocal.access.log;
error_log /var/log/nginx/dev.testapp.devlocal.error.log;
rewrite_log on;
root /path/to/cakephp/app/testapp/app/webroot;
index index.php;

# Not found this on disk?
# Feed to CakePHP for further processing!
if (!-e $request_filename) {
rewrite ^/(.+)$ /index.php?url=$1 last;

# Pass the PHP scripts to FastCGI server
# listening on
location ~ \.php$ {
fastcgi_index index.php;
fastcgi_intercept_errors on; # to support 404s for PHP files no$
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;

# Deny access to .htaccess files,
# git & svn repositories, etc
location ~ /(\.ht|\.git|\.svn) {
deny all;

Then I would add a single line to the nginx.conf file right before the http enclosure
[geshi=code] include testapp.conf;

Stop and start nginx:
  1. sudo nginx -s stop
  2. sudo nginx

Add your new server_name to your /private/etc/hosts file
[geshi=code] localhost dev.testapp.devlocal

Now navigate to http://dev.testapp.devlocal on your machine and you should see your brand new baked CakePHP 2.0 app! Congratulations, happy baking!

I hope this tutorial helped, comments are appreciated.