IT Knowledge Base

Technical Repository

HowTo: Update Nginx Configuration to support PHP files

These are the default settings for Nginx. OpenĀ /usr/local/nginx/conf/nginx.conf to edit.

nano /usr/local/nginx/conf/nginx.conf
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }

Update the nginx.conf file to these settings

        location ~ \.php$ {
            root           /var/www;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

In order to use the $document_root directive, you will need to verify that your nginx configuration file has the root under the server block.

server {
  server_name www.mydomain.com;
  root /var/www/;
  location / {
    [...]
  }
  location /foo {
    [...]
  }
  location /bar {
    [...]
  }
}

Don’t forget to set the index file under the server configuration.

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

Also refer to this article to resolve the following error:
http://www.gabrielmatteson.com/index.php/howto-fix-unixvarrunphp5-fpm-sock-failed-13-permission-denied/

connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied)



Leave a Reply