Install all packages
sudo port install php5 +fastcgi fcgi
Nginx Install
Enough foreplay, lets get to it, install nginx first.- sudo port install nginx
- 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:
- 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:
- sudo nginx
Stop Nginx on demand:
- sudo nginx -s stop
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 dev.mysql.com then just skip ahead to Installing PHP with PHP-CGI. Otherwise keep reading.
Install MySQL via macports
- sudo port install mysql5-server
- #start mysql daemon
- sudo mysqld_safe5 &
- 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.
- #~/.profile
- alias mysql='/opt/local/bin/mysql5'
- alias start_mysql='sudo mysqld_safe5 &'
- 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.- sudo port install php5 +fastcgi fcgi php5-gd php5-mysql php5-mcrypt
Starting php-cgi:
- php-cgi -q -b 127.0.0.1:9000 &
Stopping PHP-CGI:
- 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
- #!/bin/bash
- 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:
#user nobody;
worker_processes 1;
error_log /opt/local/etc/nginx/logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /opt/local/etc/nginx/logs/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 /opt/local/etc/nginx/logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
access_log /opt/local/etc/nginx/logs/host.access.log main;
location / {
root share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /opt/local/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /opt/local/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root share/nginx/html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root share/nginx/html;
# index index.html index.htm;
# }
#}
}
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
- sudo mkdir /var/www
- sudo echo "" > /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
- 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.
Stop and start nginx:
- sudo nginx -s stop
- sudo nginx
Add your new server_name to your /private/etc/hosts file
- 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.