ReynierPM ReynierPM - 1 year ago 84
Linux Question

Nginx doesn't serve PHP files

I have a new fresh CentOS7 installed and running as a VM. I am playing with Nginx since I have being using Apache my whole life and now just for fun and learn I decide to switch to Nginx. I am following this two guides:

And as part of my previous research before I get out of ideas I did read this which is not helpful at all.

Before continue I should said that I took what I need for each of them because I want to use PHP 7.0.x instead of the default that comes with CentOS 7 repos (5.4 I think).

So, this is how my config files looks like:


server {
listen 80;
server_name centos7.localdomain;
root /var/www/html;
index index.php index.html index.htm;

location / {
try_files $uri $uri/ =404;

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;

location ~ \.php$ {
try_files $uri =404
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;


; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx


; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server.
; Default Values: user and group are set as the running user
; mode is set to 0660
listen.owner = nobody = nobody

other than those values you see in here are the default ones. The full file is shared here

I have created the file
with nothing else than:


As soon as I try the URL
(or without the
) the file is downloaded instead of display it content.

Of course after all those changes I have restarted
services and check them by runing
systemctl status nginx.service
systemctl status php-fpm.service

The permissions for
are as follow:

$ ls -la /var/www/html/
total 4
drwxr-xr-x. 2 root root 22 Oct 9 20:53 .
drwxr-xr-x. 3 root root 17 Oct 9 20:24 ..
-rw-r--r--. 1 root root 18 Oct 9 20:53 index.php

This is the PHP version I am running:

$ php -v
PHP 7.0.11 (cli) (built: Sep 14 2016 08:28:52) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.11, Copyright (c) 1999-2016, by Zend Technologies
with Xdebug v2.4.1, Copyright (c) 2002-2016, by Derick Rethans

I am missing something here? If so what is it? Or what is wrong on this setup that I am playing with?

Answer Source

Remove "try_files $uri =404" from the last segment. This may solve your problem.

location ~ \.php$ {
    fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download