5

After running setup:upgrade and di compile from the bin directory, permission and ownership is reset and the magento store stops working. It works if I manually reset the permission.

When the var/generation/Magento directory is owned by sohaib/www-data (sohaib is my current username) the website is loading properly.

When I run the bin/magento setup:upgrade command, the ownership has changed and the website is throwing errors.

Muckee
70910 silver badges40 bronze badges
asked May 10, 2016 at 0:28
2
  • You have to re-deploy and chmod after that! chmod -R 777 var/* in your current root magento directory Commented May 10, 2016 at 4:18
  • Friends don't let friends 777. Commented Apr 27, 2019 at 1:11

1 Answer 1

2

With regards to Mohammad's answer, permissions should never be set to 777.

The problem here is that newly generated files are owned by the user and group who issues the command $USER:$USER, where they should belong to $USER:www-data or $USER:apache. Because apache2 does not have access to the group $USER, the website cannot be properly served.

In order to retain proper permissions, magento commands should be executed as the apache user.

For example:

Ubuntu

$ sudo -u www-data php bin/magento setup:upgrade

CentOS

$ sudo -u apache php bin/magento setup:upgrade

This way, new files are owned by www-data:www-data (in the case of ubuntu).

So long as you have properly followed Magento's documentation with regards to permissions and added the cli user to the apache2 group, then you will have proper access to the files.

If you did not follow this documentation then you can execute the following command to do so:

Ubuntu

$ sudo usermod -a -G www-data $USER

CentOS

$ sudo usermod -a -G apache $USER

Note:

In order that commands can be executed without declaring the apache2 user, Daniel's answer explains that we can modify how the Magento framework sets permissions for new files.

If you are having trouble installing/updating via composer or deploying to production, then you can simply grant ownership of any inaccessible files (these are highlighted yellow in the terminal) to www-data by issuing the following command:

sudo chown www-data:www-data "/absolute/path/to/file.json"

Then, follow BrunoBueno's answer, applying the above fix:

$ sudo -u www-data bin/magento setup:upgrade
$ sudo -u www-data bin/magento indexer:reindex
$ sudo -u www-data bin/magento deploy:mode:set production -s
$ sudo -u www-data bin/magento setup:di:compile (Here was the secret, to run the di:compile after production)
$ sudo -u www-data bin/magento setup:static-content:deploy
answered Apr 24, 2018 at 10:34
4
  • 1
    Makes sense. But how do you run deploy:mode:set, which also updates the env.php file? I get "Deployment config file env.php is not writable." Commented Jul 13, 2018 at 11:35
  • I have just been working on that. Will edit the answer now to include the solution... Commented Jul 18, 2018 at 16:57
  • Makes sense, but does not work. My webserver group is www, but I get sudo: unknown user: www Commented Dec 11, 2019 at 15:30
  • @Black what command are you issuing when you see that response? Do the commands run without sudo? I think this is more likely a problem with your linux configuration. See this question Commented Dec 14, 2019 at 1:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.