Setting the proper file permission for any web application is an important part of web hosting. In this tutorial, you will learn how to properly configure file permissions on a Laravel application hosted on a Linux web server.

First of all, Identify the user name under which the webserver is running. Here are some default cases

  • Nginx on Linux uses account – www-data
  • Apache on Debian systems uses account – www-data
  • Apache on Redhat systems uses account – apache

Setup Laravel File Permissions

Assuming that the webserver is running with www-data user on your system . So all files should have the same user ownership as the webserver user. Use the chown command to set owner and group-owner for all files and directories recursively.

sudo chown -R www-data:www-data /path/to/laravel 

Next, set 644 permission for all files and 755 for all directories. We can achieve this with chmod command and find commands together as below:

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

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

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

Now, your Laravel application is secured with proper permissions. But as all the files have owner and group-owner to web server, you may face an issue during changes via FTP/sFTP. To solve this problem add your user to the webserver user group:

sudo usermod -a -G www-data ubuntu 

Read our another tutorial to properly create an SFTP account for the web server document root.

Conclusion

In this tutorial, You have learned to properly configure file permissions for the Laravel applications.