Setting file permissions in Laravel 5

When you start developing a new Laravel project Laravel asks permission to access some files, many of us simply give chmod 777 commands recursively. That is not the best idea, Let’s see what are the best practices.

If you give 777 permissions to your folders, you are making it vulnerable to every kind of attacks. It gives access to anyone and allows anyone to execute, write, read.

Normally people setup ownership permissions in two ways. One is to give yourself ownership or making the web server the owner.

For making the web server as owner follow the steps given below.

sudo chown -R webserver-user:webserver-user/path/to/your/laravel root/directory

After making web server as the owner you will face some problems while trying to upload files or working with files through FTP. It is because FTP client will be logged in as you. To solve this add user to web server user group.

Sudo usermod –a –G webserver-user root

Here root is the user. Now set all your directories to 755 and files to 644.

sudo find /path/to/your/laravel/root/directory -type f -exec chmod 644 {} \;

sudo find /path/to/your/laravel/root/directory -type d -exec chmod 755 {} \;

The second way is to own all files and directory permissions as the user

sudo chown -R my-user: webserver-user/path/to/your/laravel/root/directory

Then give user and web server all 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 {} \;

You have to give webserver the permissions to upload, storage, cache, write.

sudo chgrp -R www-data storage bootstrap/cache

sudo chmod -R ug+rwx storage bootstrap/cache