Installing nginx with PHP on Debian

By searching the web how to install nginx with PHP5 on Debian, one can find different tutorials, but many require a lot of manual work.

At first some background information: nginx (wikipedia) is a very fast webserver with efficient use of system resources. Because of debian’s security policy you can’t install the current up to date version of out of the box, therefore you have to compile it on your own. Or one can use the dotdeb service, which offers a slightly older version of nginx. Furthermore you can use some strange setup scripts if you trust them.

But if you do not need the most up to date version of nginx and don’t want to break your systems securety with different thirdparty stuff you can follow this short guide.

Install required packages

apt-get install php5-cgi php5-suhosin spawn-fcgi nginx

Start nginx afterwards:

/etc/init.d/nginx start

Type in your web server’s IP address and you should see some error-webpage. On my system the /var/www directory was missing, so i have to create it:

mkdir -p /var/www
chown www-data:www-data /var/www

For testing you can create a index.html file inside /var/www with some “Hello World” or something like that so.

Setting up php parsing

Edit the file /etc/rc.local and put the following line above the “exit 0”:

/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid

So on every reboot of you server it starts on localhost port 9000 the php fcgi parsing service. Also you should type this command in your console to run it now.

Configure nginx

Now change the config file /etc/nginx/sites-available/default to enable and configure the php settings:

location ~ \.php$ {
location ~ \..*/.*\.php$ {return 404;}
root /var/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

Make sure that there are some spaces between include and fastcgi_params in the default file this is written as one word which is a bug.

For testing you can create a /var/www/info.php with

<?php
phpinfo();
?>

and after a restart of your webserver you should see the php-infopage

/etc/init.d/nginx restart

As you see, PHP5 is working, and it’s working through FastCGI, as shown in the Server API line. If you scroll further down, you will see all modules that are already enabled in PHP5, also for security reasons we installed already Suhosin module.

http://wiki.nginx.org/Pitfalls
http://wiki.nginx.org/PHPFcgiExample
http://wiki.nginx.org/NginxFullExample

Author: admirableadmin

Hello World! Ich bin Andreas Peichert und entwickle und programmiere Software seit 2000. Zurzeit arbeite ich als Senior Solution Architect.

2 thoughts on “Installing nginx with PHP on Debian”

  1. Heya i am for the first time here. I came across
    this board and I find It truly useful & it helped me out much.
    I hope to give something back and help others like you aided me.

Leave a Reply

Your email address will not be published. Required fields are marked *