ScaleScaleScaleScale

Tips / Nginx


PHP not working with Nginx on CentOS

One common trouble that I’ve seen a couple of times is PHP not working with Nginx. There are two ways to parse PHP if you use Nginx, as a reverse proxy and as a stand alone with php-fpm. Here are the details about how you can configure Nginx to have PHP working without issues:

1) Load php using mod_php from Apache. Not recommended but it’s up to you. See this article about how to setup a Nginx reverse proxy with Apache on the backend.

2) Load php from PHP-FPM backend, our recommendation.

Installing PHP-FPM in a few easy steps:

yum install php-fpm
chkconfig php-fpm

Edit php.ini file

nano -w /etc/php.ini

Find cgi.fix_pathinfo, uncomment and change it to

cgi.fix_pathinfo=0

Find listen = 127.0.0.1:9000 at /etc/php-fpm.d/www.conf and set it to:

listen = /tmp/php5-fpm.sock

Add this code to your virtual host configuration on Nginx:

        location ~ .php$ {
            root           /var/www/yoursite.com;
            try_files $uri =404;
            fastcgi_pass   unix:/tmp/php5-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 256 4k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;
        }

Replace /var/www/yoursite.com with your real web path and site name.

Example of full virtual host configuration:

### yoursite.com

server {
access_log off;
log_not_found off;
error_log  logs/error_log warn;
        listen       80;

        server_name  yoursite.com www.yoursite.com;

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

        location ~* .(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|flv|mp3|mid|js|css|wml|swf)$ {
                root   /var/www/yoursite.com;
                expires max;
                add_header Pragma public;
                add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }


# php-fpm parsing php files
        location ~ .php$ {
            root           /var/www/yoursite.com;
            try_files $uri =404;
            fastcgi_pass   unix:/tmp/php5-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 256 4k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;
        }


}

Restart php to apply the changes and reload nginx:

service php-fpm restart
service nginx reload

Now you should be able to parse PHP files with Nginx. If you don’t, please paste your errors in the comments field.

Popular search terms:

  • nginx not parsing php
  • nginx php not working
  • https://www scalescale com/tips/nginx/php-not-working-nginx/
  • nginx config php not working
profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking