Laravel 5 permission denied when writing in log fi

2019-01-14 05:05发布

问题:

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 it every day, due the 'daily' log configuration.

回答1:

The permissions for the storage and vendor folders should stay at 775, for obvious security reasons.

However, both your computer and your server Apache need to be able to write in these folders. Ex: when you run commands like php artisan, your computer needs to write in the logs file in storage.

All you need to do is to give ownership of the folders to Apache :

sudo chown www-data:www-data /path/to/your/project/vendor
sudo chown www-data:www-data /path/to/your/project/storage

Then you need to add your computer (referenced by it's username) to the group to which the server Apache belongs. Like so :

sudo usermod -a -G www-data userName

Most frequently, groupName is www-data but you might want to replace it with your correct group.



回答2:

  • chmod 777 is in general a security risk extremely risky.
  • chmod 775 for the storage folder is fine considering user also
    belongs to web server group.

  • with -R its extremely risky since for files an execute permissions is not at all required.

  • chmod 664 for files inside storage. chmod 775 for folders inside



回答3:

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.