[Solved] How to set up file permissions for Laravel?

Laravel Mohit Mozumder

Problem:

How to set up file permission for laravel?


Solution:

First lets understand what 777 permission means. Actually If you Set your folder permission to 777 it means you opened the folder for everyone who can find the directory. That means you give ANYONE (including hackers) permission to upload any file, virus or any other file.There are basically two ways to setup your ownership and permissions.

1. You give yourself ownership 

2. You make the webserver the owner of all files.

Webserver as owner:

Let assume www-data is your webserver user.

sudo chown -R www-data:www-data /path/to/your/laravel/root/directory

when do that, the webserver owns all the files, and group. But you will have some problems uploading files or working with files via FTP, because your FTP client will be logged in as you, not as 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 and your user is ubuntu.

Then 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

First go to your laravel root directory:

cd /var/www/html/laravel >> assuming this is your current root directory
sudo chown -R $USER:www-data .

After that  give both yourself and the webserver permissions:

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

Then give the webserver the rights to read and write to storage and cache

Now 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 so run the commands from bashy above :

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

Thats it. Hope this will help you.



Thank you for reading the article. If you face any further problem feel free to contact with us.