melp.nl

< Return to main page

NginX for Apache users

Since some time now, I've been using NginX as a local development web server. After numerous years of Apache, the configuration of NginX needs some getting used to, but in the end, it is a lot easier and more flexible. Here are a few mappings to get you started if you're used to Apache.

Virtual Hosts

Apache

#!apache
<VirtualHost *:80>
    ServerName example.org
    ServerAlias example.com

    DocumentRoot /var/www/example.org

    <Directory /var/www/example.org>
        DirectoryIndex index.php
        Allow from All
    </Directory>
</VirtualHost>

NginX

#!nginx
# inside http { ... }
server {
    listen 80;
    server_name example.org example.com;
    root /var/www/example.org;

    location / {
        index index.php;
        allow all;
    }
}

PHP configuration

For PHP, you need a fastcgi server locally.

PHP 5.2.x

Easiest is to use php-cgi:

#!shell
php-cgi -b :9000 &

This starts PHP as a fastcgi server serving at port 9000. This is, for many reasons, not the most ideal way to start it, but it is by far the easiest. It might crash sometimes, so whenever you get trouble with that, you might want to get into installing php-fpm. Remember though, that the FastCGI process is running under the current user, so it has all the rights the user that started it has. For local development usage this is no problem (even easier), but of course you should never do this in an production environment.

PHP 5.3.x

PHP 5.3 has the FastCGI Process Manager built in, so simply fire up php-fpm:

 # 5.3.x
/usr/local/sbin/php-fpm

Pass PHP files to FastCGI process

You need to tell NginX to handle all url's that end in .php to pass through to the FastCGI server at :9000, like this:

#!nginx@1
location ~ \.php$ {
    include fastcgi_params; 
    fastcgi_index     index.php;
    fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass     127.0.0.1:9000;
}
  1. The location directive with the ~ means "any uri matching the following regexp", so in this case, any uri ending with .php.
  2. The include directive directly includes a configuration file from the nginx configuration root. The file fastcgi_params is included in the default installation. Read the file to get a feeling of what it does.
  3. This makes sure that any URI that ends in a slash is appended with index.php
  4. The actual PHP script to be handled by the FastCGI server
  5. The command to pass the request through to the FastCGI server.

Front controllers

A front controller typically is the index.php handling all URI's for you that aren't assets such as css, javascript, images, etc. You know: what most CMS'es and frameworks out there use nowadays and usually takes the following Apache mod_rewrite directives to work.

RewriteEngine On
RewriteCond !-f %{REQUEST_URI}
RewriteCond !-d %{REQUEST_URI}
RewriteRule .* index.php

The literal translation:

if (!-e $request_uri ) {
    rewrite .* /index.php
}

The better solution

try_files $uri /index.php?$args;

Or in case of Drupal

try_files $uri /index.php?q=$uri&$args;

MultiViews

A lot of apache installations have multiviews enabled. You can do this, again, with the try_files directive:

location ~ ^(/.+)/ {
    try_files $uri $1?$args $1.php?$args;
}

Though it does not exactly do what Multiviews in Apache do, (like making it possible to omit the file extension), in PHP configurations it is 9 times out of ten you need the above behaviour, letting the PHP file handle all requests containing the filename and some slashes appended.


< Return to main page


You're looking at a very minimalistic archived version of this website. I wish to preserve to content, but I no longer wish to maintain Wordpress, nor handle the abuse that comes with that.