We start by installing dotdeb APT repository to get easy access to the latest .deb packages for some of our components like NginX, PHP stack etc.

$ sudo su -
# echo "deb http://packages.dotdeb.org stable all" >> /etc/apt/sources.list
# echo "deb-src http://packages.dotdeb.org stable all" >> /etc/apt/sources.list
# echo "deb http://php53.dotdeb.org stable all" >> /etc/apt/sources.list
# echo "deb-src http://php53.dotdeb.org stable all" >> /etc/apt/sources.list

# cd /usr/local/src
# wget http://www.dotdeb.org/dotdeb.gpg
# cat dotdeb.gpg | sudo apt-key add -
# apt-get update
# apt-get upgrade

Now before we proceed any further, let’s make sure we have improved vi, because we will be editing whole bunch of files and default Debian installation only comes with classic vi, which does not understand arrow keys in editor mode (and that personally bugs me a lot):

$ sudo apt-get install vim

Install Nginx and PHP 5.3 with PHP-FPM

$  sudo apt-get install nginx
$  sudo apt-get install php5-cli php5-common php5-suhosin
$  sudo apt-get install php5-fpm php5-cgi php5-apc

Let’s also install xdebug via pecl (we could install Xdebug through apt, too, but it misconfigures xdebug after install and it’s a good thing to have properly configured PECL on LAMP stack, anyway):

$ sudo apt-get install php-pear
$ sudo apt-get install build-essential
$ sudo apt-get install php5-dev
$ sudo pecl install xdebug

CAUTION: If you run into problems with installing php5-dev (e.g. an error complaining about libtools version, which is expected if you run these instructions on Debian newer than Lenny, e.g. Squeeze) you may need to download appropriate .deb packages manually from: http://mirrors.kernel.org/ubuntu/pool/main/p/php5/ and install with: “dpkg -i packagename.deb”. Please make sure to pick the correct .deb file for your CPU architecture and the version that you need.

Please note an output at the end of the xdebug installation which looks something like:

Build process completed successfully
Installing '/usr/lib/php5/20090626+lfs/xdebug.so'

You need the path after “installing” because we will have to create xdebug configuration file with that path. You php configuration files should be in “/etc/php5/conf.d/”. If that is true, please create /etc/php5/conf.d/xdebug.ini (or create xdebug.ini under wherever your php configuration files directory is) and insert following line there:

; Enable xdebug
zend_extension="/usr/lib/php5/20090626+lfs/xdebug.so"

noting that the path provided to zend_extension instruction is the same one we got from the xdebug pecl installation output.

Configure Nginx

Following instructions assume that you are configuring a domain: debian.vm

create /var/www/debian.vm/html and make it readable by Nginx user (which is probably: “www-data”)
create /var/www/debian.vm/logs and make it writable by Nginx user (which is probably: “www-data”)

Edit: /etc/nginx/sites-available/debian.vm.conf as root and enter following configuration:

server {
    listen   80;
    server_name  debian.vm;
    root /var/www/debian.vm/html/;
    access_log /var/www/debian.vm/logs/web.access.log;
    error_log /var/www/debian.vm/logs/web.error.log;

#  root location
    location / {
        index  index.html index.htm index.php;
    }

#  static content
    location ~* ^.+\.(wsf|xml|pdf|doc|jpg|jpeg|gif|css|png|js|ico|ttf|eot|otf|svg)$ {
      # access_log        off;
      expires           15d;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
         deny  all;
    }

#  php file handling
 location ~ \.php$ {
   fastcgi_pass   127.0.0.1:9000;
   fastcgi_index  index.php;

   include fastcgi_params;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   fastcgi_param SERVER_NAME $http_host;
   fastcgi_ignore_client_abort on;
 }

  gzip on;
  gzip_comp_level 2;
  gzip_proxied any;
  gzip_min_length  1000;
  gzip_disable     "MSIE [1-6]\."
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

}

Note: if you are setting up virtualhost for Drupal, your configuration needs to be more involved and may look something like the following: https://gist.github.com/1593835

Now let’s link this configuration from sites-enabled foder to enable it:

$ sudo ln -s /etc/nginx/sites-available/debian.vm.conf /etc/nginx/sites-enabled/debian.vm.conf
$ sudo mv /etc/nginx/sites-enabled/default /etc/nginx/sites-enabled/00_default

Check the resulting Nginx configuration with:

$ sudo /usr/sbin/nginx -t

Test Page

$ sudo mkdir -p /var/www/debian.vm/html
$ sudo chown -R $USER:$GROUP /var/www/debian.vm
$ sudo chmod -R 775 /var/www/debian.vm
$ echo "<?php phpinfo(); " > /var/www/debian.vm/html/index.php

$ sudo /etc/init.d/php5-fpm restart
$ sudo /etc/init.d/nginx restart

After which if the domain you configured is indeed debian.vm and it i pointing to your server (even via hosts file record), when visiting http://debian.vm/ via your browser you should see the PHP Info page generated by phpinfo() in your index.php. Please note that our nginx configuration will also cache static content for 15 days, improving performance.

Bonus: MySQL

Note: this assumes you have already set up dotdeb repositories properly (beginning of this post).

If you need to install MySQL for PHP, as well:

$ sudo apt-get install mysql-common
$ sudo apt-get install mysql-client-5.1
$ sudo apt-get install mysql-server-5.1
$ sudo apt-get install php5-mysql

Bonus: Changing hostname

If you want to permanently change the hostname of your server (so that it survives reboot) you need to follow following steps:

  • Let’s assume you are changing your server hostname to ika.ge
  • Edit /etc/hosts as a root and next to “localhost” add “ika.ge” so corresponding line reads something like:\

    127.0.0.1       localhost ika.ge
    
  • Edit “/etc/hostname” and replace existing line there with “ika.ge”
  • run:\

     sudo /etc/init.d/hostname.sh start