Search code examples
phpviewmamplaravelchmod

Laravel on MAMP every time I create a new view, I have to CHMOD storage/views to 777, then 775


I am working through Shawn McCool's book (great book) and each time I create a new view, and try to load it, I get a permissions error.

I am wondering why the views generated don't have read permissions. The odd thing is if I chmod to 775, I still get the error, I have to 777 then 775.

Can anyone shed some light on this and why it's happening?

Thanks!

Edit:

Error after adding new view

file_put_contents(/Users/jason/Sites/laravel1/storage/views/5c4b7b4707d658dffe52d481be6c680e): failed to open stream: Permission denied

Permissions on storage/views
drwxrwxr-x@  7 jason  jason  238 Mar  7 17:15 views

Permissions on new view -rw-r--r--

Error after chmod 775 views

 file_put_contents(/Users/jason/Sites/laravel1/storage/views/5c4b7b4707d658dffe52d481be6c680e): failed to open stream: Permission denied

Permissions after chmod 775 views

drwxrwxr-x@  7 jason  jason  238 Mar  7 17:15 views

After chmod 777 views, it works

drwxrwxrwx@  8 jason  jason  272 Mar  7 17:25 views

After chmod 777 views it still works

drwxrwxr-x@  8 jason  jason  272 Mar  7 17:25 views

But here is the twist, I did not have this issue if I didn't use viewname.blade.php. If I did viewname.php, there was no error. It seems to have something to do with Blade.

Edit 2:

Related: always make sure to .gitignore these files so that you don't end up with cached views, etc. getting pulled onto your server from your dev environment.


Solution

  • Since (I think) Lion, the web server does not use mod_userdir anymore, so apache does not run as your user but as _www instead, see

    user:~$ ps ax -o pid,user,rgroup,comm | grep -Ee "PID|httpd"
      PID USER             RGID COMM
    10317 root                0 /usr/sbin/httpd
    16516 _www               70 /usr/sbin/httpd
    18497 _www               70 /usr/sbin/httpd
    ...
    

    Now for your folder with permissions 775, user jason and group jason

    drwxrwxr-x@ 7 jason jason 238 Mar 7 17:15 storage/views
    

    Laravel will try to create a cache file for every blade view in that folder. If no such file exists, it will try to create that automatically. This requires write access to the folder, and since Apache runs as _www it requires write access on that directory as other.

    Once the file exists however, that file is owned by _www, and so Apache does not require write access to the storage/views directory anymore. Until a new view is created...

    Now what you can do is:

    (keep in mind that there are other caches too in storage other than storage/views, so everything here is done on the whole storage folder)

    • either set the storage directory to 777 (for development this is "ok")

      chmod -R 777 storage
      
    • Or (better) chown the storage directory to jason:_www, this way you can keep the 775 permissions and both you and apache have sufficient access.

      sudo chown -R jason:_www storage