1

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:

/etc/nginx/conf.d/default.conf

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;
    }
}

/etc/php-fpm.d/www.conf

[www]
; 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
listen.group = nobody

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

I have created the file /var/www/html/index.php with nothing else than:

<?php
    phpinfo();

As soon as I try the URL http://centos7.localdomain/index.php (or without the index.php) the file is downloaded instead of display it content.

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

The permissions for /var/www/html 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?

Community
  • 1
  • 1
ReynierPM
  • 17,594
  • 53
  • 193
  • 363
  • Can you post the output of the following command: php -v – Hackerman Oct 10 '16 at 01:25
  • @Hackerman added to the OP :) – ReynierPM Oct 10 '16 at 01:26
  • Is there a `/var/run/php-fpm/php-fpm.sock` in the filesystem? – zerkms Oct 10 '16 at 01:37
  • @zerkms yes, `ls -la /var/run/php-fpm/ total 4 drwxr-xr-x. 2 root root 80 Oct 9 20:53 . drwxr-xr-x. 24 root root 720 Oct 9 20:57 .. -rw-r--r--. 1 root root 5 Oct 9 20:53 php-fpm.pid srw-rw----. 1 nobody nobody 0 Oct 9 20:53 php-fpm.sock` (sadly comments in SO should allow markdown formatted text as well but it doesn't) – ReynierPM Oct 10 '16 at 01:39
  • @ReynierPM so it belongs to `nobody` and there is no `rw` for others. I don't see how it would be accessible by nginx user. – zerkms Oct 10 '16 at 01:46
  • @zerkms Even if your answer makes sense I am newbie so I should change this to `root`? it's recommended? or what's the fix? (if that's the issue) – ReynierPM Oct 10 '16 at 01:51
  • @ReynierPM well, I would say that if nginx is the only client of the php-fpm, you may change both `listen.owner` and `listen.group` to the nginx user name – zerkms Oct 10 '16 at 01:52
  • @zerkms that's not the issue, I have change them, restart both services and same result. What it's odd for me is in the guide the author said to put those values as `nobody` instead of the user itself. Maybe they're referencing Digital Ocean Droplets though they didn't mention anything about it – ReynierPM Oct 10 '16 at 02:04
  • Well, it may be not the only issue, but it definitely is an issue. If you don't have access to a file socket - you cannot read/write from it. – zerkms Oct 10 '16 at 02:04
  • Would it make any difference if you run this nginx config: http://pastebin.com/dCf4AdEV ? Are you sure the response was not cached by browser? – zerkms Oct 10 '16 at 02:07
  • @zerkms that setup works but I need to append the `index.php` at the end which is annoying – ReynierPM Oct 10 '16 at 02:12
  • 1
    So, now you have a working config. Spend some time with nginx documentation and modify it to satisfy your requirements. Put the `index` directive back. – zerkms Oct 10 '16 at 02:14
  • @zerkms great, thank you – ReynierPM Oct 10 '16 at 02:17

1 Answers1

1

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;
}
limen
  • 67
  • 7