File Permissions: Magento 2 Ownership And File Permission At Installation

I am going to explain about Magento 2 file permissions while installation of your Magento 2 framework. It is very necessary to give the correct file permission otherwise Magento 2 store will not work properly or that will show you the CSS and JS loading when you open the website.

 

Correct File Permissions in Magento 2

In this section first I will discuss how you set up the ownership of your Magento 2 root directory permission.

It would be different based on the Magento store mode of your development and production system.

So when you install the Magento you should give ownership of the file before the installation and here you will get to know what would be that ownership.

Based on User Requirments:

One User: In this section, we allow only one user access like you the only person who hosting the websites and managing the website, and developing the website.

Two users: In this section, we usually decide one is a developer and the second is a web server hosting manager so that manager will have all kinds of permission. This is one of the safest ways to set up the store.

The web server user can access the store frontend and admin panel.

 

File Permissions For default or Developer Mode Store Magento

In this section, we will understand what should be the correct permission for default or developer mode.

So the following directories should be writable by both Magento users in developer and default mode:

  • var
  • generated
  • pub/static
  • pub/media
  • app/etc

Remember you have to apply for the setgid permission so that will apply for inside files and folders as well.

find var generated pub/static pub/media app/etc -type f -exec chmod g+w {} +
find var generated pub/static pub/media app/etc -type d -exec chmod g+ws {} +

 

Magento 2 Production mode Files Permission

When we move our website from the development server or staging server to the live site so we have to change the Magento deploy mode.

So now remove write access from files in the following directories otherwise that will create security problems for your site.

  • vendor
  • app/code
  • app/etc
  • lib
  • pub/static
  • Any other static resources
  • generated/code
  • generated/metadata
  • var/view_preprocessed
code directories: read-only mode

Now change to production mode:

bin/magento deploy:mode:set production

Enter the following command as a user with root privileges:

find app/code lib pub/static app/etc generated/code generated/metadata var/view_preprocessed \( -type d -or -type f \) -exec chmod g-w {} + && chmod o-rwx app/etc/env.php

Note: So whenever you run some update command then you have to change the code directory file permission from reading mode to write mode then run the command.

find app/code lib var generated vendor pub/static pub/media app/etc \( -type d -or -type f \) -exec chmod g+w {} + && chmod o+rwx app/etc/env.php

 

Already Installed But Not Loading CSS, JS

If you have already installed a Magento 2 framework but just after the fresh installation, you are not able to access your store successfully then try the below permission.

Go to the installation directory of your store.

cd wishusucess

// 644 permission for files
find . -type f -exec chmod 644 {} \; 

// 755 permission for directory
find . -type d -exec chmod 755 {} \;

chmod 644 ./app/etc/*.xml

chown -R :<web server group> .

chmod u+x bin/magento

 

Magento 2 Admin Not Found Problem

If the problem is not been solved then try the below steps you will defiantly get the solution to your store loading problem.

Sometimes it happens because of the wrong file permission and when Magento not getting correct access to the files at the time of execution.

So its show error like below image, its shows Magento admin not found issue or frontend not loading correctly issues.File Permissions in Magento 2

If you are still facing the same problem as the above image then try the below steps.

Go to the Installation Directory

cd wishusucess

Now run the below command

sudo chmod -R 777 /

sudo a2enmod rewrite

sudo service apache2 restart

Now run this:

sudo gedit /etc/apache2/apache2.conf

Note: Sometimes it's not working then you have to go into the root user first then type the below command otherwise it will show you the same error.

gedit /etc/apache2/apache2.conf

you can exit from the root user

Now run below command in the magento root direcotry.

rm -rf var/di/* var/generation/* var/cache/* var/log/* var/page_cache/* var/session/* var/view_preprocessed/* pub/static/*

Now you have to edit the apache config setting so for that:

Replace this code :

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride none
Require all granted

with

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted

Note: Here we have changed AllowOverride None to AllowOverride All

Now run command:

sudo service apache2 restart

Now, run below command

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento setup:static-content:deploy -f

Magento 2 Loading Issues of Websites

 

Related Post:

Magento 2.4 Installation Guide: How to Install Magento 2.4.2

Elasticsearch: How to Install Elasticsearch in Ubuntu 18.04

 

Recommended Post:

eCommerce Framework: Magento 2 eCommerce Developers in India