Set Magento 2 File Permissions Correctly

Magento 2 file permissions are one of the most common reasons why stores break after running setup:upgrade, setup:di:compile, or deploying static content. If permissions are wrong, Magento cannot write files, cache fails, and the site may show 500 errors. In this guide, you’ll learn how to set Magento 2 file permissions correctly for both single-user and multi-user servers.

Understanding Magento 2 File Permissions

Before fixing Magento file permissions, you need to understand three basic concepts in Linux:

  • Owner – the user who owns the file
  • Group – a group of users who share access
  • Permission – read (r), write (w), execute (x)

Common permission values:

  • 755 = owner can write, others can read
  • 775 = owner and group can write
  • 644 = file readable by all, writable by owner

Magento needs write access to some folders, but most of the system should stay read-only for security.

Magento File Permissions for Single-User Servers

A single-user setup means:

  • SSH user = web server user (nginx/apache)

This is the simplest and safest model.

Recommended setup:

cd /var/www/magento
find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;
chmod +x bin/magento

chmod -R 775 var generated pub/static pub/media

Why this works:

  • Core files stay protected
  • Magento can write to:
    • var
    • generated
    • pub/static
    • pub/media

This setup prevents most Magento 2 file permissions issues.

Magento File Permissions for Multi-User Servers

Multi-user setup means:

  • One user runs SSH commands
  • Another user runs web server

This often causes permission denied errors.

Step 1: Create a shared group

sudo groupadd magento
sudo usermod -a -G magento www-data
sudo usermod -a -G magento youruser

Step 2: Change ownership

sudo chown -R youruser:magento /var/www/magento

Step 3: Set group permissions

find . -type d -exec chmod 775 {} \;
find . -type f -exec chmod 664 {} \;
chmod g+s .
chmod +x bin/magento

chmod -R 775 var generated pub/static pub/media

Now both users can write safely without breaking Magento 2 file permissions.

Recommended Magento File Permissions by Folder

Folder/FilePermissionReason
app/755Read-only code
vendor/755Composer files
var/755Cache, logs
generated/755DI compile
pub/static/755Static deploy
pub/media/755Uploads
bin/magento755Executable
.htaccess644Read-only

Why Magento Breaks After setup:upgrade or setup:di:compile

Magento 2 file permissions often break because:

  • Commands are run as a different user
  • New files get wrong owner
  • Web server cannot read/write them
  • Result: 500 error, blank page, or admin down

Example:

  • You run setup:di:compile as root
  • Files become owned by root
  • Web server cannot access them
  • Site crashes

How to Fix Permission Errors Step by Step

1. Go to Magento root

2. Reset ownership

sudo chown -R youruser:magento /var/www/magento

3. Reset permissions

find . -type d -exec chmod 775 {} \;
find . -type f -exec chmod 664 {} \;
chmod -R 775 var generated pub/static pub/media
chmod +x bin/magento

4. Clear generated folders

rm -rf var/cache var/page_cache generated/*

5. Run Magento commands again

Security Tips for permission config

  • Never use 777 in production
  • Never run Magento as root
  • Keep app/ and vendor/ read-only
  • Only allow write access where needed
  • Always fix permissions after deploy

Correct Magento file permissions protect your store from both errors and attacks.

Conclusion

Magento file permissions are critical for a stable and secure store. Most broken setups come from wrong ownership or permissions, especially after running setup:upgrade or setup:di:compile.

If your store feels unstable, don’t panic—our full Magento services team is ready to help you fix permission issues, improve performance, and grow your business. Explore all our services.

Set Magento 2 file permissions correctly from day one, and you’ll avoid many painful production issues later.

5 1 vote
Article Rating
Aaron LX

Aaron LX

Aaron is a passionate writer, crazy about shopping, eCommerce and trends. Besides his outstanding research skills and a positive mind, Aaron eagerly shares his experience with the readers.

Leave a Reply or put your Question here

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x