Setting up a PHP development environment with Nginx on Ubuntu 11.04

Warning: This blog post was written a long time ago and might be no longer relevant.
Nginx and PHP logo

I already described how to prepare a PHP development environment with Nginx on Ubuntu 10.04.

Ubuntu 11.04 (Natty Narwhal) simplifies the procedure a lot since php-fpm is available in the PHP 5.3 core.

As I also managed to improve my Nginx configuration for PHP I decided to write on the subject again.

PHP

Install PHP with chosen modules (whatever you need):

sudo apt-get install php5-fpm php5-cli php5-common php5-curl php5-gd \
  php5-mcrypt php5-mysql php5-pgsql php5-sqlite php5-tidy php5-xmlrpc \
  php5-xsl php5-intl php5-imagick php5-xdebug php-apc php-pear

Note: Standard configuration is just fine to start with. In case you want to tweak it - all the PHP and php-fpm configuration files are located in the /etc/php5/fpm directory.

Nginx

Nginx installation is not complicated as well. Following command will install it with standard core modules:

sudo apt-get install nginx

I usually use fake domains for development (like kuba.dev or myproject.dev). All virtual hosts are handled with a single configuration file (/etc/nginx/sites-available/dev):

server {
    listen 80 default;
    server_name *.dev;

    root /var/www/$host/web;

    access_log /var/log/nginx/$host-access.log;
    error_log  /var/log/nginx/dev-error.log error;

    index index.php index.html index.htm;

    try_files $uri $uri/ @rewrite;

    location @rewrite {
        rewrite ^/(.*)$ /index.php/$1;
    }

    location ~ \.php {
        # try_files $uri =404;

        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9000;

        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
        deny all;
    }
}

You need to enable the configuration:

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

Note: I explained the Nginx directives and options in my another blog post "Nginx configuration for Symfony projects".

System configuration

To avoid using sudo in the future you will need to make sure that files in the /var/www directory can be altered by you. First add yourself to www-data group and relogin:

sudo usermod -a -G www-data kuba
sudo su kuba

Then allow everyone in the group to create virtual hosts:

sudo mkdir /var/www
sudo chown -R www-data:www-data /var/www
sudo chmod -R 775 /var/www

Define your domains in /etc/hosts (this can be done much better with the dnsmasq but for now hosts file should be sufficient):

127.0.1.1 loki info.dev kuba.dev myproject.dev

Tests

Create a test website:

mkdir /var/www/info.dev/web -p
echo "<?php echo phpinfo(); ?>" > /var/www/info.dev/web/index.php
chown -R :www-data /var/www/info.dev
chmod -R 775 /var/www/info.dev

Start php-fpm and Nginx services:

sudo service php5-fpm start
sudo service nginx start

Open http://info.dev in your favorite browser. You should see a detailed information about your PHP installation.

phpinfo
Jakub Zalas

Jakub Zalas

Agile PHP Developer, addicted to BDD and Symfony