#

WordPress, nginx, Linode, oh my!

December 18, 2013

I recently switched from HostNine to Linode for my web-hosting needs. Nothing against HostNine, they’re a great company with superb support and excellent shared hosting. It’s just that I needed more. Linode offers great pricing on VPS service, and it’s extremely fast. It’s also completely root, so I feel at home with complete SSH/SFTP filesystem access.

Transferring the site files was a trivial task (as it should be for anyone with at least rudimentary webserver knowledge). Transferring databases is made easy via phpMyAdmin.

But there is one thing I had to consider: what web server?

Most shared hosting providers choose this for you, and it’s almost always Apache. Apache is a great project; it is a facet of the FOSS community and has matured over many years of development into a powerful server. But there is always competition. nginx is another open-source web server with an emphasis on performance. nginx is insanely fast. But it is configured differently than Apache, so getting PHP, MySQL,  and WordPress to work with it took a bit of research.

The nginx article on the WordPress Codex was a fantastic resource. WordPress requires some URL rewrite rules to enable the pretty permalinks, like the one for this article. With Apache, rewriting is achieved via a .htaccess file. nginx uses configuration files similar to JSON. The core rules required to make WordPress permalinks be pretty are as follows:

# WordPress single blog rules.
# Designed to be included in any server {} block.

# This order might seem weird - this is attempted to match last if rules below fail.
# http://wiki.nginx.org/HttpCoreModule
location / {
	try_files $uri $uri/ /index.php?$args;
}

# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

That made WordPress work, but a problem persisted: I couldn’t upload pictures via the WordPress dashboard. This was obviously a permissions issue. I expected nginx to be running as root, but to my surprise, it was not:

 

On Linux, the following command will reveal the user a particular process is running as:

ps -elf | grep nginx

This revealed that nginx is a process of the “www-data” user. Changing permissions of the uploads folder appropriately solved the problem.

Interestingly, PHP also has a built-in way to see who owns the process:

echo exec("whoami");

Initial configuration was a bit of work, but I’m a happy camper. nginx and PHP with FPM is fast. Ricky Bobby-fast.

Leave a Reply

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