DEV Community

Carlos Santos
Carlos Santos

Posted on

πŸ” Laravel File Permissions Setup

Introduction

Hi, I wanted to offer a brief and straight to the point way to setup your Laravel project permissions. This examples are like a work for all case. You will have to taken into consideration your server users, configurations, etc.
β€Žβ€Ž
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.

β€β€β€Ž

Web Server as owner (Recommended for PROD)
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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 {} \; 
Enter fullscreen mode Exit fullscreen mode

Set directory permissions

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

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:

sudo chgrp -R www-data storage bootstrap/cache  
sudo chmod -R ug+rwx storage bootstrap/cache
Enter fullscreen mode Exit fullscreen mode

β€β€β€Ž

Your user as owner (Convenient for STAGING and DEV)

If you prefer to own all the directories and files (it makes working with everything much easier), go to your Laravel root directory:

cd /var/www/html/laravel >> assuming this is your current root directory
Enter fullscreen mode Exit fullscreen mode

β€β€β€Ž β€Ž
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
Enter fullscreen mode Exit fullscreen mode
sudo chown -R $USER:www-data .
Enter fullscreen mode Exit fullscreen mode

Then you give your user and the webserver permissions:

sudo find . -type f -exec chmod 664 {} \;  
sudo find . -type d -exec chmod 775 {} \;
Enter fullscreen mode Exit fullscreen mode

β€β€β€Ž
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:

sudo chgrp -R www-data storage bootstrap/cache  
sudo chmod -R ug+rwx storage bootstrap/cache
Enter fullscreen mode Exit fullscreen mode

‏

Conclusion

I hope you found this article helpful.

If you have any feedback or found mistakes, please don’t hesitate to reach out to me.

Credit to: Andres Felipe and bgies answers.

Top comments (0)