Skip to content

Instantly share code, notes, and snippets.

@rotemreiss
Forked from GreenSkunk/fix-drupal8-permissions.sh
Last active March 20, 2020 13:31
Show Gist options
  • Save rotemreiss/9e9bb150b6822c629f1512833d0b3f2d to your computer and use it in GitHub Desktop.
Save rotemreiss/9e9bb150b6822c629f1512833d0b3f2d 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
path=${1%/}
user=${2}
group=${3}
help="\nThis script is used to fix permissions of a Drupal 8 installation.\nYou need to provide the following arguments:\n\t 1) Path to your drupal installation\n\t 2) Username of the user that you want to give files/directories ownership\n\nUsage: bash ${0##*/} drupal_path user_name\n"
echo "Refer to https://www.Drupal.org/node/244924"
# Validate the given path
if [ -z "${path}" ] || [ ! -d "${path}/sites" ] || [ ! -f "${path}/core/core.api.php" ]; then
echo "Please provide a valid drupal path"
echo -e $help
exit
fi
# Validate the user name
if [ -z "${user}" ] || [ "`id -un ${user} 2> /dev/null`" != "${user}" ]; then
echo "Please provide a valid user"
echo -e $help
exit
fi
# Validate the gropu name
if [ -z "${group}" ] || [ "`id -gn ${group} 2> /dev/null`" != "${group}" ]; then
echo "Please provide a valid group"
echo -e $help
exit
fi
cd $path;
echo -e "Changing ownership of all contents of "${path}" :\n user => "${user}" \t group => "${group}"\n"
chown -R ${user}:${group} .
echo "Changing permissions of all directories inside "${path}" to "750"..."
find . -type d -exec chmod u=rwx,g=rx,o= '{}' \;
echo -e "Changing permissions of all files inside "${path}" to "640"..."
find . -type f -exec chmod u=rw,g=r,o= '{}' \;
cd sites
echo "Changing permissions of "files" directories in "${path}/sites" to "770"..."
find . -type d -name files -exec chmod ug=rwx,o= '{}' \;
echo "Changing permissions of all directories inside all "files" directories in "${path}/sites" to "770"..."
find . -name files -type d -exec find '{}' -type d \; | while read DIR; do chmod ug=rwx,o= "$DIR"; done
echo "Changing permissions of all files inside all "files" directories in "${path}/sites" to "660"..."
find . -name files -type d -exec find '{}' -type f \; | while read FILE; do chmod ug=rw,o= "$FILE"; done
# Hardening of settings files - note that we are also matching settings.local.php / services.local.yml
echo "Changing permissions of settings files in "${path}/sites" to "440"..."
find . -type f -name "settings*.php" -exec chmod ug=r,o= {} \;
find . -type f -name "services*.yml" -exec chmod ug=r,o= {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment