Running WordPress on Nginx

Hopefully, this is old-hat to many of you by now, but trying to figure out how to get my Apache configs to play nicely with my Nginx configs on my local dev machine has proven – shall we say – challenging. I had a need to spin up my WordPress instance locally for some theme and plugin development a couple months ago and decided to bite the bullet and just get it running on Nginx so that I didn’t have to switch server contexts between my PHP and Node.js projects.

It took me a little time to work out all the nuances, but when everything was said and done, it really wasn’t very hard. For me, I generally set up a dev.* subdomain on my local box for whatever site I’m currently working on. So, if I’m actively developing my site here, I usually start with simply verifying my /etc/hosts entry (sorry PC users, I’m working Mac all the way here):

Once you’ve registered your hosts, requests to the specified domain will be routed to that IP address (localhost). So, now, we need to be sure that we have something able to listen on port 80 to resolve those requests. In my case, I like to create a new server conf file for any new projects I’m wanting to run locally. In this case, I’ll create a new file here:

The following config is pretty self-explanatory, but we will look at it in a little depth:

The first section to call out is where we tell Nginx the order in which to look for index files in our directories. Immediately after, we include a conf file we have created explicitly for PHP handling:

The php-fpm file essentially tells Nginx how and where to process PHP requests via FastCGI:

This config assumes you’re running FastCGI on the default port 9000, obviously. Once this is set up, you can restart Nginx, and requests to your host will begin flowing.

In order to get the WordPress piece working well, I just needed to reflect some of my Apache mod_rewrite rules here in Nginx. That is what the following lines are doing:

With this redirect in place, all routes will be passed through to the WordPress query parser at index.php.

That’s really all there is to it! With your configuration set up and Nginx restarted, WordPress will be served locally mirrored to the previous Apache behavior.

PHP Routing Library

For the past few years, I have been working more and more with Node.js and proportionally less frequently with PHP. As might be expected, though the underpinnings of web behavior is platform agnostic, the architecture for the web application layer changes to suite the technology stack in which you are building. In fact, one of the marks of a savvy engineer is the ability to leverage the best parts of a technology in the way they build their software. However, I found myself spending far too much time trying to relearn techniques in PHP for projects as I came back to them, so I decided to see if I could build a lightweight routing system in PHP that would mirror some of the patterns I use in my Node.js work every day.

Anyone who has worked with Express, Hapi, Django, Rails or even Grails can understand the ease of route declaration with something like this:

Continue reading →