slim 3 php application not working on CentOS nginx

2019-03-06 00:18发布

I have been struggling lately on installing latest nginx 1.14, php 7.2.5 and mariaDB 10.3.7 on CentOS 7 on virtualbox on macOS.

Finally php is working, I have already tested php_info(), index.php, testDBconnection.php successfully and even redirecting to a /public folder.

However, when copying my Slim 3 application to folder, I get this error:

access denied

I access the log via # tail /var/log/nginx/error.log and the output is the following:

PHP message: PHP Warning:  session_start(): Failed to read session data: files (path: /var/opt/remi/php72/lib/php/session) in /usr/share/nginx/html/slim3.local/bootstrap/app.php on line 5" while reading response header from upstream, client: 192.168.1.71, server: slim3.local, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "slim3.local"
2018/06/23 01:27:40 [error] 2514#2514: *5 FastCGI sent in stderr: "PHP message: PHP Warning:  session_start(): open(/var/opt/remi/php72/lib/php/session/sess_1ncvjg5us9384bs0m6vo0m46k7, O_RDWR) failed: Permission denied (13) in /usr/share/nginx/html/slim3.local/bootstrap/app.php on line 5
PHP message: PHP Warning:  session_start(): Failed to read session data: files (path: /var/opt/remi/php72/lib/php/session) in /usr/share/nginx/html/slim3.local/bootstrap/app.php on line 5" while reading response header from upstream, client: 192.168.1.71, server: slim3.local, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "slim3.local"

Line 5 of app.php is: session_start();

Moreover, it looks weird to see the ip address http://192.168.1.71/ when it is actually http://192.168.1.76/

I have changed permissions in /var/opt/remi/php72/lib/php to folders session, opcache, & wsdlcache by using the following command:

# chown nginx:nginx /var/opt/remi/php72/lib/php/session

I have restarted all:

# systemctl restart php72-php-fpm # systemctl restart nginx

Also I have tried:

# grep session.save_path  /etc/opt/remi/php72/php.ini
;     session.save_path = "N;/path"
;     session.save_path = "N;MODE;/path"
;session.save_path = "/tmp"
;       (see session.save_path above), then garbage collection does *not*

I reload the page "http://slim3.local" and I still get the same access denied error!

:(

# ls -la 

returns

# ls -lah /var/opt/remi/php72/lib/php/
total 0
drwx------. 5 nginx nginx 53 jun 19 19:51 .
drwxr-xr-x. 7 root  root  71 jun 19 19:51 ..
drwx------. 3 nginx nginx 46 jun 25 19:02 opcache
drwxrwx---+ 2 nginx nginx 84 jun 25 19:02 session
drwx------. 2 nginx nginx  6 may 23 01:59 wsdlcache

the www.conf file

; Start a new pool named 'www'.
; the variable $pool can we used in any directive and will be replaced by the
; pool name ('www' here)
[www]
...
; RPM: apache user chosen to provide access to the same directories as httpd
;user = apache
user = nginx
; RPM: Keep a group allowed to write in log dir.
;group = apache
group = nginx
...
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = 127.0.0.1:9000

; Set listen(2) backlog.
; Default Value: 511
;listen.backlog = 511

; 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
;listen.mode = 0660
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
...
; See warning about choosing the location of these directories on your system
; at http://php.net/session.save-path
php_value[session.save_handler] = files
php_value[session.save_path]    = /var/opt/remi/php72/lib/php/session
php_value[soap.wsdl_cache_dir]  = /var/opt/remi/php72/lib/php/wsdlcache
php_value[opcache.file_cache]  = /var/opt/remi/php72/lib/php/opcache

In the php.ini file i got:

...
cgi.fix_pathinfo=1
...
session.save_path = "/var/opt/remi/php72/lib/php/session"
...

nginx default.conf file:

server {
    listen   80;
    server_name  localhost;
    root /usr/share/nginx/html;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        #root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
 # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        try_files      $uri = 404;
        include        /etc/nginx/fastcgi_params;
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
}

my nginx virtual.conf

server {
        server_name slim3.local;
        root /usr/share/nginx/html/slim3nc.local/public;#without this line, it throws NOT FOUND

        location / {
             index index.html index.htm index.php;
        }

    location ~ \.php$ {
             try_files      $uri = 404;
             include        /etc/nginx/fastcgi_params;
             root           /usr/share/nginx/html/slim3nc.local/public;#Esta carpeta influye más
             fastcgi_pass   127.0.0.1:9000;
             fastcgi_index  index.php;
             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
             #include /etc/nginx/fastcgi_params;
             #fastcgi_pass 127.0.0.1:9000;
             #fastcgi_index index.php;
             #fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/slim3nc.dev$fastcgi_script_name;
        }
}

and the command

# lsattr /var/opt/remi/php72/lib/php/session

returns

---------------- /var/opt/remi/php72/lib/php/session/sess_1ncvjg5us9384bs0m6vo0m46k7
---------------- /var/opt/remi/php72/lib/php/session/sess_923979fqgr7r807majmn114vuk

Testing sestatus

# sestatus

It returns

SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31

And if I apply

# setenforce 0

it returns

# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   permissive
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31

Moreover, I have tried to change the setup in path for cache right on Slim3 container settings and nothing works :(

$container['view'] = function ($container) {
    $view = new \Slim\Views\Twig(__DIR__ . '/../resources/views', [
        'cache' => false,/*__DIR__ .'/../cache/views', *//* todo 'path/to/cache' on production, we need to set up a directory to cache the views*/
        'debug' => false, /*todo Turn this off in production*/
    ]);

    ...

    return $view;
};

I still get the message in a blank page

access denied

This slim 3 web app I got does not work in any server: centOS 7 production, laravel homestead, ubuntu 16, ... it simply doesn't work!!! What am I missing?

How do I fix this??

-> Meanwhile I am using Laravel ...

0条回答
登录 后发表回答