Skip to content

Instantly share code, notes, and snippets.

@macbleser
Created February 21, 2014 15:37
Show Gist options
  • Save macbleser/9136424 to your computer and use it in GitHub Desktop.
Save macbleser/9136424 to your computer and use it in GitHub Desktop.
WordPress Permissions Configuration Script
#!/bin/bash
#
# This script configures WordPress file permissions based on recommendations
# from http://codex.wordpress.org/Hardening_WordPress#File_permissions
#
# Author: Michael Conigliaro
#
WP_OWNER=changeme # <-- wordpress owner
WP_GROUP=changeme # <-- wordpress group
WP_ROOT=/home/changeme # <-- wordpress root directory
WS_GROUP=changeme # <-- 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 .htaccess
touch ${WP_ROOT}/.htaccess
chgrp ${WS_GROUP} ${WP_ROOT}/.htaccess
chmod 664 ${WP_ROOT}/.htaccess
# 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 {} \;
@yupthatguy
Copy link

Hi there,

I just tried to run the latest version of the script above on my Ubuntu 18.04 LAMP dev environment and I got the following errors:

Fixing permissions on .
Wordpress owner.group: 1000.1000
Web Server group: nobody
reset to safe defaults
find: missing argument to `-exec'
find: missing argument to `-exec'
find: missing argument to `-exec'
allow wordpress to manage wp-config.php (but prevent world access)
chgrp: invalid group: ‘nobody’
allow wordpress to manage .htaccess
chgrp: invalid group: ‘nobody’
allow wordpress to manage wp-content
find: missing argument to `-exec'
find: missing argument to `-exec'
find: missing argument to `-exec'

I also made a post on wordpress.stackexchange.com regarding the original script at the top of this thread.
https://wordpress.stackexchange.com/questions/372449/permissions-script-not-working

@libre
Copy link

libre commented Oct 21, 2020

Warning Broken system
Small modification for the command line use with option.
On the other hand, important modification related to security: I add this condition because if the folder does not exist, you completely block your system until you can no longer start! (cron: "root" account)

#!/bin/bash
#
# This script configures WordPress file permissions based on recommendations
# from http://codex.wordpress.org/Hardening_WordPress#File_permissions
#
# Author: Michael Conigliaro
#
WP_OWNER=$1 # <-- wordpress owner
WP_GROUP=$2 # <-- wordpress group
WP_ROOT=$4 # <-- wordpress root directory
WS_GROUP=$3 # <-- webserver group

# Check value option
#
if [ -z ${1} ] || [ -z ${2} ] || [ -z ${3} ] || [ -z ${4} ] ; then
echo "Error, option not found"
echo "usage: $0 OWNER GROUP WEBSERVGROUP FOLDER"
echo "ex: $0 www-data www-data www-data /var/www/html"
echo ""
exit 1
fi

#I add this condition because if the folder does not exist, you completely block your system until you can no longer start.!
#I wanted to share the experience with you because I had the case with a cron job which blocked a system because the folder no longer ``existed ....
if [ ! -d ${WP_ROOT} ]; then
echo "STOP, The folder Wordpress not found !"
echo "Please check the path"
exit 1
fi

# 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 .htaccess
touch ${WP_ROOT}/.htaccess
chgrp ${WS_GROUP} ${WP_ROOT}/.htaccess
chmod 664 ${WP_ROOT}/.htaccess

# 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 {} \;
exit 0

@phance
Copy link

phance commented Nov 2, 2020

I have made a few additions:

  1. Allowed parameters to be added as the script is called so standard usage would be for example:
[scriptname] /var/www/sitename username groupname

I always set the groupname to that of the webserver (nginx/apache) which as far as I know is secure but let me know if you disagree

  1. Added SE Linux context changes using chcon. Mine are standard Redhat / Centos contexts which you may need to alter

  2. Speeded up the script by applying standard file permissions with chmod recursively (no find necessary) and then altering directory permissions using find and chmod as before for directories. Doing this means it executes 10-20x faster on large sites.

***** WARNING - I've only tested this on a couple of live sites so far with no issues it's been fine. Posting this in case it's useful but please do your own testing and be careful before using it in production or cronjobs. The original script by Michael is probably safer until this one has been tested more! **********

