I have a script that I run using php artisan (with root user), and sometimes it causes the daily log file to be created before the apache www-data user does - which means that when a real user uses my web application, I get the folder permission error:
Failed to open stream: Permission denied
I change the permissions back to www-data everytime but I want to solve this by having the log file always created with the correct permissions.
I've considered creating a cron job that creates the file or touches it to make sure it has the right permission everyday, but I'm looking for a better solution that doesn't rely on another script.
We've also considered wrapping php artisan in another script to make sure that it is always run with the www-data credentials, but somethings that we want to do are actually root procedures that apache should not be allowed to do.
Any more suggestions?
The best way I found is that fideloper suggest, http://fideloper.com/laravel-log-file-name, you can set laravel log configuration without touch Log class. Have differents names for Console programs and Http programs, I think, is the best solution.
Add something like the following to the start of your
app/start/artisan.php
file (this is with Laravel 4):Adjust the path if the daily log file you mention is not the standard Laravel log file. You also may not want to change the group or set the permissions as I am doing here. The above sets the group to
www-data
and sets group write permissions. I've then added my regular user to thewww-data
group so that running artisan commands as my regular user can still write to the log.A related tweak is to put the following at the start of your
app/start/global.php
file:If you do this the
chmod
line above becomes moot. With the umask set to this, any new files PHP (and therefore Laravel) makes will have their permissions masked only so that "other" users won't have write permissions. This means directories will start asrwxrwxr-x
and files asrw-rw-r--
. So ifwww-data
is running PHP, any cache and log files it makes will be writeable by default by anyone in that user's main group, which iswww-data
.I had this worked very simple way:
I ran into the same problem on Laravel 5.6
In
config/logging.php
I just updated daily channel's path value withphp_sapi_name()
in it.This creates seperate durectory for different php_sapi_name and puts log file with the time stamp into their perticular directory.
So for me,
fpm-fcgi
directory: Logs from website,owner: www-data
cli
directory: from the artisan command(cronjob).owner: root
More info on Laravel 5.6 logging: https://laravel.com/docs/5.6/logging
Here is my
config/logging.php
file:Laravel 5.1
In our case we wanted to create log files so that the all the processes and users in
deploy
group had read/write permissions - so we needed new created files with permission 0664. Default for new log files is 0644. So this was our solution.Also we added a formatter to add newlines and a more readable log
Also it's possible to combine this with the accepted answer
One non-Laravel way to make this work is to simply executure your cronjob as www-data.
eg https://askubuntu.com/questions/189189/how-to-run-crontab-as-userwww-data
Laravel 5.5
Add this code to
bootstrap/app.php
:laravel-2018-01-27-cli-raph.log
andlaravel-2018-01-27-fpm-cgi-raph.log
which is more readable.Laravel 5.6
You have to create a class for your logger:
Then, you have to register it in
config/logging.php
:Same behavior as for 5.5:
laravel-2018-01-27-cli-raph.log
andlaravel-2018-01-27-fpm-cgi-raph.log
which is more readable.