If you get one of these permission errors:

1
exception 'Magento\Framework\Exception\LocalizedException' with message 'Can't create directory /var/www/html/magento2/var/generation

it can be because:

  • the user permissions on the var folder are wrong
  • the owner of the var folder is wrong
  • the user doesn't have access to the database

If it is one of the first 2 situations you can solve it simply with find /var -type d -exec chmod g+s {} \; from the root folder. Or chown -R user var to change the user.

If you still get the error and maybe you are running a VM, make sure that the user has access to the database. Run a magento command like php bin/magento setup:static-content:deploy. If you get a database error like this one:

1
2
[PDOException]                                                                          
  SQLSTATE[HY000] [1045] Access denied for user ...

Then you need to login to mysql and give the right privileges to your user:

1
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

Another issue can be that you are using a VM and you are applying the permissions to the shared folder from the VM instead from your local system.

If it still doesn’t work, maybe it is a cached issue, so you can:

1
2
3
4
5
cd pub/static
find . -depth -name .htaccess -prune -o -delete
cd ../../
rm -rf var/cache/ var/generation/ var/page_cache/ var/view_preprocessed/
php bin/magento setup:static-content:deploy