Create a gist now

Instantly share code, notes, and snippets.

Embed
Fix wordpress file permissions
#!/bin/bash
#
# This script configures WordPress file permissions based on recommendations
# from http://codex.wordpress.org/Hardening_WordPress#File_permissions
#
# Author: Michael Conigliaro <mike [at] conigliaro [dot] org>
#
WP_OWNER=www-data # <-- wordpress owner
WP_GROUP=www-data # <-- wordpress group
WP_ROOT=$1 # <-- wordpress root directory
WS_GROUP=www-data # <-- webserver group
# reset to safe defaults
find ${WP_ROOT} -exec chown ${WP_OWNER}:${WP_GROUP} {} \;
find ${WP_ROOT} -type d -exec chmod 755 {} \;
find ${WP_ROOT} -type f -exec chmod 644 {} \;
# allow wordpress to manage wp-config.php (but prevent world access)
chgrp ${WS_GROUP} ${WP_ROOT}/wp-config.php
chmod 660 ${WP_ROOT}/wp-config.php
# allow wordpress to manage wp-content
find ${WP_ROOT}/wp-content -exec chgrp ${WS_GROUP} {} \;
find ${WP_ROOT}/wp-content -type d -exec chmod 775 {} \;
find ${WP_ROOT}/wp-content -type f -exec chmod 664 {} \;
@timbru31

This comment has been minimized.

Show comment
Hide comment
@timbru31

timbru31 Jun 8, 2014

thanks! :)

timbru31 commented Jun 8, 2014

thanks! :)

@tcope25

This comment has been minimized.

Show comment
Hide comment
@tcope25

tcope25 Jul 23, 2014

this worked perfect for me. Thanks!

tcope25 commented Jul 23, 2014

this worked perfect for me. Thanks!

@solarjohn

This comment has been minimized.

Show comment
Hide comment
@solarjohn

solarjohn Jan 28, 2015

This saved me! thank you! my site was totally offline for some reason and this solved it. double thumbs up

This saved me! thank you! my site was totally offline for some reason and this solved it. double thumbs up

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Jun 3, 2015

So, I used this because my wp-content/uploads was giving me permission errors (on a new install).
It works now, however since the data is now "owned" by www-data, i can no longer edit files via ftp.
I tried adding the user that I use to ftp to the www-data group, but that doesn't seem to work. Any thoughts?

ghost commented Jun 3, 2015

So, I used this because my wp-content/uploads was giving me permission errors (on a new install).
It works now, however since the data is now "owned" by www-data, i can no longer edit files via ftp.
I tried adding the user that I use to ftp to the www-data group, but that doesn't seem to work. Any thoughts?

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Jun 3, 2015

https://codex.wordpress.org/Changing_File_Permissions

Typically, all files should be owned by your user (ftp) account on your web server, and should be writable by that account. On shared hosts, files should never be owned by the webserver process itself (sometimes this is www, or apache, or nobody user).

ghost commented Jun 3, 2015

https://codex.wordpress.org/Changing_File_Permissions

Typically, all files should be owned by your user (ftp) account on your web server, and should be writable by that account. On shared hosts, files should never be owned by the webserver process itself (sometimes this is www, or apache, or nobody user).

@smuralii

This comment has been minimized.

Show comment
Hide comment
@smuralii

smuralii Nov 3, 2015

This works link a charm. Thank for the resource.

smuralii commented Nov 3, 2015

This works link a charm. Thank for the resource.

@alagu

This comment has been minimized.

Show comment
Hide comment
@alagu

alagu Apr 10, 2016

Worked like magic.

alagu commented Apr 10, 2016

Worked like magic.

@malhal

This comment has been minimized.

Show comment
Hide comment
@malhal

malhal May 14, 2016

Fixed all my problems thanks

malhal commented May 14, 2016

Fixed all my problems thanks

@erickacevedor

This comment has been minimized.

Show comment
Hide comment
@erickacevedor

erickacevedor May 24, 2016

I'm sorry, but i dont know how this works, someone can explain it pls? I've got a problem w/ wordpress permissions and this seems to be a solution.

