Setup file permission for Laravel

Setup file permission for Laravel

During the deployment of laravel application, many face the issue due to incorrect file permission in the production server specifically in linux based server. Let’s see how to fix & setup file permission for laravel application.

Important Note

Generally many developers will give full permission to the whole project (sudo chmod -R 777 path/to/project_directory) as a quick fix, but that is a bad idea.

Beaware of taking this decision/running this command to the production server as it’s very dangerous. As you are going to give all the permissions for file (read/write/execute) to any users (or unknown users) to your server and hackers may misuse it to gain access to the production server.

Setup file permission for Laravel

First of all, Identify the user name under which the web server is running (Generally you can check this using top command or from web servers config file). 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

Here, we are assuming that our web server is running with account www-data as we are using nginx web server. Now change the owner and group-owner for all files and directories recursively using below command:

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

Ok, now we are going to give permission 644  to files and 755 to directories using below command:

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

What does it mean?

  • First command changes all file under /path/to/laravel_root_directory permissions to 644
  • Second command changes all directory under /path/to/laravel_root_directory permissions to 755

So, we’ve done most of the setup and your application now should work as expected 🙂

0 0 votes
Article Rating
Inline Feedbacks
View all comments