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 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/

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_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


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.

About admirableadmin

Hello World! I am Andreas Peichert develop and program software since 2000. Currently I am working as an Open Source Software Engineer.

2 Responses to “Installing nginx with PHP on Debian”

Leave a Reply

Time limit is exhausted. Please reload CAPTCHA.