Skip to content

Instantly share code, notes, and snippets.

@bigmonmulgrew
Forked from GreenSkunk/fix-drupal8-permissions.sh
Last active December 14, 2022 12:47
Show Gist options
  • Save bigmonmulgrew/7bfc452b74c2bef10b7806efeda2bbab to your computer and use it in GitHub Desktop.
Save bigmonmulgrew/7bfc452b74c2bef10b7806efeda2bbab to your computer and use it in GitHub Desktop.
Drupal - Bash shell script to fix Drupal 8 permissions where the PHP Handler is FastCGI.
#!/bin/bash
## ######### ##
## Variables ##
## ######### ##
referrer_text="\nFor more informaiton on permissions refer to https://www.Drupal.org/node/244924\n"
drupal_path=${1%/}
drupal_user=${2}
httpd_group="${3:-www-data}"
help="\nHelp: This script is used to fix permissions of a Drupal 8 installation in which the PHP Handler is FastCGI\n"
help="${help}You need to provide the following arguments:\n"
help="${help}\t 1) Path to your drupal installation.\n"
help="${help}\t 2) Username of the drupal_user that you want to give files/directories ownership.\n"
help="${help}\t 3) HTTPD group name (defaults to www-data for Apache).\n"
help="${help}\n"
help="${help}Usage: (sudo) bash ${0##*/} --drupal_path=PATH --drupal_user=USER\n"
help="${help}Usage: (sudo) bash ${0##*/} --drupal_path=PATH --drupal_user=USER --httpd_group=GROUP\n"
help="${help}Example: (sudo) bash ${0##*/} --drupal_path=/usr/local/apache2/htdocs --drupal_user=john --httpd_group=www-data\n"
## ######### ##
## functions ##
## ######### ##
print_help() {
# Help menu
echo -e $help
exit $1
}
root_check(){
#check for root
if [ $(id -u) != 0 ]; then
printf "\n"
printf "***************************************************\n"
printf "* Error: You must run this with sudo or root *\n"
printf "***************************************************\n"
printf "\n"
print_help 1
fi
}
parse_arguments(){
# Parse Command Line Arguments
while [ "$#" -gt 0 ]; do
case "$1" in
--drupal_path=*)
drupal_path="${1#*=}"
;;
--drupal_user=*)
drupal_user="${1#*=}"
;;
--httpd_group=*)
httpd_group="${1#*=}"
;;
--help) print_help;;
*)
printf "***********************************************************\n"
printf "* Error: Invalid argument, run --help for valid arguments. *\n"
printf "***********************************************************\n"
exit 1
esac
shift
done
}
check_drupal_path(){
#Check drupal path is valid
if [ -z "${drupal_path}" ] || [ ! -d "${drupal_path}/sites" ] || [ ! -f "${drupal_path}/core/core.api.php" ]; then
echo "Please provide a valid drupal drupal_path"
print_help
fi
}
check_drupal_user(){
#Check drupal user is valid
if [ -z "${drupal_user}" ] || [ "`id -un ${drupal_user} 2> /dev/null`" != "${drupal_user}" ]; then
echo "Please provide a valid drupal_user"
print_help
fi
}
## ############### ##
## execution start ##
## ############### ##
echo -e $referrer_text
root_check
parse_arguments "$@"
check_drupal_path
check_drupal_user
cd $drupal_path;
echo -e "Changing ownership of all contents of "${drupal_path}" :\n drupal_user => "${drupal_user}" \t group => "${httpd_group}"\n"
chown -R ${drupal_user}:${httpd_group} .
echo "Changing permissions of all directories inside "${drupal_path}" to \"rwxr-xr-x\"..."
find . -type d -exec chmod u=rwx,go=rx {} \;
echo -e "Changing permissions of all files inside "${drupal_path}" to \"rw-r--r--\"...n"
find . -type f -exec chmod u=rw,go=r {} \;
cd sites ;
echo "Changing permissions of "files" directories in "${drupal_path}/sites" to \"rwxrwxr-x\"..."
find . -type d -name files -exec chmod ug=rwx,o=rx '{}' \;
echo "Changing permissions of all files inside all "files" directories in "${drupal_path}/sites" to \"rw-rw-r--\"..."
find . -name files -type d -exec find '{}' -type f \; | while read FILE; do chmod ug=rw,o=r "$FILE"; done
echo "Changing permissions of all directories inside all "files" directories in "${drupal_path}/sites" to \"rwxrwxr-x\"..."
find . -name files -type d -exec find '{}' -type d \; | while read DIR; do chmod ug=rwx,o=rx "$DIR"; done
@bigmonmulgrew
Copy link
Author

Forking to add some additional validation

@bigmonmulgrew
Copy link
Author

Added additional validation from the original script at https://www.drupal.org/node/244924

Moved some actions to functions to improve readability. Probably a bit carried away with print_help, it was longer.

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