3

SOLVED - see my answer below.

I changed my theme and all pages showed only html without any styling. Then I ran sudo php bin/magento setup:static-content:deploy and after website doesnt work and every page shows HTTP ERROR 500.

Any idea what this error log below means?

nginx error log:

2017/01/25 16:10:44 [error] 1124#1124: *540 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Zend_Cache_Exception: cache_dir "/var/www/html/mg/var/page_cache" is not writable in /var/www/html/mg/vendor/magento/zendframework1/library/Zend/Cache.php:209
Stack trace:
#0 /var/www/html/mg/vendor/magento/zendframework1/library/Zend/Cache/Backend/File.php(180): Zend_Cache::throwException('cache_dir "/var...')
#1 /var/www/html/mg/vendor/colinmollenhour/cache-backend-file/File.php(87): Zend_Cache_Backend_File->setCacheDir('/var/www/html/m...')
#2 /var/www/html/mg/vendor/magento/zendframework1/library/Zend/Cache.php(153): Cm_Cache_Backend_File->__construct(Array)
#3 /var/www/html/mg/vendor/magento/zendframework1/library/Zend/Cache.php(94): Zend_Cache::_makeBackend('Cm_Cache_Backen...', Array, true, true)
#4 /var/www/html/mg/vendor/magento/framework/App/Cache/Frontend/Factory.php(155): Zend_Cache::factory('Magento\\Framewo...', 'Cm_Cache_Backen...', Array, Array, true, true, true)
#5 /var/www/html/mg/vendor/magento/framework/App/Cache/Frontend/Pool.php(67): Magento\Fr" while reading response header from upstream, client: 127.0.0.1, server: edu.dev, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "edu.dev"
  • After deploy, upgrade command, you should change permission for var, pub/static directory and it's 777. – Sohel Rana Jan 25 '17 at 14:41
  • 2017/01/25 16:10:44 [error] 1124#1124: *540 FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught Zend_Cache_Exception: cache_dir "/var/www/html/mg/var/page_cache" is not writable in /var/www/html/mg/vendor/magento/zendframework1/library/Zend/Cache.php:209 -> this means '/var/www/html/mg/var/page_cache' is not writable. – Sohel Rana Jan 25 '17 at 14:43
6

I had to re-run permission commands on my magento 2 installation directory. Don't know why would I need to re-run them.

sudo chmod -R 777 /var/www/html/mg

Is this correct? Seems excessive permission to me. Maybe within a close test environment?

  • 1
    Isn't giving 777 permission to the whole mag dir is kinda risky ? – Mohammed Joraid Jul 28 '17 at 6:30
  • Perhaps, but when working locally @MohammedJoraid I don't see the huge security risks – FooBar Feb 14 at 20:16
  • @FooBar Absolutely. I wasn't aware of the assumption that it's a local environment. – Mohammed Joraid Feb 26 at 13:53
3

As this is a permissions issue you should run all commands from the CLI as the web server user. Typically it is www-data user, so after you set all permissions (as magento devdocs recommends) DO NOT execute:

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

Because the magento will generate all files with the root user and it will be the owner of the files. In this case web-server user www-data cant write this files without +w permission set for the others.

Right way:

sudo -u www-data php bin/magento setup:static-content:deploy

In this case the www-data user will be set as file owner and all should work fine.

0

You must config the security context of the files and force it to be in the Read and Write mode.

This is the command:

chcon -R -t httpd_sys_rw_content_t directory_or_filename

0

As Siarhey Uchukhlebau mentions, you should not run the command php bin/magento setup:static-content:deploy using sudo. Instead you should create a new user that will own the magento filesystem, add that user to the web server group (if using apache2 this will be www-data), and then chown recursively all files on your magento installation. And use that same user to run any magento commands. So:

  1. Create the user that will own the magento file system :

    sudo adduser magento
    

The name after adduser command is the name of your new user. In this case we will use magento as the user name for simplicity. Give it a password when prompted.

  1. Add the new user to the web server group:

    sudo usermod -aG www-data magento
    
  2. Assign the new user as the new owner of the magento file system:

    sudo chown -R magento:www-data ./
    

Please not that you should only use ./ if you are inside your magento root directory. Replace this by your root installation path

  1. Log in as the new user:

     su - magento 
    

Use this new user to run any magento commands, without using sudo

0

give permission to var folder 777 and as well it's all subfolers 777 it worked for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.