For another couple of hours I was struggling with permissions on my Vagrant/Puphpet virtual machine. After installation of fresh laravel instance I tried to run some migrations but got the error:
PHP Fatal error: Uncaught exception 'UnexpectedValueException' with message 'The stream or file "/var/www/laracast/storage/logs/laravel-2015-04-25.log" could not be opened: failed to open stream: Permission denied' in /var/www/laracast/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:84
Simple chmod -R didn’t do the magic and permissions stayed untouched. After some googling I found that NFS shared folder type doesn’t allow chmod on the fly. Then I found this option which should fix the problem:
config.vm.synced_folder "#{folder['source']}", "#{folder['target']}", id: "#{folder['id']}", type: nfs, :mount_options => ["dmode=777","fmode=666"]
But when running up the VM vagrant printed me error:
mount.nfs: an incorrect mount option was specified
Googled again and found than NFS is not supporting options like dmode and fmode. Oh my zhh, what is wrong with you, NFS?
After another hour of searching found nothing yet I finally decided to switch to rsync or smth more friendly and went to puphpet site to find out how. But by accident came across a note about NFS
Make sure to install the vagrant-bindfs plugin with $ vagrant plugin install vagrant-bindfs.
I already had it installed and deiced to check what bindfs is. The documentation said:
This allow you to change owner, group and permissions on files and, for example, work around NFS share permissions issues.
What? The solution was under my very nose all that time. So I looked through to bindfs examples and found what I really needed:
config.bindfs.bind_folder "/mnt/vagrant-#{i}", "#{folder['target']}", user: sync_owner, group: sync_group, perms: "u=rwx:g=rwx:o=r"
I just added , perms: "u=rwx:g=rwx:o=r" to existing record and voila - everything worked just find.
Summary: In order to change permissions on NFS shared folders under Vagrant VM add desired permissions to config.bindfs.bind_folder config.
PS: While searching for solution found some pleasant article on vagrant’s documentation site. Every time I ran vagrant box I had to type my pass in order to mount shared folders. This article showed how to avoid this step:
which will open sudoer file. Now add this entry to it
Cmnd_Alias VAGRANT_EXPORTS_ADD = /usr/bin/tee -a /etc/exports
Cmnd_Alias VAGRANT_NFSD = /sbin/nfsd restart
Cmnd_Alias VAGRANT_EXPORTS_REMOVE = /usr/bin/sed -E -e /*/ d -ibak /etc/exports
%admin ALL=(root) NOPASSWD: VAGRANT_EXPORTS_ADD, VAGRANT_NFSD, VAGRANT_EXPORTS_REMOVE
No more password required. Magic