Create a gist now

Instantly share code, notes, and snippets.

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 commented Jun 8, 2014

thanks! :)

tcope25 commented Jul 23, 2014

this worked perfect for me. Thanks!

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

@ghost

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

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 commented Nov 3, 2015

This works link a charm. Thank for the resource.

alagu commented Apr 10, 2016

Worked like magic.

malhal commented May 14, 2016

Fixed all my problems thanks

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.

thanks

h3nr1ke commented Aug 22, 2016

thanks man..

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

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 commented Sep 28, 2016

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

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

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.

#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 commented Mar 28, 2017 edited

@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 commented May 6, 2017

thanks!

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

daudi250 commented Jun 13, 2017 edited

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

Works great! Thank you :D

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