How to set up file permissions for Laravel?

前端 未结 15 2157
忘了有多久
忘了有多久 2020-11-22 00:08

I\'m using Apache Web Server that has the owner set to _www:_www. I never know what is the best practice with file permissions, for example when I create new La

15条回答
  •  無奈伤痛
    2020-11-22 00:23

    I have installed laravel on EC2 instance and have spent 3 days to fix the permission error and at last fixed it. So I want to share this experience with other one.

    1. user problem When I logged in ec2 instance, my username is ec2-user and usergroup is ec2-user. And the website works under of httpd user: apache: apache so we should set the permission for apache.

    2. folder and file permission A. folder structure first, you should make sure that you have such folder structure like this under storage

      storage

      • framework
        • cache
        • sessions
        • views
      • logs The folder structure can be different according to the laravel version you use. my laravel version is 5.2 and you could find the appropriate structure according to your version.

    B. permission At first, I see the instructions to set 777 under storage to remove file_put_contents: failed to open stream error. So i setup permission 777 to storage chmod -R 777 storage But the error was not fixed. here, you should consider one: who writes files to storage/ sessions and views. That is not ec2-user, but apache. Yes, right. "apache" user writes file (session file, compiled view file) to the session and view folder. So you should give apache to write permission to these folder. By default: SELinux say the /var/www folder should be read-only by the apache deamon.

    So for this, we can set the selinux as 0: setenforce 0

    This can solve problem temporally, but this makes the mysql not working. so this is not so good solution.

    You can set a read-write context to the storage folder with: (remember to setenforce 1 to test it out)

    chcon -Rt httpd_sys_content_rw_t storage/
    

    Then your problem will be fixed.

    1. and don't forget this composer update php artisan cache:clear

      These commands will be useful after or before.

      I hope you save your time. Good luck. Hacken

提交回复
热议问题