When developing Laravel applications on Linux-based systems, you may run into file permission issues with auto-generated files such as logs or cache files. I ran into this in the past often and my quick workaround was to manually change permissions and owner for the files with incorrect permissions. It would be something like following:
chmod ug+w -R storage/logs
chown username.www-data -R storage/logs
However, I started running into this issue more often since Laravel logs are now being generated with dates like laravel-2019-07-29.log instead of single file, laravel.log. So I decided to take a closer look and see what I need to do to fix this permanently so I don’t have to manually update permissions anymore.
Upon quick researching, I realized that when these logs or other files are automatically created by web server process – apache in my case – the files are owned by www-data for user and group and set with rw-r-r permission, meaning that only www-data can write to these files. This works fine until you run some artisan commands yourself and need to write to files.
The solution is to tell apache process to set files it create writable by other users. You do this by adding following to your apache environment file:
On Ubuntu, the apache environment file is located at /etc/apache2/envvars. Just edit that file and add above line at the end of file. Next time apache creates Laravel log or other files, it will create them with rw-rw-rw permission so you and the apache process can both write to log files. Of course, I would do this only on dev environment, not on production servers.
If you edit the environment file, don’t forget to restart the apache process.
Hope this will help other Laravel developers who constantly run into permission issues.