I'm sorry, but i dont know how this works, someone can explain it pls? I've got a problem w/ wordpress permissions and this seems to be a solution.

@jimmyhuynz

This comment has been minimized.

Show comment
Hide comment
@jimmyhuynz

jimmyhuynz Jun 13, 2016

thanks

thanks

@h3nr1ke

This comment has been minimized.

Show comment
Hide comment
@h3nr1ke

h3nr1ke Aug 22, 2016

thanks man..

h3nr1ke commented Aug 22, 2016

thanks man..

@johnfelipe

This comment has been minimized.

Show comment
Hide comment
@johnfelipe

johnfelipe Aug 25, 2016

hi bro, why show me this:

felipe@felipeurrego:/var/www/html$ sudo ./fix-wordpress-permissions.sh
chgrp: cannot access ‘/wp-config.php’: No such file or directory
chmod: cannot access ‘/wp-config.php’: No such file or directory
find: `/wp-content': No such file or directory
find: `/wp-content': No such file or directory
find: `/wp-content': No such file or directory

hi bro, why show me this:

felipe@felipeurrego:/var/www/html$ sudo ./fix-wordpress-permissions.sh
chgrp: cannot access ‘/wp-config.php’: No such file or directory
chmod: cannot access ‘/wp-config.php’: No such file or directory
find: `/wp-content': No such file or directory
find: `/wp-content': No such file or directory
find: `/wp-content': No such file or directory
@DevelopIntelligenceBoulder

This comment has been minimized.

Show comment
Hide comment
@DevelopIntelligenceBoulder

DevelopIntelligenceBoulder Sep 13, 2016

Also getting the


find: `/wp-content': No such file or directory
find: `/wp-content': No such file or directory
find: `/wp-content': No such file or directory

Also getting the


find: `/wp-content': No such file or directory
find: `/wp-content': No such file or directory
find: `/wp-content': No such file or directory
@malhal

This comment has been minimized.

Show comment
Hide comment
@malhal

malhal Sep 28, 2016

You need to supply the folder your wordpress is in, e.g. ./fix-wordpress-permissions.sh wordpress

malhal commented Sep 28, 2016

You need to supply the folder your wordpress is in, e.g. ./fix-wordpress-permissions.sh wordpress

@NatiNog

This comment has been minimized.

Show comment
Hide comment
@NatiNog

NatiNog Oct 18, 2016

Thank you!!!
I was wondering if you could tell me why I'm getting this error when trying to update the core:
"The update cannot be installed because we will be unable to copy some files. This is usually due to inconsistent file permissions.: wp-admin/includes/update-core.php"

NatiNog commented Oct 18, 2016

Thank you!!!
I was wondering if you could tell me why I'm getting this error when trying to update the core:
"The update cannot be installed because we will be unable to copy some files. This is usually due to inconsistent file permissions.: wp-admin/includes/update-core.php"

@kikoking

This comment has been minimized.

Show comment
Hide comment
@kikoking

kikoking Feb 5, 2017

NaitNog - "You need to supply the folder your wordpress is in, e.g. ./fix-wordpress-permissions.sh wordpress". I get a permission denied when trying to run this. Also just wondering, is it necessary to set this? Is this something that should be set when installing wordpress out of the box every time? I'm just wondering because I had a few permissions issues after installing wordpress with wget in terminal, but it was resolved by running chown -R ftpusername:ftpusername *

kikoking commented Feb 5, 2017

NaitNog - "You need to supply the folder your wordpress is in, e.g. ./fix-wordpress-permissions.sh wordpress". I get a permission denied when trying to run this. Also just wondering, is it necessary to set this? Is this something that should be set when installing wordpress out of the box every time? I'm just wondering because I had a few permissions issues after installing wordpress with wget in terminal, but it was resolved by running chown -R ftpusername:ftpusername *

@primedime

This comment has been minimized.

Show comment
Hide comment
@primedime

primedime Mar 10, 2017

