55

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.

  • 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 – Rishabh Rk Rai Sep 1 '17 at 7:00
  • 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

12 Answers 12

105

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 web server 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 as bellow recommended.

cd <your Magento install dir> 

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

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

find ./var -type d -exec chmod 777 {} \;                // 777 permission for var folder    

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> .

chmod u+x bin/magento

I hope this will help you.

  • 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
  • 3
    I think you also need chmod u+x bin/magento to be able to execute console commands. – Volvox Aug 17 '16 at 11:08
  • 37
    friends don't let friends 777 – Darren Felton Jan 31 '17 at 0:12
  • 2
    app/etc 777> Haha nice. Hackable easy. app/etc contains your db information – CompactCode May 11 '18 at 23:07
16

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.

  • 1
    Cant find /lib/internal/Cm/Cache/Backend/File.php on M231 – snh_nl Apr 11 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. – Daniel van der Garde Apr 15 at 12:05
9

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
  • How to give write permission to generated folder in windows? @Rafael Corrêa Gomes – zus Dec 15 '18 at 10:24
7
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

  • why? (There is minimum to a comment length) – vitoriodachef Oct 18 '18 at 10:16
7

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)

5

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

4

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/
  • 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. – themanwhoknowstheman Jun 5 '18 at 13:59
4

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
  • How to give write permission to generated folder in windows? @MagenX – zus Dec 15 '18 at 10:26
3
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

  • 4
    Question calls for advise on Magento 2 – Flipmedia Dec 16 '16 at 14:18
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> .
  • 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. – themanwhoknowstheman Jun 5 '18 at 13:55
0

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.

protected by Community Nov 20 '16 at 6:20

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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