How to set up file permissions for Laravel?

前端 未结 15 2109
忘了有多久
忘了有多久 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:20

    I found an even better solution to this. Its caused because php is running as another user by default.

    so to fix this do

    sudo nano /etc/php/7.0/fpm/pool.d/www.conf

    then edit the user = "put user that owns the directories" group = "put user that owns the directories"

    then:

    sudo systemctl reload php7.0-fpm

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-22 00:24

    Most folders should be normal "755" and files, "644"

    Laravel requires some folders to be writable for the web server user. You can use this command on unix based OSs.

    sudo chgrp -R www-data storage bootstrap/cache
    sudo chmod -R ug+rwx storage bootstrap/cache
    
    0 讨论(0)
  • 2020-11-22 00:27

    As posted already

    All you need to do is to give ownership of the folders to Apache :

    but I added -R for chown command: sudo chown -R www-data:www-data /path/to/your/project/vendor sudo chown -R www-data:www-data /path/to/your/project/storage

    0 讨论(0)
  • 2020-11-22 00:28

    For Laravel developers, directory issues can be a little bit pain. In my application, I was creating directories on the fly and moving files to this directory in my local environment successfully. Then on server, I was getting errors while moving files to newly created directory.

    Here are the things that I have done and got a successful result at the end.

    1. sudo find /path/to/your/laravel/root/directory -type f -exec chmod 664 {} \;
      sudo find /path/to/your/laravel/root/directory -type d -exec chmod 775 {} \;
    2. chcon -Rt httpd_sys_content_rw_t /path/to/my/file/upload/directory/in/laravel/project/
    3. While creating the new directory on the fly, I used the command mkdir($save_path, 0755, true);

    After making those changes on production server, I successfully created new directories and move files to them.

    Finally, if you use File facade in Laravel you can do something like this: File::makeDirectory($save_path, 0755, true);

    0 讨论(0)
  • 2020-11-22 00:30

    I decided to write my own script to ease some of the pain of setting up projects.

    Run the following inside your project root:

    wget -qO- https://raw.githubusercontent.com/defaye/bootstrap-laravel/master/bootstrap.sh | sh
    

    Wait for the bootstrapping to complete and you're good to go.

    Review the script before use.

    0 讨论(0)
提交回复
热议问题