Downloads

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: 2927 Comments: 2 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 http://www.macports.org/install.php 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


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

http://dev.mysql.com/downloads/mysql/

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 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 127.0.0.1:9000 &


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/start_php_cgi.sh

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


Configure Nginx



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

  1. #user  nobody;
  2. worker_processes  1;
  3.  
  4. error_log  /opt/local/logs/error.log;
  5. pid        /opt/local/logs/nginx.pid;
  6.  
  7. events {
  8.     worker_connections  1024;
  9. }
  10.  
  11. http {
  12.     include       mime.types;
  13.     default_type  application/octet-stream;
  14.  
  15.     access_log  /opt/local/logs/access.log  main;
  16.  
  17.     sendfile        on;
  18.     #tcp_nopush     on;
  19.  
  20.     #keepalive_timeout  0;
  21.     keepalive_timeout  65;
  22.     gzip  on;
  23.  
  24.     server {
  25.         listen       80;
  26.         server_name  localhost;
  27.         root         /var/www;
  28.         index  index.html index.htm index.php;
  29.  
  30.         # redirect server error pages to the static page /50x.html
  31.         #
  32.         error_page   500 502 503 504  /50x.html;
  33.         location = /50x.html {
  34.             root   /var/www;
  35.         }
  36.  
  37.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  38.         #
  39.         location ~ \.php$ {
  40.             #root           /var/www;
  41.             fastcgi_pass   127.0.0.1:9000;
  42.             fastcgi_index  index.php;
  43.             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
  44.             include        fastcgi_params;
  45.         }
  46.     }
  47. }


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: http://www.phpmyadmin.net/home_page/downloads.php (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 http://git-scm.com but you could install it using macports if you want. After you have git up and running run:



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:
  1. server {
  2.   listen          80;
  3.   server_name     dev.testapp.devlocal;
  4.   access_log      /var/log/nginx/dev.testapp.devlocal.access.log;
  5.   error_log       /var/log/nginx/dev.testapp.devlocal.error.log;
  6.   rewrite_log     on;
  7.   root            /path/to/cakephp/app/testapp/app/webroot;
  8.   index           index.php;
  9.  
  10.   # Not found this on disk?
  11.   # Feed to CakePHP for further processing!
  12.   if (!-e $request_filename) {
  13.     rewrite ^/(.+)$ /index.php?url=$1 last;
  14.     break;
  15.   }
  16.  
  17.   # Pass the PHP scripts to FastCGI server
  18.   # listening on 127.0.0.1:9000
  19.   location ~ \.php$ {
  20.     fastcgi_pass   127.0.0.1:9000;
  21.     fastcgi_index  index.php;
  22.     fastcgi_intercept_errors on; # to support 404s for PHP files no$
  23.     fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
  24.     include fastcgi_params;
  25.   }
  26.  
  27.   # Deny access to .htaccess files,
  28.   # git & svn repositories, etc
  29.   location ~ /(\.ht|\.git|\.svn) {
  30.     deny  all;
  31.   }
  32. }


Then I would add a single line to the nginx.conf file right before the http enclosure
  1. 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
  1. 127.0.0.1 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.