Skip to content

Instantly share code, notes, and snippets.

@mmrwoods
Last active December 30, 2015 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmrwoods/7791393 to your computer and use it in GitHub Desktop.
Save mmrwoods/7791393 to your computer and use it in GitHub Desktop.
Experimental script to fix fscked wordpress permissions
#!/bin/bash
# Experimental script to fix fscked wordpress permissions, needs testing
usage() {
echo "Usage: $0 [-u user] path-to-wordpress-install"
}
# allow user to be set via command line option
while getopts “u:v” OPTION
do
case $OPTION in
u)
user=$OPTARG
;;
?)
usage
exit
;;
esac
done
# discard extracted options from args
shift $(( OPTIND - 1 ))
path=$1
if test -z "$path"; then
usage
exit 1
elif ! test -d $path; then
echo "$0: invalid path -- '$path' is not a directory" 1>&2
usage
exit 1
elif ! test -d "$path/wp-includes"; then
echo "$0: invalid path -- '$path' does not contain a wordpress install" 1>&2
usage
exit 1
fi
# default user to owner of directory
if test -z "$user"; then
user=$(ls -ld $path | awk '{print $3}')
fi
# set group according to OS/Distro
# FIXME: this sucks, it should read from the apache config
if test $(uname) == "Darwin"; then
group="_www"
elif test -f "/etc/redhat-release"; then
group="apache"
else
group="www-data"
fi
set -x
# Reset ownership of all files and directories
chown -R $user $path
chgrp -R $group $path
# Default permissions such that files and directories only writable by user
find $path -type d -exec chmod 755 {} \;
find $path -type f -exec chmod 644 {} \;
# Make .htaccess writable by group
chmod 775 $path/.htaccess
# Make wp-content writable by group
find $path/wp-content -type d -exec chmod 775 {} \;
find $path/wp-content -type f -exec chmod 664 {} \;
# Make sure wp-content/plugins only writable by user
find $path/wp-content/plugins -type d -exec chmod 755 {} \;
find $path/wp-content/plugins -type f -exec chmod 644 {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment