Solving the Laravel storage folder access issue in Vagrant
I use Vagrant for almost all of my projects and a lot of my projects use Laravel. Now, Laravel is a decent and modern PHP framework that is quite fun to work with.
However, there is something that has always been a problem. Laravel makes use of a special folder named “storage” that stores dynamically created logs, cached views, etc.
Missing permissions
The storage folder is in your host OS. The PHP runs in the Vagrant VM. There is a permission mismatch. Meaning that you end up running a chmod on the storage folder in order to give the processes in the VM write access. Worse still, you end up having to redo the chmod command fairly often.
I finally decided to figure this problem out once and for all.
The permissions solution
The solution is actually fairly simple. Define the storage folder as a synced folder and declare the guest OS’s user and group to match the Web service.
In other words, add the following config setting to your Vagrantfile:
config.vm.synced_folder "www/storage", "/vagrant/www/storage", owner: "www-data", group: "adm"
In my case, the storage folder is under the www folder in the root of my project’s directory and Nginx & PHP-FPM processes end up using the www-data user and the adm group in the Ubuntu guest OS.
The result is that the storage folder is seen as having the assigned user and group within the guest OS. No more permission errors!
For more info on synced folders in Vagrant check out the synced folders basic usage info in the Vagrant docs.