Getting Magento 2 File Ownership and Permissions Right
Many Magento 2 errors, from failed deploys to missing images, come down to file permissions. The aim is simple: one file system owner, the web server group able to write where needed, and nothing world-writable.
1. Work as the Magento file owner
Log in as, or switch to, the user that owns the Magento files, then go to the installation folder:
cd <web server docroot>/<magento2 base dir>
Typical locations:
Ubuntu: /var/www/magento2 CentOS: /var/www/html/magento2
2. Set group ownership
chown -R :<your web server group name> .
For example:
CentOS: chown -R :apache . Ubuntu: chown -R :www-data .
3. Set permissions
find . -type d -exec chmod 770 {} \; && find . -type f -exec chmod 660 {} \; && chmod u+x bin/magento
If you need sudo:
sudo find . -type d -exec chmod 770 {} \; && sudo find . -type f -exec chmod 660 {} \; && sudo chmod u+x bin/magento
In production, tighten this further by making code read-only and leaving only var, generated, pub/static and pub/media writable.
Need a second pair of eyes on this? Our team handles Magento development every week. See how we can help.