Instantly share code, notes, and snippets.

Embed
What would you like to do?
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.

timbru31 commented Jun 8, 2014

thanks! :)

@tcope25

This comment has been minimized.

tcope25 commented Jul 23, 2014

this worked perfect for me. Thanks!

@solarjohn

This comment has been minimized.

solarjohn commented Jan 28, 2015

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.

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.

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.

smuralii commented Nov 3, 2015

This works link a charm. Thank for the resource.

@alagu

This comment has been minimized.

alagu commented Apr 10, 2016

Worked like magic.

@malhal

This comment has been minimized.

malhal commented May 14, 2016

Fixed all my problems thanks

@ghost

This comment has been minimized.

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

@jimmyhuynz

This comment has been minimized.

jimmyhuynz commented Jun 13, 2016

thanks

@h3nr1ke

This comment has been minimized.

h3nr1ke commented Aug 22, 2016

thanks man..

@johnfelipe

This comment has been minimized.

johnfelipe commented 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
@DevelopIntelligenceBoulder

This comment has been minimized.

DevelopIntelligenceBoulder commented 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
@malhal

This comment has been minimized.

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.

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.

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.

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

@pankaj9296

This comment has been minimized.

pankaj9296 commented 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)

@richb-hanover

This comment has been minimized.

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.

boaglio commented May 6, 2017

thanks!

@CallMeJess

This comment has been minimized.

CallMeJess commented May 25, 2017

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

@daudi250

This comment has been minimized.

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.

briann-cs commented Jul 20, 2017

Works great! Thank you :D

@neel92

This comment has been minimized.

neel92 commented Sep 27, 2017

It's work. Thanks !!

@PabloWestphalen

This comment has been minimized.

PabloWestphalen commented Oct 12, 2017

i love this and use it frequently, you rock

@fsspencer

This comment has been minimized.

fsspencer commented Oct 24, 2017

Very helpful! Thanks

@tonicastillo

This comment has been minimized.

tonicastillo commented 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

@ptykamikaze

This comment has been minimized.

ptykamikaze commented Nov 24, 2017

Thanks a lot!

@bossfight

This comment has been minimized.

bossfight commented Dec 23, 2017

nice work

@dmarcelino

This comment has been minimized.

dmarcelino commented Jan 13, 2018

Thank you! I only regret not finding this earlier.

@Mythwrestler

This comment has been minimized.

Mythwrestler commented Jan 20, 2018

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

@tschabet

This comment has been minimized.

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.

g30h commented Mar 16, 2018

Thanks it worked!

@chrislovessushi

This comment has been minimized.

chrislovessushi commented Apr 6, 2018

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

@starsky135

This comment has been minimized.

starsky135 commented 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!

@409industries

This comment has been minimized.

409industries commented 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!

@gjongenelen

This comment has been minimized.

gjongenelen commented 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} {} \;

?

@miguelmx

This comment has been minimized.

miguelmx commented Jun 1, 2018

It worked great! thank you!

@msesxi

This comment has been minimized.

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.

kcwebguy commented Jun 11, 2018

Very helpful. I especially appreciate daudi250's comments.

@ovflowd

This comment has been minimized.

ovflowd commented Jun 15, 2018

That saves lifes and files haha.

@xlucasfelix

This comment has been minimized.

xlucasfelix commented Jun 27, 2018

This saved me from a tiring job! Thanks! @Adirael and @daudi250

@shakee93

This comment has been minimized.

shakee93 commented Jul 3, 2018

if thee .sh file is in root directory

run

 sudo sh ./fix-wordpress-permissions.sh ./
@DogByteMarketing

This comment has been minimized.

DogByteMarketing commented Jul 7, 2018

Following daudi250 I had to change around based on CentOS usage

WP_OWNER=cpanelusername # <-- wordpress owner
WP_GROUP=nobody # <-- wordpress group
WP_ROOT=$1 # <-- wordpress root directory
WS_GROUP=nobody # <-- webserver group

However it still did not fix my permission issues. I don't understand how my permissions got messed up, was updating fine, haven't installed any plugins, then today I couldn't do it. I had to run:

find . -type f -print -exec chmod 777 {} \;
find . -type d -print -exec chmod 777 {} \;

Perform the update

then revert permissions back

find . -type f -print -exec chmod 644 {} \;
find . -type d -print -exec chmod 755 {} \;
@devzons

This comment has been minimized.

devzons commented Jul 17, 2018

Thx it works for me.

@rickslayer

This comment has been minimized.

rickslayer commented Jul 28, 2018

thanks man. save my day ;)

@tasaduq

This comment has been minimized.

tasaduq commented Oct 22, 2018

thanks! perfect way to restore messed up permissions.

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