How to Configure File and Folder Permissions in Laravel Applications

1. First and foremost, determine the user name under which the web server is running. Apache and Nginx both utilize the www-data account in Ubuntu.

2. Now, recursively change the owner and group-owner of all files and folders using following command:

sudo chown -R www-data:www-data /path/of/laravel-project

3. Now set permission 644 for all files and 755 for all directories using following command:

sudo find /path/of/laravel-project -type f -exec chmod 644 {} \;
sudo find /path/of/laravel-project -type d -exec chmod 755 {} \;

4. To make Laravel project work correctly, you need to provide read and write permissions to the web server for storage, cache, and any other directories. So run the following commands:

cd /path/of/laravel-project
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache 

5. Your Laravel application is now properly secured with appropriate permissions. However, because all files on the web server have an owner and a group owner, you may encounter a problem while making modifications through FTP/sFTP. To resolve this issue, add your user to the web server user group:

sudo usermod -a -G www-data ubuntu