Wednesday, February 2, 2022

[SOLVED] Nginx Centos7 Laravel 5 can't write to log files

Issue

I got a brand new Centos 7 server and would like to install laravel + nginx

I added Remi repo for PHP5.6 Also added epel

yum update
yum install nmap rsync nano wget curl
yum install epel-release
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm 
nano /etc/yum.repos.d/remi.repo

yum install nginx
systemctl start nginx
systemctl enable nginx

sudo firewall-cmd --permanent --zone=public --add-service=http 
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

#PHP
yum install php php-mysql php-fpm php-dom php-mcrypt php-mbstring
nano /etc/php.ini
cgi.fix_pathinfo=0
nano /etc/php-php.d/www.conf 
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nobody
listen.group = nobody
user = nginx
group = nginx

systemctl start php-fpm
systemctl enable php-fpm

The NGINX server is working, and phpinfo(); is looking good too.

inside /etc/nginx/nginx.conf:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        #root         /usr/share/nginx/html;
        root /usr/share/nginx/html/lv/public;
        index index.php index.html index.htm

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
                try_files $uri /index.php =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;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

Inside /usr/share/nginx/html/lv I have installed alaravel 5 project via git.

[root@213 lv]# ls -la
total 164
drwxr-xr-x. 11 nginx nginx   4096 Jan 28 02:19 .
drwxr-xr-x.  3 root  root    4096 Jan 28 02:17 ..
drwxrwxr-x. 15 nginx nginx   4096 Oct 21 10:44 app
-rw-rw-r--.  1 nginx nginx   1635 Aug 19 08:57 artisan
drwxrwxr-x.  2 nginx nginx     39 Nov 28 01:22 bootstrap
-rw-rw-r--.  1 nginx nginx   1007 Aug 19 08:57 composer.json
-rw-rw-r--.  1 nginx nginx 107281 Aug 19 08:57 composer.lock
drwxrwxr-x.  2 nginx nginx   4096 Aug 19 08:57 config
drwxrwxr-x.  4 nginx nginx     52 Aug 19 08:57 database
-rw-rw-r--.  1 nginx nginx    503 Aug 19 08:57 gulpfile.js
-rw-rw-r--.  1 nginx nginx     79 Aug 19 08:57 package.json
-rw-rw-r--.  1 nginx nginx     87 Aug 19 08:57 phpspec.yml
-rw-rw-r--.  1 nginx nginx    729 Aug 19 08:57 phpunit.xml
drwxrwxr-x.  4 nginx nginx   4096 Nov 28 01:03 public
-rw-rw-r--.  1 nginx nginx   1724 Aug 19 08:57 readme.md
drwxrwxr-x.  5 nginx nginx     42 Aug 19 08:57 resources
-rw-rw-r--.  1 nginx nginx    561 Aug 19 08:57 server.php
drwxrwxrwx.  6 nginx nginx     76 Aug 19 08:57 storage
drwxrwxr-x.  2 nginx nginx     47 Aug 19 08:57 tests
drwxrwxr-x. 29 nginx nginx   4096 Aug 19 08:57 vendor
[root@213 lv]# cd storage
[root@213 storage]# ls -la
total 12
drwxrwxr-x.  6 nginx nginx   76 Aug 19 08:57 .
drwxr-xr-x. 11 nginx nginx 4096 Jan 28 02:19 ..
drwxrwxr-x.  2 nginx nginx   23 Aug 19 08:57 app
drwxrwxr-x.  3 nginx nginx   21 Aug 19 08:57 documents
drwxrwxr-x.  5 nginx nginx   62 Aug 19 08:57 framework
-rwxrwxr-x.  1 nginx nginx   11 Aug 19 08:57 .gitignore
drwxrwxrwx.  2 nginx nginx 4096 Jan 27 14:01 logs

logs is empty.

I still get the following the error:

PHP message: PHP Fatal error:  Uncaught exception 'UnexpectedValueException' with message 'The stream or file "/usr/share/nginx/html/lv/storage/logs/laravel-2016-01-28.log" could not be opened: failed to open stream: Permission denied' in /usr/share/nginx/html/lv/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:95
Stack trace:
#0 /usr/share/nginx/html/lv/vendor/monolog/monolog/src/Monolog/Handler/RotatingFileHandler.php(88): Monolog\Handler\StreamHandler->write(Array)
#1 /usr/share/nginx/html/lv/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(37): Monolog\Handler\RotatingFileHandler->write(Array)
#2 /usr/share/nginx/html/lv/vendor/monolog/monolog/src/Monolog/Logger.php(269): Monolog\Handler\AbstractProcessingHandler->handle(Array)
#3 /usr/share/nginx/html/lv/vendor/monolog/monolog/src/Monolog/Logger.php(545): Monolog\Logger->addRecord(400, 'exception 'Symf...', Array)
#4 /usr/share/nginx/html/lv/vendor/larav

So I ran whoami.php which has `echo shell_exec('whoami');' in it, and the user is nginx.

So I don't get the problem


Solution

I found the problem

SELinux was turned on:

nano /etc/sysconfig/selinux
set to disabled


Answered By - Mazzy
Answer Checked By - Marilyn (WPSolving Volunteer)