Permission Denied - Laravel 5.2 - Vagrant

Published 7 months ago by afonte15

 local.ERROR: ErrorException: file_put_contents: failed to open stream: Permission denied in /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php:111

I have changed permissions. I tried

sudo chmod -R o+w storage/

and

php artisan cache:clear php artisan clear-compiled

sudo chmod -R 777 storage/ composer dump-autoload

This does not fix the issue. Any help would be appreciated.

My host machine is Ubuntu 16.04

Laravel 5.2

vmitchell85

Was the path to the error the same after running your chmod commands?

Maybe ensure bootstrap/cache also is 777

vapenation

please don't give bad advice such as 777 on storage... it's a security risk

afonte15

I also ran the command below on the bootstrap/cache

sudo chmod -R o+w bootstrap/cache

vmitchell85

@vapenation The recommended change was intended to be for testing, not for permanent use.

afonte15

I get the same errors after changing the permissions. Any help would be appreciated! I have been stuck on this for weeks now.

vmitchell85

If you create a brand new laravel site does the new site work?

Also are you running those commands in your host Ubuntu or the vagrant box?

afonte15

At this point I ran the permission change on both the host and vagrant box.

What is the correct way to run the permission change commands? On my host machine which is also Ubuntu or do I change the permissions in vagrant once I SSH into it?

When I created my laravel project, when I run php artisan laravel new blog I ran that command in vagrant. Should I have ran that command on my host machine?

Is there a way to avoid this issue in the future?

vmitchell85

I'm a little rusty on Homestead as I've been using Valet for a while now, but if I remember correctly I would normally run my laravel new while SSHd into the Homestead Vagrant box.

afonte15

Yes, that is how I have always done it as well.

The error refers to a line in the filesystem.php file. Line 111

public function put($path, $contents, $lock = false)
    {
        return file_put_contents($path, $contents, $lock ? LOCK_EX : 0); //line 111
    }
vmitchell85

Did you run php artisan key:generate to populate the APP_KEY in your .env file? Found a similar post that states that may be the issue.

afonte15

Yes. I have run it a few times at this point. Hoping that would resolve the issue. Do you know if the permission changes should be done on the host machine or the vagrant machine?

vmitchell85

The permissions changes should always be on the Vagrant machine, since that is where the app is running.

Is there any additional info in the error message?

afonte15

It seems like the webserver doesn't have permission to write to storage...what is the webserver needs write access. Do you know how I can change permission for my webserver to have access?

vmitchell85

Running chmod 777 on any directory gives the directory owner, group, and everyone read/write/execute access... this means if you've ran it with 777 then everyone has read/write/execute access.

So, even though it says "permission denied" it might be something else contributing to it.

If you run ls -l in your main application directory does it show drwxrwxrwx for your storage and bootstrap directories?

afonte15

but it still only shows Vagrant...my webserver needs access though

[email protected]:~/sites/inventory$ ls -l total 204

drwxrwxrwx 1 vagrant vagrant   4096 Apr 13 14:17 app
-rw-rw-r-- 1 vagrant vagrant   1646 Jul 27  2016 artisan
drwxrwxrwx 1 vagrant vagrant   4096 Jul 27  2016 bootstrap
-rw-rw-r-- 1 vagrant vagrant   1386 Nov 30 22:07 composer.json
-rw-rw-r-- 1 vagrant vagrant 116748 Mar 21 16:55 composer.lock
drwxrwxr-x 1 vagrant vagrant   4096 Apr 18 15:34 config
drwxrwxr-x 1 vagrant vagrant   4096 Jul 27  2016 database
-rw-rw-r-- 1 vagrant vagrant    658 Aug 12  2016 gulpfile.js
drwxrwxr-x 1 vagrant vagrant  24576 Jul 27  2016 node_modules
-rw-rw-r-- 1 vagrant vagrant    212 Jul 27  2016 package.json
-rw-rw-r-- 1 vagrant vagrant   1026 Jul 27  2016 phpunit.xml
drwxrwxr-x 1 vagrant vagrant   4096 Aug  9  2016 public
-rw-rw-r-- 1 vagrant vagrant   1918 Jul 27  2016 readme.md
drwxrwxr-x 1 vagrant vagrant   4096 Jul 27  2016 resources
-rw-rw-r-- 1 vagrant vagrant    567 Jul 27  2016 server.php
drwxrwxrwx 1 vagrant vagrant   4096 Jul 27  2016 storage
drwxrwxr-x 1 vagrant vagrant   4096 Jul 27  2016 tests
drwxrwxrwx 1 vagrant vagrant   4096 Mar 21 16:55 vendor

I found this response of stackoverflow.....Do you think this can resolve the issue?

Just to state the obvious for anyone viewing this discussion.... if you give any of your folders 777 permissions, you are allowing ANYONE to read, write and execute any file in that directory.... what this means is you have given ANYONE (any hacker or malicious person in the entire world) permission to upload ANY file, virus or any other file, and THEN execute that file...

IF YOU ARE SETTING YOUR FOLDER PERMISSIONS TO 777 YOU HAVE OPENED YOUR SERVER TO ANYONE THAT CAN FIND THAT DIRECTORY. Clear enough??? :)

There are basically two ways to setup your ownership and permissions. Either you give yourself ownership or you make the webserver the owner of all files.

Webserver as owner (the way most people do it):

assuming www-data is your webserver user.

sudo chown -R www-data:www-data /path/to/your/root/directory

if you do that, the webserver owns all the files, and is also the group, and you will have some problems uploading files or working with files via FTP, because your FTP client will be logged in as you, not your webserver, so add your user to the webserver user group:

sudo usermod -a -G www-data ubuntu

Of course, this assumes your webserver is running as www-data (the Homestead default), and your user is ubuntu (it's vagrant if you are using Homestead.

Then you set all your directories to 755 and your files to 644... SET file permissions

sudo find /path/to/your/root/directory -type f -exec chmod 644 {} ;

SET directory permissions

sudo find /path/to/your/root/directory -type d -exec chmod 755 {} ;

Your user as owner

I prefer to own all the directories and files (it makes working with everything much easier), so I do:

sudo chown -R my-user:www-data /path/to/your/root/directory

Then I give both myself and the webserver permissions:

sudo find /path/to/your/root/directory -type f -exec chmod 664 {} ; sudo find /path/to/your/root/directory -type d -exec chmod 775 {} ;

Then give the webserver the rights to read and write to storage and cache

Whichever way you set it up, then you need to give read and write permissions to the webserver for storage, cache and any other directories the webserver needs to upload or write too (depending on your situation), so run the commands from bashy above :

sudo chgrp -R www-data storage bootstrap/cache sudo chmod -R ug+rwx storage bootstrap/cache

Now, you're secure and your website works, AND you can work with the files fairly easily

Please sign in or create an account to participate in this conversation.