find: `/wp-content': No such file or directory

That is happening because that is not the path to your directory. You may need to add /var/www/vhosts/domainname.com/httpsdocs/wp-content/

which would be the full folder path. This example path might not be the same as yours, it is just an example. Basically you need a more precise path.

find: `/wp-content': No such file or directory

That is happening because that is not the path to your directory. You may need to add /var/www/vhosts/domainname.com/httpsdocs/wp-content/

which would be the full folder path. This example path might not be the same as yours, it is just an example. Basically you need a more precise path.

@pankaj9296

This comment has been minimized.

Show comment
Hide comment
@pankaj9296

pankaj9296 Mar 15, 2017

#create fix-wordpress-permissions.sh ("sudo nano fix-wordpress-permissions.sh" and put the content in it)
chmod +x fix-wordpress-permissions.sh (give execute permissions)
./fix-wordpress-permissions.sh . (execute)

#create fix-wordpress-permissions.sh ("sudo nano fix-wordpress-permissions.sh" and put the content in it)
chmod +x fix-wordpress-permissions.sh (give execute permissions)
./fix-wordpress-permissions.sh . (execute)

@richb-hanover

This comment has been minimized.

Show comment
Hide comment
@richb-hanover

richb-hanover Mar 28, 2017

@Adirael Thanks for this script - it worked fine! Many of the questions above would go away if you could add this explanation after line 6 of the original script:

# Source for this script: http://www.conigliaro.org/script-to-configure-proper-wordpress-permissions/
#
# To use this script, supply the full path to your wordpress directory
# That directory is often in /var/www, and the wordpress directory is frequently named 'wordpress'
# The example below operates on /var/www/wordpress directory
#
# sudo sh ./fix-wordpress-permissions.sh /var/www/wordpress

richb-hanover commented Mar 28, 2017

@Adirael Thanks for this script - it worked fine! Many of the questions above would go away if you could add this explanation after line 6 of the original script:

# Source for this script: http://www.conigliaro.org/script-to-configure-proper-wordpress-permissions/
#
# To use this script, supply the full path to your wordpress directory
# That directory is often in /var/www, and the wordpress directory is frequently named 'wordpress'
# The example below operates on /var/www/wordpress directory
#
# sudo sh ./fix-wordpress-permissions.sh /var/www/wordpress
@boaglio

This comment has been minimized.

Show comment
Hide comment
@boaglio

boaglio May 6, 2017

thanks!

boaglio commented May 6, 2017

thanks!

@CallMeJess

This comment has been minimized.

Show comment
Hide comment
@CallMeJess

CallMeJess May 25, 2017

Thank you so much! Can't believe I just found this now.

Thank you so much! Can't believe I just found this now.

@daudi250

This comment has been minimized.

Show comment
Hide comment
@daudi250

daudi250 Jun 13, 2017

the struggle for this one is real. tried so many time and failed. finally worked with the help @richb-hanover comment.

So i will explain the thing further.

  1. you need to make a .sh file.
  2. create a new file under the name of fix-wordpress-permissions and use .sh as its extension
  3. add the above content.

now comes the tricky part.

you need to upload the file, where your website or wordpress is being hosted i.e. the directory/path/wordpress for eg.

/home/admin/web/mywebsite/public_html

I am using Ubuntu 16 on amazon ec2 and i have installed VestaCP. Therefore the path of my directory is the one mentioned. You path can be

/Var/www/wesite/yourwebiste or something.

It all depends where the website is installed.

How i got here?

I used SSH to login and navigated here using CD command.

cd /home/admin/web/mywebsite/public_html.

  1. You need to upload the .sh file over here using filezilla or scp or cybercuk or whatever software you want to use..

  2. now execute the command as mentioned by @pankaj

chmod +x fix-wordpress-permissions.sh (gives execute permissions)

You need to be in the directory of your wordpress (do remember this prior executing the command)
now

sudo sh ./fix-wordpress-permissions.sh /home/admin/web/yourwebsite-goes-here/public_html (use this command to execute).

Please do note that this is my directory and your directory can be different. you need to be in the directory of your wordpress installation to execute the command.

