77

I suspect that the permissions might have gotten messed up on my Magento 2 installation. On the old versions I would run the following command to make folders 755 and files 644:

find . -type f -exec chmod -c 644 {} \; && find . -type d -exec chmod -c 755 {} \;

Can you please let me know what the correct file and folder permissions are for Magento 2 since they seem to be different? Also, if there are some particular folders or files that would need different permissions.

4
  • Try 777 Permission before that take backup
    – Magento 2
    Nov 27 '15 at 19:54
  • My server's PHP handler is suPHP and I don't really want to assign 777 to everything since that would practically open my website to anyone. Are there more specific permissions that can be assigned?
    – Neekoy
    Nov 27 '15 at 20:02
  • Magento have the file permission on folders to 775 and for the files permission set to 644 Sep 1 '17 at 7:00
  • 1
    FYI, if magento 2 is in a "home" directory, var/cache and pub/static need suid bits set. I used: find var/cache -type d -print0 | xargs -0 sudo chmod 1775
    – Chris K
    Feb 6 '18 at 19:17

14 Answers 14

151

You can refer http://devdocs.magento.com/

The important things:

  • The owner of the Magento file system: Must have full control (read/write/execute) of all files and directories.

  • Must not be the webserver user; it should be a different user.

  • The web server user must have write access to the following files and directories:

    • var
    • app/etc
    • pub
    • (and probably new in 2.2.1:) generated
  • In addition, the web server's group must own the Magento file system so that the Magento user (who is in the group) can share access to files with the web server user. (This includes files created by the Magento Admin or other web-based utilities.)

  • We recommend setting the permissions as follows:

    • All directories have 770 permissions.

    • 770 permissions give full control (that is, read/write/execute) to the owner and to the group and no permissions to anyone else.

    • All files have 660 permissions.

    • 660 permissions mean the owner and the group can read and write but other users have no permissions.

You should set it as below:

cd <your Magento install dir> 

// 644 permission for files
find . -type f -exec chmod 644 {} \; 
                   
// 755 permission for directory
find . -type d -exec chmod 755 {} \;    

