Nginx – Getting PHP to work with nginx

nginxPHPphp5

I am trying to get nginx to run with PHP on my ubuntu staging server. Nginx by itself is working like a charm with static *.html-files, but any PHP file called by the browser outputs the code as plain text, which undoubtedly means that PHP isnt working properly. I am calling http://192.168.1.100/test.php which outputs the content of test.php inside the /var/www folder.

I've tried various solutions, but followed this guide as the main way of getting nginx to work, which it currently isn't with PHP files as mentioned.

My /etc/nginx/sites-enabled/default looks as below:

server {

        listen   80; ## listen for ipv4
        listen   [::]:80 default ipv6only=on; ## listen for ipv6
        server_name  localhost;

        access_log  /var/log/nginx/localhost.access.log;

        location / {
                root   /var/www;
                index  index.html index.htm index.php;
                allow all;
        }

        location ~ .php$ {
          fastcgi_pass   127.0.0.1:9000;  # By all means use a different server$
          fastcgi_index  index.php;

          fastcgi_param  SCRIPT_FILENAME  /var/www$fastcgi_script_name;   # !! $
          fastcgi_param  QUERY_STRING     $query_string;
          fastcgi_param  REQUEST_METHOD   $request_method;
          fastcgi_param  CONTENT_TYPE     $content_type;
          fastcgi_param  CONTENT_LENGTH   $content_length;
        }


}

Running netstat -plan | grep :9000 outputs tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 1352/php5-cgi so apparently PHP is running.

What am I doing wrong and what should be changed to get it working?

Best Answer

I have no idea on what went wrong. I reset the VPS to the fresh install image, tried a completely different tutorial, http://www.howtoforge.com/installing-nginx-with-php5-and-php-fpm-and-mysql-support-on-ubuntu-10.10-p2, which worked perfectly!