Laravel 5 permission denied when writing in log file

前端 未结 4 673
無奈伤痛
無奈伤痛 2020-12-05 08:49

I have Ubuntu and Laravel 5 framework and I see the white screen in the browser.
When I change storage/logs directory permissions it helps, but I have to do

4条回答
  •  攒了一身酷
    2020-12-05 09:24

    Short answer:

    sudo chmod -R 777 vendor storage
    echo "umask 000" | sudo tee -a /etc/resolv.conf
    sudo service apache2 restart

    Extensive answer:

    When you launch Laravel 5 framework on Apache server with enabled by default 'daily' option for creating log files, sometimes you would face with forbiddance of writing into logfiles due the file permissions.
    Ususally, when you have php project all files belong to www-data user, and your current user has no need to write to logfiles.
    Regarding the Laravel, two different processes need to write to your logfiles:
    1) Apache server (user www-data) when you do something in your browser;
    2) Php process (your user) when you execute php artisan something in command line.

    Of course, you can execute sudo -u www-data php artisan your_command (like suggested here ) each time you want to use artisan, but it is a bit annoying.
    First of all you need to give permissions to vendor and storage directories for Apache user. Most easiest way (but not the best one) is to perform: sudo chmod -R 777 vendor storage

    Now, lets see what happens when logfile creates in both cases.

    If initially logfile storage/logs/laravel-2015-mm-dd.log was created through the error raised by php artisan something command (case 2), log file will have

    `-rw-rw-r-- your_user:your_user` 
    

    permissions.
    If it was created by your apache server (case 1), which ussually launch under www-data user, permissions will look like this:

    -rw-r-r-- www-data:www-data
    

    So, my suggestion is to change permissins for newly created files by apache.
    Let's add line umask 000 to /etc/resolv.conf file.

    echo "umask 000" | sudo tee -a /etc/resolv.conf
    

    Now,

    sudo service apache2 restart
    

    That's it.
    Be aware, this solution is applicable for development environment only, due the possible secutity risk.

提交回复
热议问题