Installing nginx and php-fpm on debian

February 18, 2015

Since I started developing in php, I always assumed apache was the best solution as a webserver. But I was always curious to see how some shared hosting offerd the client to choose the php version. Recently I gave this some research and stumbled upon nginx. So I decided to give it a try.

I started off with a clean debian squeeze installation and started installing nginx.

$ apt-get install nginx

I started playing around with the configuration and noticed some resembance to apache. Namely the sites-available and site-enabled.

Here’s my catch-all config.

server {
        listen   80; ## listen for ipv4
        listen   [::]:80 default ipv6only=on; ## listen for ipv6

        server_name  _; #this is an invalid name so it doesn't match any vhosts.

        access_log  /var/log/nginx/catch-all.access.log;

        location / {
                root   /var/www/catch-all;
                index  index.html index.htm;
        }
}

Make a symlink to sites-enabled and reload the service.

By now it’s time to install some php. But as I am going to install this on a different server. This way I can loadbalance the php service over more servers. So I start again from an empty debian squeeze server. Add these lines to /etc/apt/sources.list

deb http://packages.dotdeb.org stable all
deb-src http://packages.dotdeb.org stable all

Then update apt, and fetch the appropriate GnuPG key for the repo:

$ apt-get update
$ wget http://www.dotdeb.org/dotdeb.gpg
$ cat dotdeb.gpg | apt-key add -

If you would like a php5.4 server, change the sources.list entries to

deb http://packages.dotdeb.org squeeze-php54 all
deb-src http://packages.dotdeb.org squeeze-php54 all

Now install php-fpm

$ apt-get install php5-cli php5-suhosin php5-fpm php5-cgi php5-mysql

Check if php-fpm is running. If not start it.

$ netstat -plunt|grep php
tcp        0      0 127.0.0.1:9090          0.0.0.0:*               LISTEN      14834/php-fpm.conf

$ /etc/init.d/php-fpm start

As you can see, my php-fpm is still listening on 127.0.0.1, this can be changed in the php.ini file.

On the nginx server. Configure the .php handling.

location ~ \.php$ {
      fastcgi_pass 127.0.0.1:9090;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
      include fastcgi_params;
}

And restart nginx

$ /etc/init.d/nginx restart

comments powered by Disqus