Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

afonte15's avatar

Permission Denied - Laravel 5.2 - Vagrant

 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

0 likes
28 replies
vmitchell85's avatar

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

Maybe ensure bootstrap/cache also is 777

lmxdev's avatar

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

afonte15's avatar

I also ran the command below on the bootstrap/cache

sudo chmod -R o+w bootstrap/cache

afonte15's avatar

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

vmitchell85's avatar

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's avatar

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's avatar

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's avatar

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's avatar

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's avatar

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's avatar

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's avatar

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's avatar

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's avatar

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

vagrant@homestead:~/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

afonte15's avatar

When I originally got the error I tried changing the permissions to the storage and boostrap/cache folder

vmitchell85's avatar

The stack overflow thing you posted is irrelevant. You're developing on a dev machine so you generally don't need to work about permissions right now. When you deploy your code online then you'll need correct permissions. Having 777 right now is fine.

I want to know exactly what you are doing to get the error. Are you running a command on the vagrant box? Are you trying to load a url in the browser? What exactly are you doing to make the error show up?

afonte15's avatar

I am at the login page and I provide the credentials to login and it just redirects back to the login page. Password is correct and then I check my laravel.log file and I get that permission error.

afonte15's avatar

[2017-04-19 01:31:38] local.ERROR: ErrorException: file_put_contents(/home/vagrant/sites/inventory/storage/framework/sessions/ff658e4153dbe2a47c29e631e8d583919f9cd549): failed to open stream: Permission denied in /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php:111
Stack trace:
#0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'file_put_conten...', '/home/vagrant/s...', 111, Array)
#1 /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php(111): file_put_contents('/home/vagrant/s...', 'a:4:{s:6:"_toke...', 2)
#2 /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Session/FileSessionHandler.php(83): Illuminate\Filesystem\Filesystem->put('/home/vagrant/s...', 'a:4:{s:6:"_toke...', true)
#3 /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Session/Store.php(262): Illuminate\Session\FileSessionHandler->write('ff658e4153dbe2a...', 'a:4:{s:6:"_toke...')
#4 /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(88): Illuminate\Session\Store->save()
#5 /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(155): Illuminate\Session\Middleware\StartSession->terminate(Object(Illuminate\Http\Request), Object(Illuminate\Http\RedirectResponse))
#6 /home/vagrant/sites/inventory/public/index.php(48): Illuminate\Foundation\Http\Kernel->terminate(Object(Illuminate\Http\Request), Object(Illuminate\Http\RedirectResponse))
#7 {main}  
[2017-04-19 01:31:38] local.ERROR: ErrorException: file_put_contents(/home/vagrant/sites/inventory/storage/framework/sessions/21c72020b901982cd900a79aef6151ea0cd9ed72): failed to open stream: Permission denied in /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php:111
Stack trace:
#0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'file_put_conten...', '/home/vagrant/s...', 111, Array)
#1 /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php(111): file_put_contents('/home/vagrant/s...', 'a:5:{s:6:"_toke...', 2)
#2 /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Session/FileSessionHandler.php(83): Illuminate\Filesystem\Filesystem->put('/home/vagrant/s...', 'a:5:{s:6:"_toke...', true)
#3 /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Session/Store.php(262): Illuminate\Session\FileSessionHandler->write('21c72020b901982...', 'a:5:{s:6:"_toke...')
#4 /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(88): Illuminate\Session\Store->save()
#5 /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(155): Illuminate\Session\Middleware\StartSession->terminate(Object(Illuminate\Http\Request), Object(Illuminate\Http\RedirectResponse))
#6 /home/vagrant/sites/inventory/public/index.php(48): Illuminate\Foundation\Http\Kernel->terminate(Object(Illuminate\Http\Request), Object(Illuminate\Http\RedirectResponse))
#7 {main}  
[2017-04-19 01:31:38] local.ERROR: ErrorException: file_put_contents(/home/vagrant/sites/inventory/storage/framework/sessions/f0d71b09248682b4a9688510fcd4c3001e20682a): failed to open stream: Permission denied in /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php:111
Stack trace:
#0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'file_put_conten...', '/home/vagrant/s...', 111, Array)
#1 /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php(111): file_put_contents('/home/vagrant/s...', 'a:5:{s:6:"_toke...', 2)
#2 /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Session/FileSessionHandler.php(83): Illuminate\Filesystem\Filesystem->put('/home/vagrant/s...', 'a:5:{s:6:"_toke...', true)
#3 /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Session/Store.php(262): Illuminate\Session\FileSessionHandler->write('f0d71b09248682b...', 'a:5:{s:6:"_toke...')
#4 /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(88): Illuminate\Session\Store->save()
#5 /home/vagrant/sites/inventory/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(155): Illuminate\Session\Middleware\StartSession->terminate(Object(Illuminate\Http\Request), Object(Illuminate\Http\Response))
#6 /home/vagrant/sites/inventory/public/index.php(48): Illuminate\Foundation\Http\Kernel->terminate(Object(Illuminate\Http\Request), Object(Illuminate\Http\Response))
#7 {main}  
vmitchell85's avatar

