EN VI

PHP Laravel - How to set up file permissions when deploy in host vps centos linux

Problem

I’m using Apache Web Server that has the folder owner set to _www:_www. I keep forgetting about the best practice when it comes to file permissions, for example when I create a new Laravel 5 project.

Does this mean that Apache needs access to the storage and vendor folders as well or just their current contents?

Laravel 5 requires /storage folder to be writable. Despite not being the best idea, I have been setting the 777 permission for the folder

Solution

Before giving the final answer, I just want to state the obvious. If you want to give any folder a 777 permission, you are pretty much-inviting everyone to read, write, and execute files in that directory.

There are basically two ways to set up your ownership and permissions:

  • Give yourself ownership
  • Make the webserver the owner of all files.

The second is how most people do and that’s what Laravel suggests.

assuming www-data is your web server user, you can run:

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


Then, a good practice is to set all your directories to 755 and all of your files to 644… SET file permissions using the following command:

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


SET directory permissions:

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

One thing you don’t want to forget is to give the webserver access to read and write files in the cache folder

Your webserver will need to upload and store data as well so make sure you give the permissions for the storage folder as well:

sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache
Rating: 10 (1 Votes)
Comment

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login