files folders permission

How to Securely Set Laravel 5 Files Folders Permission and Ownership Setup

Files Folders Permission and Ownership:

Deploying a Laravel application to a production environment can be challenging at times. So, in this post, we will discuss how to deal with files folders permission and ownership problems.

Every developer wants his/her application to be setup in a secure environment when deploying it to a production server. One key area you should look at is the folder permissions because that could be the simple point of failure. Making the application venerable to hackers.

Security Issue: Files and Folders Permission

The first error you might get is due to improper file and folder permissions. Because of that most people quickly set their files and folders permission to 777 on the production server.

However, it is a bad practice to set your files and folders directory permission to 777, because that makes your server open to the world.

That approach makes it possible for anyone using the application to have read, write and execute permission on your production server.

Simply, anyone can read, write and execute files using your application. So, hackers can upload malicious files that damage your project.

Therefore, always avoid setting 777 Permission for your files and folders.

Setting Files Folders Permission and Ownership for Laravel 5:

Firstly, find the web server user, for apache it is www-data. But use the following command to check:

The output should be similar to this:

From the output, apache user is www-data.

Now, change the owner of the project directory to www-data using the following command:

Note: you must specify the path to the project directory

Set Folders permissions to 755 and file of your project to 644:

Files and Folders/Directories Permission:

Setting Folders/Directories Permission

The appropriate permissions to be given to your folders or directories is 755. The command below set the folder permissions for your project to 755:

Setting Files Permissions:

The appropriate permissions for your files should be 644. Use the following command to set the file permissions for your project to 644:

Giving the appropriate permissions and ownership to your project folders and files limit the end user’s permission to read-only and cannot write and execute malicious files on the production server.

Even though we have secure the files and folders Laravel still needs read-write permission to the storage and the cache folder.

Use the following commands to fix the read-write permission:

You can assign the same permission to your file upload directory as well.

Setting Permissions for SFTP/FTP Upload Files:

Add your user to the group using the following command:

Now change the ownership using the following command:

Finally, assign files and folder permissions using the following command:

We can be sure that our production environment is relatively secure for the Laravel project.

Clemence Ayekple

Let's grab a cup of coffee and talk about programming

Add comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.

Most discussed

%d bloggers like this: