WordPress on OS X with Nginx, PHP, and mysql

wordpress-logo-notext-rgb

Recently, I wanted to do some WordPress development on my Mac.  I’ve got Ubuntu installed on a virtual machine, but I decided to get the stack running on OS X.

I’ve been using Nginx for a while now as a web server, caching proxy, and load balancer for some Plone sites.   It’s been fantastic – fast, reliable, easy to configure – greatly simplifying my life as a sysadmin.  I was interested to see how it would work for serving wordpress.

If you haven’t checked out homebrew, you should.   No, it’s not beer, but a package manager for OS X.  Homebrew is, they claim and I agree, the easiest and most flexible way to install the UNIX tools Apple didn’t include with OS X.

First, install and start the mysql server:

brew install mysql
/usr/local/bin/mysqld_safe --datadir=/usr/local/Cellar/mysql/5.5.10/data

Installing PHP is a little more complicated, since there isn’t an official homebrew formula. Instead, grab this formula:

wget https://github.com/ampt/homebrew/raw/php/Library/Formula/php.rb
mv php.rb `brew --prefix`/Library/Formula

Then, build PHP with mysql and fastcgi support:

brew install php --with-mysql --with-fpm

Tell PHP to listen on port 9000:

/usr/local/Cellar/php/5.3.6/bin/php-cgi -b 9000

Installing nginx is as simple as:

brew install nginx

You’ll need to modify the nginx.conf, which can be found in /usr/local/etc/nginx, and add this configuration:

server {
    listen       8080;
    server_name  localhost;

    location / {
        root   /path/to/wordpress;
        index  index.php index.html index.htm;
    }

    location ~ .php {
    include fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /path/to/wordpress/$fastcgi_script_name;
}

Start the Nginx server like this:

/usr/local/sbin/nginx -c /usr/local/etc/nginx/nginx.conf

There are more robust ways to start these services, but since this is just a development environment, I prefer not to have them running unless I am actively working with them. Now you should be ready to install WordPress.  Happy developing!

9 thoughts on “WordPress on OS X with Nginx, PHP, and mysql

  1. Hi, thank you for writing this post.

    I have a problem with starting php-cgi, the file isn’t there.
    $ls /usr/local/Cellar/php/5.3.6/bin/

    -rwxr-xr-x 1 root staff 884 May 19 00:10 pear
    -rwxr-xr-x 1 root staff 905 May 19 00:10 peardev
    -rwxr-xr-x 1 root staff 821 May 19 00:10 pecl
    lrwxr-xr-x 1 root staff 41 May 19 00:10 phar -> /usr/local/Cellar/php/5.3.6/bin/phar.phar
    -rwxr-xr-x 1 root staff 14813 May 19 00:10 phar.phar
    -rwxr-xr-x 1 root staff 10796292 May 19 00:10 php
    -rwxr-xr-x 1 root staff 3861 May 19 00:10 php-config
    -rwxr-xr-x 1 root staff 4540 May 19 00:10 phpize

      1. I didn’t work. But I figured out and here what I did and worked.

        Start php
        $ sudo /usr/local/sbin/php-fpm

        Stop php
        $ sudo killall -c php-fpm

        Start nginx
        $ sudo /usr/local/sbin/nginx -c /usr/local/etc/nginx/nginx.conf

        Stop nginx (must enable pid /usr/local/etc/nginx/nginx.pid; in /usr/local/etc/nginx/nginx.conf)
        $ sudo sudo kill `cat /usr/local/etc/nginx/nginx.pid`

        I don’t know why php-cgi didn’t install, and also I don’t know what is the difference between php-cgi and php-fpm

  2. Commenting for the benefit of others. Slight modifications of the above worked for me.

    First, use the Homebrew – Alt repository:

    https://raw.github.com/adamv/homebrew-alt/master/duplicates/php.rb

    Second, make sure you have GMP installed first:

    `brew install gmp`

    Third, you will not have php-cgi as noted in the comment above. Don’t worry about it, use php-fpm.

    You’ll need to copy the default php-fpm conf file that gets created.

    cp /usr/local/Cellar/php/5.3.8/etc/php-fpm.conf.default /usr/local/Cellar/php/5.3.8/etc/php-fpm.conf
    (or whatever version of PHP you’ve installed by now, obv.)

    Edit it and set pm.whatevers however you’d like. You can also specify the port and everything else in that file.

    If you don’t, you’ll get errors when running:

    $ sudo /usr/local/sbin/php-fpm

    Then you can start php as noted here and in Gaston’s comment. Follow the nginx instructions as included in the main post.

    gl

    1. And how do you stop the service?

      All I can come up with is grep the pid from ‘ps aux’ and kill them, but I’m sure is not the proper way

Leave a Reply

Your email address will not be published. Required fields are marked *