When you press enter it will work. Wait for sometime.
screen shot 2017-06-13 at 11 19 08 pm

daudi250 commented Jun 13, 2017

the struggle for this one is real. tried so many time and failed. finally worked with the help @richb-hanover comment.

So i will explain the thing further.

  1. you need to make a .sh file.
  2. create a new file under the name of fix-wordpress-permissions and use .sh as its extension
  3. add the above content.

now comes the tricky part.

you need to upload the file, where your website or wordpress is being hosted i.e. the directory/path/wordpress for eg.

/home/admin/web/mywebsite/public_html

I am using Ubuntu 16 on amazon ec2 and i have installed VestaCP. Therefore the path of my directory is the one mentioned. You path can be

/Var/www/wesite/yourwebiste or something.

It all depends where the website is installed.

How i got here?

I used SSH to login and navigated here using CD command.

cd /home/admin/web/mywebsite/public_html.

  1. You need to upload the .sh file over here using filezilla or scp or cybercuk or whatever software you want to use..

  2. now execute the command as mentioned by @pankaj

chmod +x fix-wordpress-permissions.sh (gives execute permissions)

You need to be in the directory of your wordpress (do remember this prior executing the command)
now

sudo sh ./fix-wordpress-permissions.sh /home/admin/web/yourwebsite-goes-here/public_html (use this command to execute).

Please do note that this is my directory and your directory can be different. you need to be in the directory of your wordpress installation to execute the command.

When you press enter it will work. Wait for sometime.
screen shot 2017-06-13 at 11 19 08 pm

@briann-cs

This comment has been minimized.

Show comment
Hide comment
@briann-cs

briann-cs Jul 20, 2017

Works great! Thank you :D

Works great! Thank you :D

@neel92

This comment has been minimized.

Show comment
Hide comment
@neel92

neel92 Sep 27, 2017

It's work. Thanks !!

neel92 commented Sep 27, 2017

It's work. Thanks !!

@PabloWestphalen

This comment has been minimized.

Show comment
Hide comment
@PabloWestphalen

PabloWestphalen Oct 12, 2017

i love this and use it frequently, you rock

i love this and use it frequently, you rock

@fsspencer

This comment has been minimized.

Show comment
Hide comment
@fsspencer

fsspencer Oct 24, 2017

Very helpful! Thanks

Very helpful! Thanks

@tonicastillo

This comment has been minimized.

Show comment
Hide comment
@tonicastillo

tonicastillo Nov 23, 2017

Thanks, it works perfect, but something like this, at start, would be fine to prevent no parameter error:
# alert if no parameters if [[ $# -eq 0 ]] ; then echo 'Error. You need to spacify folder, example: ./fix-wordpress-permissions.sh public_html' exit 0 fi

Thanks, it works perfect, but something like this, at start, would be fine to prevent no parameter error:
# alert if no parameters if [[ $# -eq 0 ]] ; then echo 'Error. You need to spacify folder, example: ./fix-wordpress-permissions.sh public_html' exit 0 fi

@ptykamikaze

This comment has been minimized.

Show comment
Hide comment
@ptykamikaze

ptykamikaze Nov 24, 2017

Thanks a lot!

Thanks a lot!

@bossfight

This comment has been minimized.

Show comment
Hide comment
@bossfight

bossfight Dec 23, 2017

nice work

nice work

@dmarcelino

This comment has been minimized.

Show comment
Hide comment
@dmarcelino

dmarcelino Jan 13, 2018

Thank you! I only regret not finding this earlier.

Thank you! I only regret not finding this earlier.

@Mythwrestler

This comment has been minimized.

Show comment
Hide comment
@Mythwrestler

Mythwrestler Jan 20, 2018

Holy crap, what a simple and elegant solution! THANK YOU!!!!

Holy crap, what a simple and elegant solution! THANK YOU!!!!

@tschabet

This comment has been minimized.

Show comment
Hide comment
@tschabet

tschabet Feb 9, 2018

