36

I'm having problem in setting up Laravel 4. I'm having this error when opening the index page:

file_put_contents(/Users/laravel/app/storage/meta/services.json) [function.file-put-contents]:
failed to open stream: Permission denied.

What am I missing here?

Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
Bryan P
  • 4,142
  • 5
  • 41
  • 60

6 Answers6

52

I think chmod -777 is a bad practice.

To solve permissions issue on Laravel, I've done this (with root user):

cd app/

chown -R www-data:www-data storage

cd storage/

find . -type d -exec chmod 775 {} \; && find . -type f -exec chmod 664 {} \;

And always in app/storage directory :

chown your_user:user_group .gitignore cache/.gitignore logs/.gitignore meta/.gitignore sessions/.gitignore views/.gitignore

Now, exit the root user, and it's ok.

EDIT : this was for Laravel4. This doesn't works for Laravel5 because of a different structure.

François
  • 706
  • 1
  • 7
  • 9
  • Thanks this was helpful. Just for more info, in OSX 10.9, the default user and group that the apache (apache that was shipped with OSX) is _www and _www so I did chown -R _www:_www storage/ – jonprasetyo Sep 27 '14 at 09:09
  • +1 Only changes for Laravel 5 are that you don't do `cd app` first and the last command should be `chown vagrant:www-data .gitignore framework/cache/.gitignore logs/.gitignore app/.gitignore framework/sessions/.gitignore framework/views/.gitignore` – Simon Aug 20 '15 at 11:16
  • Probably, it would be easier to use `chmod` instead of `find`: `chmod -R ug=rwX,o=r app/storage` – antonbormotov Jun 14 '16 at 06:51
46

The storage directory needs to be writable by the webserver user.

Franz
  • 11,353
  • 8
  • 48
  • 70
15

It's better that you do not change the permissions, you should just change the owner:group of the storage folder to that of the owner:group of apache by using chown, this is more complicated but is more secure and you'll get used to this soon, you can find this in the httpd.conf

MAMP:

/Applications/MAMP/conf/apache/httpd.conf

XAMMP:

/Applications/XAMPP/xamppfiles/etc/httpd.conf

Open the httpd.conf with your preferred editor then search for User/Group. After searching you will see these lines below that highlighted line you searched:

User someuser
Group somegroup

Once you already know the User and Group, cd to your laravel directory then, do the chown:

chown -R someuser:somegroup app/storage

P.S: If you get permission something error upon doing chown, try this:

sudo chown -R someuser:somegroup app/storage

It will then ask for your password so input it then done.

  • In debian 7 you should change the user/group in /etc/apache2/envvars and then change the ownership of /var/lock/apache2. I suppose that in other debian based linux should be similar. – Salvador P. Mar 21 '15 at 23:50
5

Just ran into this problem on centos 7. The blocker wasn't folder permissions, rather it was selinux policy blocking nginx/php to access /var/www. So, if the above doesn't work, try disabling selinux as a test to see if you can re-run composer without any permission issues.

user3670777
  • 101
  • 2
  • 2
  • 4
    This took me almost an entire day to figure out. [Here's a link to more information on this program](http://www.crypt.gen.nz/selinux/disable_selinux.html) as well as how to disable it. For quick reference the command is: `setenforce 0` – Drellgor Nov 17 '14 at 23:48
  • 3
    You should NOT disable selinux. You can solve your problem here: http://stackoverflow.com/a/27377624/2570054 – Sky Dec 09 '14 at 11:18
1

If you are using Apache on Ubuntu, storage and bootstrap/cache folders need to be writable by web server user.

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

This will allow write permissions to these folders for web server users.

Ajnas Kt
  • 461
  • 4
  • 4
0

In my case this error was thrown:

fopen(php://stdout): failed to open stream: operation failed

i am using a virtual host in Wamp. therefor i added this line to my <VirtualHost> -> <Directory> block in httpd-vhosts.conf file:

Require all granted

and my block is:

<VirtualHost *:80>
    ServerName laravel
    DocumentRoot d:/wamp/www
    <Directory  "d:/wamp/www/">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

then problem solved!