After dealing with nginx for some time I loathe the thought of going back to Apache. Therefore when I need to set up a new environment, even if its just for development, I try and make it work with nginx first. We'll assume you're starting out with a fresh install of Ubuntu Server 11.10.

To start out, we install all the necessary packages using apt-get.

sudo apt-get install nginx postgresql php5-common php5-cli php5-suhosin php5-pgsql php5-cgi php5-gd php5-curl php5-fpm

You'll see a lot of other nginx + php tutorials outline writing your own fastcgi init script or to grab the spawn-fcgi process from lighthttpd. Neither of these are necessary since php 5.3 as we can now install the FastCGI Process Manager (php5-fpm).

Now let's start nginx and php5-fpm.

sudo /etc/init.d/php5-fpm start && sudo /etc/init.d/nginx start

You can test this by browsing to the IP of the server, or localhost if this is being done on your local machine. You should see a “Welcome to nginx!” message.

Now we need to set up our first virtual host and install Drupal. Let's start by creating a new site file at @/etc/nginx/sites-available/@.

sudo vi /etc/nginx/sites-available/drup7

Now we need to enter the following contents into this new file [source]:

    server {
            root /var/www/drup7; ## <-- Your only path reference.

            location = /favicon.ico {
                    log_not_found off;
                    access_log off;

            location = /robots.txt {
                    allow all;
                    log_not_found off;
                    access_log off;

            # This matters if you use drush
            location = /backup {
                    deny all;

            # Very rarely should these ever be accessed outside of your lan
            location ~* .(txt|log)$ {
                    deny all;

            location ~ ..*/.*.php$ {
                    return 403;

            location / {
                    # This is cool because no php is touched for static content
                    try_files $uri @rewrite;

            location @rewrite {
                    # Some modules enforce no slash (/) at the end of the URL
                    # Else this rewrite block wouldn't be needed (GlobalRedirect)
                    rewrite ^/(.*)$ /index.php?q=$1;

            location ~ .php$ {
                    fastcgi_split_path_info ^(.+.php)(/.+)$;
                    #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                    include fastcgi_params;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    fastcgi_intercept_errors on;

            # Fighting with ImageCache? This little gem is amazing.
            location ~ ^/sites/.*/files/imagecache/ {
                    try_files $uri @rewrite;
            # Catch image styles for D7 too.
            location ~ ^/sites/.*/files/styles/ {
                    try_files $uri @rewrite;

            location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
                    expires max;
                    log_not_found off;

Save and close. I want my site to be accessed via so this domain was used as the server_name. Let's enable our new site by creating a symbolic link:

    sudo ln -s /etc/nginx/sites-available/drup7 /etc/nginx/sites-enabled/

Ok, our site is set up. Now lets go make sure /var/www/drup7 exists and populate it with the Drupal source.

    sudo mkdir /var/www
    cd /var/www
    sudo wget
    sudo tar xvf drupal-7.8.tar.gz
    sudo mv drupal-7.8 drup7
    sudo rm drupal-7.8.tar.gz

Now let's make a settings.php file and set ownership of the whole directory to the www-data user.

    sudo cp drup7/sites/default/default.settings.php drup7/sites/default/settings.php
    sudo chown -R www-data:www-data drup7

Ok, now we need to create a postgres user and dataabse.

    sudo -u postgres -i
    createuser drup7 --pwprompt --encrypted
    createdb drup7 --owner=drup7

You'll be prompted for a password and several access-releated questions when you run createuser, you'll probably want to answer “n” for all of these.

Since we're using a real domain in the virtual host file, you'll either need to configure your DNS settings to point to the server you're working on, or more likely just change your hosts file. I'm working on a virtual machine, so my hosts entry will look like so:

We've made a bunch of changes, let's restart php5-fpm and nginx:

    sudo /etc/init.d/php5-fpm stop && sudo /etc/init.d/nginx stop
    sudo /etc/init.d/php5-fpm start && sudo /etc/init.d/nginx start

Fire up your favorite browser and navigate to! You should see the Drupal installation screen. Now go through the installation wizard, it should automatically detect that you have Postres installed and not MySQL.

Pat yourself on the back, you're all done!

Share this article:

If this resonates with you I'd love to help!

I help business with problems like these every day.

Click below to schedule a call.

About The Author

Bryce Hamrick

Facebook Twitter

Bryce Hamrick is an entrepreneur, business & marketing strategist, and product consultant with nearly two decades of experience in industry. Bryce has been a software engineer, product manager, and director of product management for startups as well as large enterprises. He has led teams to bring dozens of products to market and has executed numerous six-figure product launches. Today Bryce and his team focus on leveraging his product execution strategy to help businesses with growth and scale.