does the /home/vagrant/sites/inventory/storage/framework/sessions/ folder exist? what are the permissions on that folder if it does exist

afonte15's avatar

vagrant@homestead:~/sites/inventory/storage$ ls -l total 12

drwxr-xrwx 1 vagrant vagrant 4096 Mar  2 21:55 app
drwxr-xrwx 1 vagrant vagrant 4096 Mar  2 21:55 framework
drwxr-xrwx 1 vagrant vagrant 4096 Apr 18 02:04 logs
vagrant@homestead:~/sites/inventory/storage$ cd framework
vagrant@homestead:~/sites/inventory/storage/framework$ ls -l
total 12
drwxrwxrwx 1 vagrant vagrant 4096 Mar 10 20:55 cache
drwxrwxrwx 1 vagrant vagrant 4096 Apr 19 01:31 sessions
drwxrwxrwx 1 vagrant vagrant 4096 Apr 19 01:31 views
vagrant@homestead:~/sites/inventory/storage/framework$ cd sessions
vagrant@homestead:~/sites/inventory/storage/framework/sessions$ ls -l
total 20
-rw-r--r-- 1 vagrant vagrant 197 Apr 19 01:30 074589b8bef8732a3a55eaca60ceaa9e2adb4610
-rw-r--r-- 1 vagrant vagrant 360 Apr 19 01:31 21c72020b901982cd900a79aef6151ea0cd9ed72
-rw-r--r-- 1 vagrant vagrant 268 Apr 19 01:31 236b18b7007d091ef48d2d863627e545286c39e1
-rw-r--r-- 1 vagrant vagrant 349 Apr 19 01:31 f0d71b09248682b4a9688510fcd4c3001e20682a
-rw-r--r-- 1 vagrant vagrant 268 Apr 19 01:31 ff658e4153dbe2a47c29e631e8d583919f9cd549
vagrant@homestead:~/sites/inventory/storage/framework/sessions$ 
vmitchell85's avatar

I'm out of ideas... If you can I would suggest destroying the vagrant box, downloading the latest Homestead again.

Sorry I couldn't be of more assistance.

afonte15's avatar

I run into this error when i have

class InventoriesController extends Controller
{
   public function __construct()
    {
        $this->middleware('auth');  
    }

But if I uncomment

class InventoriesController extends Controller
{
   public function __construct()
    {
        //$this->middleware('auth');  //does not allow users to login, redirects back to login when using LDAP credentials

    }

I am able to login and I receive no errors but then I am able to access the other web url without being logged in.

Any thoughts?

vmitchell85's avatar

It seems you're using file based sessions and auth requires the session files. Since you cannot access the files for some reason that is why commenting that out works, since you don't need auth anymore for those routes.

afonte15's avatar

Can I ask you one more question. I am setting up my development machine again.

Setting up vagrant/homestead

I ran bash init.sh and it said homestead initialized but when I try to navigate to the .homestead folder I get bash: cd: .homestead: No such file or directory

I am on Ubuntu 16.04

Please or to participate in this conversation.