Great script, but it did not fix my update problems, at least not for Wordpress version updates. It is still necessary to enter FTP credentials. However it works for plugin or theme updates. Any ideas on avoiding to enter FTP data when trying to update?
I am talking about a Webserver with several websites, where customers upload their files with FTP. Ownership and Permissions should be right but it still does not work. I searched around in Google but did not find a solution for that. Why is this that complicated?

tschabet commented Feb 9, 2018

Great script, but it did not fix my update problems, at least not for Wordpress version updates. It is still necessary to enter FTP credentials. However it works for plugin or theme updates. Any ideas on avoiding to enter FTP data when trying to update?
I am talking about a Webserver with several websites, where customers upload their files with FTP. Ownership and Permissions should be right but it still does not work. I searched around in Google but did not find a solution for that. Why is this that complicated?

@g30h

This comment has been minimized.

Show comment
Hide comment
@g30h

g30h Mar 16, 2018

Thanks it worked!

g30h commented Mar 16, 2018

Thanks it worked!

@chrislovessushi

This comment has been minimized.

Show comment
Hide comment
@chrislovessushi

chrislovessushi Apr 6, 2018

Just wanted to say thanks, I use this all of the time and it's a life saver!

Just wanted to say thanks, I use this all of the time and it's a life saver!

@starsky135

This comment has been minimized.

Show comment
Hide comment
@starsky135

starsky135 Apr 7, 2018

This worked perfectly and is definitely something all WP developers should have on hand since nothing did the job as well as this. Thanks!

This worked perfectly and is definitely something all WP developers should have on hand since nothing did the job as well as this. Thanks!

@409industries

This comment has been minimized.

Show comment
Hide comment
@409industries

409industries Apr 8, 2018

Adirael-
You are a rockstar for making this, got my moved site back up and running after some file / folder permissions got whacked. Kudos to you sir. Surprised there isn't a "Fix Broken WP permissions" plugin?

@daudi250
Thanks for taking the time to explain how to properly use this script! I just needed a little guidance to actually execute the .sh and it got it all going. Cheers to you!

Adirael-
You are a rockstar for making this, got my moved site back up and running after some file / folder permissions got whacked. Kudos to you sir. Surprised there isn't a "Fix Broken WP permissions" plugin?

@daudi250
Thanks for taking the time to explain how to properly use this script! I just needed a little guidance to actually execute the .sh and it got it all going. Cheers to you!

@gjongenelen

This comment has been minimized.

Show comment
Hide comment
@gjongenelen

gjongenelen May 29, 2018

Wouldn't it be faster to use

chown -R ${WP_OWNER}:${WP_GROUP} ${WP_ROOT}

instead of

find ${WP_ROOT} -exec chown ${WP_OWNER}:${WP_GROUP} {} \;

?

Wouldn't it be faster to use

chown -R ${WP_OWNER}:${WP_GROUP} ${WP_ROOT}

instead of

find ${WP_ROOT} -exec chown ${WP_OWNER}:${WP_GROUP} {} \;

?

@miguelmx

This comment has been minimized.

Show comment
Hide comment
@miguelmx

miguelmx Jun 1, 2018

It worked great! thank you!

miguelmx commented Jun 1, 2018

It worked great! thank you!

@msesxi

This comment has been minimized.

Show comment
Hide comment
@msesxi

msesxi Jun 8, 2018

I'm using centos/cpanel/litespeed and I have the following error:
chown: invalid user: www-data:www-data
chgrp: invalid group: www-data

how should I fix it ?

msesxi commented Jun 8, 2018

I'm using centos/cpanel/litespeed and I have the following error:
chown: invalid user: www-data:www-data
chgrp: invalid group: www-data

how should I fix it ?

@kcwebguy

This comment has been minimized.

Show comment
Hide comment
@kcwebguy

kcwebguy Jun 11, 2018

Very helpful. I especially appreciate daudi250's comments.

Very helpful. I especially appreciate daudi250's comments.

@ovflowd

This comment has been minimized.

Show comment
Hide comment
@ovflowd

ovflowd Jun 15, 2018

That saves lifes and files haha.

ovflowd commented Jun 15, 2018

That saves lifes and files haha.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment