How to set up file permissions for Laravel?

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

    I had the following configuration:

    • NGINX (running user: nginx)
    • PHP-FPM

    And applied permissions correctly as @bgies suggested in the accepted answer. The problem in my case was the php-fpm's configured running user and group which was originally apache.

    If you're using NGINX with php-fpm, you should open php-fpm's config file:

    nano /etc/php-fpm.d/www.config
    

    And replace user and group options' value with one NGINX is configured to work with; in my case, both were nginx:

    ... ; Unix user/group of processes ; Note: The user is mandatory. If the group is not set, the default user's group ; will be used. ; RPM: apache Choosed to be able to access some dir as httpd user = nginx ; RPM: Keep a group allowed to write in log dir. group = nginx ...

    Save it and restart nginx and php-fpm services.

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

    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, and the Laravel doc's way):

    assuming www-data (it could be something else) is your webserver user.

    sudo chown -R www-data:www-data /path/to/your/laravel/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/laravel/root/directory -type f -exec chmod 644 {} \;    

    SET directory permissions

    sudo find /path/to/your/laravel/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/laravel/root/directory

    Then I give both myself and the webserver permissions:

    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 {} \;

    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

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

    The permissions for the storage and vendor folders should stay at 775, for obvious security reasons.

    However, both your computer and your server Apache need to be able to write in these folders. Ex: when you run commands like php artisan, your computer needs to write in the logs file in storage.

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

    sudo chown -R www-data:www-data /path/to/your/project/vendor
    sudo chown -R www-data:www-data /path/to/your/project/storage
    

    Then you need to add your computer (referenced by it's username) to the group to which the server Apache belongs. Like so :

    sudo usermod -a -G www-data userName
    

    NOTE: Most frequently, groupName is www-data but in your case, replace it with _www

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

    We've run into many edge cases when setting up permissions for Laravel applications. We create a separate user account (deploy) for owning the Laravel application folder and executing Laravel commands from the CLI, and run the web server under www-data. One issue this causes is that the log file(s) may be owned by www-data or deploy, depending on who wrote to the log file first, obviously preventing the other user from writing to it in the future.

    I've found that the only sane and secure solution is to use Linux ACLs. The goal of this solution is:

    1. To allow the user who owns/deploys the application read and write access to the Laravel application code (we use a user named deploy).
    2. To allow the www-data user read access to Laravel application code, but not write access.
    3. To prevent any other users from accessing the Laravel application code/data at all.
    4. To allow both the www-data user and the application user (deploy) write access to the storage folder, regardless of which user owns the file (so both deploy and www-data can write to the same log file for example).

    We accomplish this as follows:

    1. All files within the application/ folder are created with the default umask of 0022, which results in folders having drwxr-xr-x permissions and files having -rw-r--r--.
    2. sudo chown -R deploy:deploy application/ (or simply deploy your application as the deploy user, which is what we do).
    3. chgrp www-data application/ to give the www-data group access to the application.
    4. chmod 750 application/ to allow the deploy user read/write, the www-data user read-only, and to remove all permissions to any other users.
    5. setfacl -Rdm u:www-data:rwx,u:deploy:rwx application/storage/ to set the default permissions on the storage/ folder and all subfolders. Any new folders/files created in the storage folder will inherit these permissions (rwx for both www-data and deploy).
    6. setfacl -Rm u:www-data:rwX,u:deploy:rwX application/storage/ to set the above permissions on any existing files/folders.
    0 讨论(0)
  • 2020-11-22 00:38

    Change the permissions for your project folder to enable read/write/exec for any user within the group owning the directory (which in your case is _www):

    chmod -R 775 /path/to/your/project
    

    Then add your OS X username to the _www group to allow it access to the directory:

    sudo dseditgroup -o edit -a yourusername -t user _www
    
    0 讨论(0)
  • 2020-11-22 00:39

    The Laravel 5.4 docs say:

    After installing Laravel, you may need to configure some permissions. Directories within the storage and the bootstrap/cache directories should be writable by your web server or Laravel will not run. If you are using the Homestead virtual machine, these permissions should already be set.

    There are a lot of answers on this page that mention using 777 permissions. Don't do that. You'd be exposing yourself to hackers.

    Instead, follow the suggestions by others about how to set permissions of 755 (or more restrictive). You may need to figure out which user your app is running as by running whoami in the terminal and then change ownership of certain directories using chown -R.

    If you do not have permission to use sudo as so many other answers require...

    Your server is probably a shared host such as Cloudways.

    (In my case, I had cloned my Laravel application into a second Cloudways server of mine, and it wasn't completely working because the permissions of the storage and bootstrap/cache directories were messed up.)

    I needed to use:

    Cloudways Platform > Server > Application Settings > Reset Permission

    Then I could run php artisan cache:clear in the terminal.

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