chmod 644 ./app/etc/*.xml

chown -R :<web server group> .

chmod u+x bin/magento

I hope this will help you.

14
  • 3
    It only works for me with \;, e.g.: find ./var -type d -exec chmod 777 {} \;
    – bpoiss
    Jan 11 '16 at 14:42
  • 1
    But this is for development mode. For production it should be group-readonly - no idea how that should work
    – Alex
    Feb 22 '16 at 18:52
  • 4
    I think you also need chmod u+x bin/magento to be able to execute console commands.
    – Volvox
    Aug 17 '16 at 11:08
  • 54
    friends don't let friends 777 Jan 31 '17 at 0:12
  • 10
    app/etc 777> Haha nice. Hackable easy. app/etc contains your db information May 11 '18 at 23:07
22

In some rare cases, you can't use 770 and 660, like @MagenX says, 755 and 644 could also be the permission you need. (Some Fast-CGI users I guess)

So in that case, you run:

find . -type d -exec chmod 755 {} \; && find . -type f -exec chmod 644 {} \; && chmod u+x bin/magento

That seems to fix it, but if Magento generates new files or directories, those will have the permission 770 and 660 again. You can edit those default chmod values in the following files:

/vendor/magento/framework/Filesystem/DriverInterface.php 
(WRITEABLE_DIRECTORY_MODE and WRITEABLE_FILE_MODE)

/lib/internal/Cm/Cache/Backend/File.php 
(directory_mode and file_mode)

After these changes, run the first command again and after that, newly generated files shouldn't be an issue anymore.

Note: Editing files like this is never a good idea, but I suspect these chmod options are going to be configurable in the future, so I took the easy way.

2
  • 1
    Cant find /lib/internal/Cm/Cache/Backend/File.php on M231
    – snh_nl
    Apr 11 '19 at 15:06
  • @snh_nl In newer Magento version, they don't set the permission anymore if they do, it looks like they use 775. After a fresh installation you can just run: find . -type f -exec chmod 664 {} \; find . -type d -exec chmod 775 {} \; find ./var -type d -exec chmod 777 {} \; find ./pub/media -type d -exec chmod 777 {} \; find ./pub/static -type d -exec chmod 777 {} \; chmod 777 ./app/etc chmod 644 ./app/etc/*.xml chmod u+x bin/magento In the root and you should be fine. Apr 15 '19 at 12:05
19

You can use the method like the Magento documentation recommends:

find . -type f -exec chmod 664 {} \;
find . -type d -exec chmod 775 {} \;
find var pub/static pub/media app/etc -type f -exec chmod g+w {} \;
find var pub/static pub/media app/etc -type d -exec chmod g+ws {} \;
chmod u+x bin/magento
2
  • How to give write permission to generated folder in windows? @Rafael Corrêa Gomes
    – zus
    Dec 15 '18 at 10:24
  • I still get ".../var/cache/" is not writable
    – Black
    Feb 27 '20 at 11:25
9

If you are using SELinux ̣(e.g. CentOS) try:

sudo chcon -R -t httpd_sys_rw_content_t var

(from magento root folder of course)

8
sudo find . -type d -exec chmod 770 {} \; && sudo find . -type f -exec chmod 660 {} \; && sudo chmod u+x bin/magento

For local development set pub and var to 775 or 777

2
  • 1
    why? (There is minimum to a comment length) Oct 18 '18 at 10:16
  • 2
    Why do unknown users need access to pub or cache??
    – Black
    Feb 27 '20 at 11:24
7

In Magento Community recommend

We recommend setting the permissions as follows:

All directories have 770 permissions. 770 permissions give full control (that is, read/write/execute) to the owner and to the group and no permissions to anyone else.

All files have 660 permissions. 660 permissions mean the owner and the group can read and write but other users have no permissions.

For more ref this link https://devdocs.magento.com/guides/v2.3/install-gde/prereq/file-system-perms.html

5

Similar issue also for sharing hosting:
Magento 2: Recommended File Permission for shared hosting

devdocs magento for permisions:
http://devdocs.magento.com/guides/v2.0/install-gde/prereq/file-system-perms.html
and for prod:
http://devdocs.magento.com/guides/v2.0/config-guide/prod/prod_file-sys-perms.html

5

Put this content inside your root folder, then execute it with the console.

Dont forget to chmod +x yourfile.sh.

#!/bin/sh
chmod 2775 -R .

chmod 777 -R ./pub/

chmod 777 -R ./var/
2
  • how can i put this code ? as htacces or php file
    – matinict
    Mar 4 '18 at 4:02
  • Avoid this, it's basically making your webserver directory open to the public. instead read the devdocs provided by magento to properly secure the version your running. Jun 5 '18 at 13:59
5

no special permissions, you just make sure that files owned by user who runs php or in case of cPanel - cpanel user, and same group

so your command is good:

find . -type f -exec chmod -c 644 {} \; && find . -type d -exec chmod -c 755 {} \;

just update files owner:

chown -R user:user *

where user is the owner, user who runs php.

instead of changing permissions everytime, login / work as proper user, files owner, from root to user:

cd /magento/root/folder/
su $(stat -c '%U' index.php) -s /bin/bash
whoami
pwd
1
  • How to give write permission to generated folder in windows? @MagenX
    – zus
    Dec 15 '18 at 10:26
4
find . -type f -exec chmod 400 {} \;
find . -type d -exec chmod 500 {} \;
find var/ -type f -exec chmod 600 {} \;
find media/ -type f -exec chmod 600 {} \;
find var/ -type d -exec chmod 700 {} \;
find media/ -type d -exec chmod 700 {} \;
chmod 700 includes
chmod 600 includes/config.php

Also detailed information is available on, http://devdocs.magento.com/guides/m1x/install/installer-privileges_after.html

1
  • 6
    Question calls for advise on Magento 2
    – Flipmedia
    Dec 16 '16 at 14:18
4

Execute these commands as a root user. If you have already given 777 permission revert it using first two commands else proceed with the rest.

find . -type f -exec chmod 664 {} \;

find . -type d -exec chmod 775 {} \;

find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +

find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +

Set the ownership to Magento user and web user using

sudo chown -R <Magento user>:<web server group> .

Running these commands sets the permission for folders and files in Magento root and for the new files to be created in var, pub and generated directories.

Don't run any command as root user as it might cause the new static files to be created with root ownership which might prevent web user to access such files forcing you to provide 777 permission.Please at any case don't give 777 permission if so please revert it back.So run Magento commands as Magento user.

1
0

i solved in this way for CentOS Linux 7.4.1708 on Plesk Onyx 17.8.11

find . -type f -exec chmod 644 {} \; &&  find . -type d -exec chmod 755 {} \; && find ./var -type d -exec chmod 777 {} \; && find ./pub/media -type d -exec chmod 777 {} \; && find ./pub/static -type d -exec chmod 777 {} \; && chmod 777 ./app/etc  && chmod 644 ./app/etc/*.xml

chown -R :<web server group> .
1
  • You should avoid using 777 on files and directories. It opens your shop to unnecessary security risk. Instead, read the docs provided by magento for the version your running. Jun 5 '18 at 13:55
0

If you face any issue like 500 internal server error then you need to check the following things:

  1. First, you need to check your server log, and if you find and error then first try to solve that.

  2. Check your .htaccess file.

  3. Please check your index.php file permission. If it's 0664 then Change index.php file permissions from 0664 to 0644

  4. Check your file and folder permissions. Make sure it's proper as per the Magento 2 requirement.

I Hope, this information helps someone.

0

Usually it's file ownership and not file permissions I find.

File permissions I use:

find . -type f \-exec chmod 644 {} \;
find . -type d \-exec chmod 755 {} \; 
find . -type f -name "*.php" \-exec chmod 600 {} \;
chmod 600 app/etc/*.xml
chmod u+x bin/magento

Different hosts though may have slightly different specs, first lines file permission 664 and 775 may be fine too. Avoid 777/666 like the plague! Lines 3 and 4 tighten it up a little.

Check what user:group owns the files, run on the folder containing the magento installation:

ls -la

results will show you file permissions, user and group along with other info, including the user and group of the magento installation folder itself (file listed as . at the top).

Use below to change the user:group as required (if in doubt, consult host):

chown -R user:group .

As others have said, refer to your Magento versions documentation.

If you still problems, you are likely going to need to consult your host, the tracing goes on deep, run below in the Magento install folder which checks which user is running apache processes, if you've got this far, it's likely to give you a clue and your host if something's out

ps -ef | egrep '(httpd|apache2|apache)' | grep -v `whoami` | grep -v root | head -n1 | awk '{print $1}'

Usually its the same user as the folder above magento's install, but at this stage it's really hosting environment dependant, dig into stack exchange https://serverfault.com/! I've had sleepless nights on Magento 2 permissions and ownership, hate it, I do hope they introduce a more friendly solution to this persistent problem not common on many other platforms.

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