Yin and Yang

CHARLES AUER (dot) NET

Installing Nginx and PHP 5.4 on Debian Squeeze

This tutorial has been updated and was tested on Debian Squeeze.

Installing

In order to get more up-to-date packages for a LAMP stack on Debian Squeeze, you can add the repository for getdeb.org. Detailed instructions can be found on their site, but basically all you need to do is add these lines to /etc/apt/sources.list

nano /etc/apt/sources.list

Add these lines to the bottom of the file

# dotdeb.org repo
deb http://packages.dotdeb.org squeeze all
deb-src http://packages.dotdeb.org squeeze all
# php 5.4 repo
deb http://packages.dotdeb.org squeeze-php54 all
deb-src http://packages.dotdeb.org squeeze-php54 all

Get the signing key and add it to the keystore.

wget -q http://www.dotdeb.org/dotdeb.gpg -O- | apt-key add -

Update the repository index.

apt-get update

Then install nginx and php5-fpm (FastCGI Process Manager).

apt-get install nginx php5-fpm

It will prompt you to confirm the install, so just hit enter.

Configuring

First off, you will need to decide where you want to store your website's root directory. I usually pick /var/www/example.com because I am used to the way Apache's DocumentRoot directive works, but you can choose any place on the file system. For this tutorials I chose to use /var/www/example.com, so create that directory.

mkdir -p /var/www/example.com

By default there is a default file in /etc/nginx/sites-enabled/. This is a link to /etc/nginx/sites-avalible/default, so it is fine to remove it because we are going to use a different configuration file.

rm /etc/nginx/sites-enabled/default

Now you will create a configuration file for your first site. Run this command, and replace example.com with whatever naming convention you use. I usually stick with the url of the site - e.g., example.com.

nano /etc/nginx/sites-enabled/example.com

Add this same configuration text to the new file.

  • Do not forget to replace ipaddress with the IP address of your server.
  • Change example.com to the domain of the site you are creating. There are two instances of example.com - one after server_name and one after root.
server {
        listen ipaddress:80;

        root /var/www/example.com;
        index index.html index.htm index.html;

        server_name example.com;

        location ~ \.html$ {
                fastcgi_split_path_info ^(.+\.html)(/.+)$;
                # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.html;
                include fastcgi_params;
        }
}

Save and close that file and start the nginx server.

service nginx start

After that is done, try to access the server via the address you configured above.

http://example.com

You should get a "403 Forbidden" message assuming you have not populated the folder you set as the root. This is normal as nginx does not allow directory listings by default.

If this is the only site you will be hosting on your server, you are now done. However, if you intend to host more than one site, you can copy the above server block to another file in /etc/nginx/sites-enabled/ and populate it with the server name, and root directory of the new site.

Restart nginx and you should be able to access the new site by the URL, assuming DNS is set up correctly and has had time to propagate.

service nginx restart

Do not forget to create the root directory for your new site, otherwise you will get a "not found" error and be left scratching your head.