#!/bin/bash
#
# This script configures WordPress file permissions based on recommendations
# from http://codex.wordpress.org/Hardening_WordPress#File_permissions
#
# Author: Michael Conigliaro with tweaks to make it faster and SE Linux by Jonathan Gittos
#
# check if this script has been called with a first parameter and use it as the wordpress root directory
[  -z "$1" ] && WP_ROOT=$PWD || WP_ROOT=$1
# check owner and group of wp-config and use later to give same to rest of WP DIR unless parameter 2 or 3 set
[  -z "$2" ] && WP_OWNER="$(ls -ld $WP_ROOT/wp-config.php | awk '{print $3}')" || WP_OWNER=$2
[  -z "$3" ] && WP_GROUP="$(ls -ld $WP_ROOT/wp-config.php | awk '{print $4}')" || WP_GROUP=$3

# Make sure we're in what looks like a WP directory to avoid big snafus

if [ ! -f $WP_ROOT/wp-config.php ]; then
    echo ""
    echo "--------------------------------------------"
    echo "This doesn't look like a Wordpress directory!"
    echo "Current or chosen directory is " $WP_ROOT
    echo "Maybe change directory, or specify a directory as a parameter to this script and try again?"
exit

fi

echo -e "\nAbout to reset permissions on:\n " $WP_ROOT
echo "Changing owner and group to:" $WP_OWNER $WP_GROUP

read -p "Does that look right? " -n 1 -r
echo

if [[ $REPLY =~ ^[Yy]$ ]]; then

    echo -e "\nRestoring permissions on: " $WP_ROOT

# reset to safe defaults
chown -R ${WP_OWNER}:${WP_GROUP} $WP_ROOT;
chmod -R 644 $WP_ROOT;
chcon -R system_u:object_r:httpd_sys_content_t:s0 $WP_ROOT;
find ${WP_ROOT} -type d -exec chmod -R 755 {} \;

# allow wordpress to manage wp-config.php (but prevent world access)
chgrp ${WP_GROUP} ${WP_ROOT}/wp-config.php
chmod 660 ${WP_ROOT}/wp-config.php
chcon system_u:object_r:httpd_sys_rw_content_t:s0 ${WP_ROOT}/wp-config.php

# allow wordpress to manage .htaccess
touch ${WP_ROOT}/.htaccess
chgrp ${WP_GROUP} ${WP_ROOT}/.htaccess
chmod 664 ${WP_ROOT}/.htaccess
chcon system_u:object_r:httpd_sys_rw_content_t:s0 ${WP_ROOT}/.htaccess

# allow wordpress to manage wp-content
chmod -R 664 $WP_ROOT/wp-content;
chcon -R system_u:object_r:httpd_sys_rw_content_t:s0 $WP_ROOT/wp-content;
find ${WP_ROOT}/wp-content -type d -exec chmod -R 775 {} \;
echo -e "\nDoing it\n"

fi

@juanpgarciac
Copy link

If you are having "No such file or directory" problems and you use windows to upload the file a quick fix is changing the format to unix again:

#vim fix_permision.sh
:set fileformat=unix
:wq

#sh fix_permision.sh

@progonkpa
Copy link

progonkpa commented Feb 14, 2022

I'm on docker and had permission issues when trying to update or install a plugin.
I used this script with this config and want to leave some pointer as this issue might not be obvious to others, especially WS_GROUP. At least, it wasn't for me.

WP_OWNER=my_user_on_host_machine
WP_GROUP=my_user_group_on_host_machine # same name as previous in my case
WP_ROOT=/path/to/wordpress/src
WS_GROUP=http # docker web user on host machine

http is the apache process created by Docker in my host. I can see http in ps aux when the Docker WordPress container is running.

Curiously however, when I go inside the Docker container running WordPress (guest), I see the web user is www-data. The WordPress image I'm running is wordpress:5.4-php7.3-apache.

At any rate, my permission issues went away with the config above, with http.

@ErvinSabic
Copy link

The number of times I have come back to this page is insane lol.

@MassStash
Copy link

MassStash commented Jul 25, 2022

Landed here to fix a perm issue, so not sure if 1) I messed up script somehow haha, absolutely possible, 2) think the mess up may be still trying to wrap my head around litespeed entirely! Anyone have more wisdom than I? Seems nobody is WS_GROUP as default script has, but ran script and still issues with images not showing, but after script site has critical error... Setting back to basic 755 / 644 brings it back, but things are still not loading and running strange. I has suspicion it is a secret "easy" perms thing though since this is all after fooling around migrating sites somewhat.... 🤦 Any insights would helpful!

Thanks for the work on the script, and the comments everyone!

Addon: Oh yea, other issue I believe, is the .htaccess couldn't be accessed by litespeed... So setting chgrp on .htaccess to WS not good fro litespeed? Or I possible have wrong WS group... right?

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