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
  • Select an option

  • Save bigmonmulgrew/7bfc452b74c2bef10b7806efeda2bbab to your computer and use it in GitHub Desktop.

Select an option

Save bigmonmulgrew/7bfc452b74c2bef10b7806efeda2bbab to your computer and use it in GitHub Desktop.

Revisions

  1. bigmonmulgrew revised this gist Dec 14, 2022. 1 changed file with 100 additions and 23 deletions.
    123 changes: 100 additions & 23 deletions fix-drupal8-permissions.sh
    Original file line number Diff line number Diff line change
    @@ -1,39 +1,116 @@
    #!/bin/bash

    path=${1%/}
    user=${2}
    group=${2}
    ## ######### ##
    ## Variables ##
    ## ######### ##

    help="\nHelp: This script is used to fix permissions of a Drupal 8 installation in which the PHP Handler is FastCGI\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\nNote: This script assumes that the group is the same as the username if your PHP is compiled to run as FastCGI. If this is different you need to modify it manually by editing this script or checking out forks on GitHub.\nUsage: bash ${0##*/} drupal_path user_name\n"
    referrer_text="\nFor more informaiton on permissions refer to https://www.Drupal.org/node/244924\n"

    echo "Refer to https://www.Drupal.org/node/244924"
    drupal_path=${1%/}
    drupal_user=${2}
    httpd_group="${3:-www-data}"

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

    if [ -z "${user}" ] || [ "`id -un ${user} 2> /dev/null`" != "${user}" ]; then
    echo "Please provide a valid user"
    echo -e $help
    exit
    fi
    ## ######### ##
    ## functions ##
    ## ######### ##

    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 "755"..."
    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 "${path}" to "644"...n"
    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 "${path}/sites" to "775"..."
    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 "${path}/sites" to "664"..."
    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 "${path}/sites" to "775"..."
    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


  2. @GreenSkunk GreenSkunk revised this gist Nov 28, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fix-drupal8-permissions.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ path=${1%/}
    user=${2}
    group=${2}

    help="\nHelp: This script is used to fix permissions of a drupal installation in which the PHP Handler is FastCGI\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\nNote: This script assumes that the group is the same as the username if your PHP is compiled to run as FastCGI. If this is different you need to modify it manually by editing this script or checking out forks on GitHub.\nUsage: bash ${0##*/} drupal_path user_name\n"
    help="\nHelp: This script is used to fix permissions of a Drupal 8 installation in which the PHP Handler is FastCGI\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\nNote: This script assumes that the group is the same as the username if your PHP is compiled to run as FastCGI. If this is different you need to modify it manually by editing this script or checking out forks on GitHub.\nUsage: bash ${0##*/} drupal_path user_name\n"

    echo "Refer to https://www.Drupal.org/node/244924"

  3. @GreenSkunk GreenSkunk revised this gist Nov 3, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fix-drupal8-permissions.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ path=${1%/}
    user=${2}
    group=${2}

    help="\nHelp: This script is used to fix permissions of a drupal 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\nNote: This script assumes that the group is the same as the username if your PHP is compiled to run as FastCGI. If this is different you need to modify it manually by editing this script or checking out forks on GitHub.\nUsage: bash ${0##*/} drupal_path user_name\n"
    help="\nHelp: This script is used to fix permissions of a drupal installation in which the PHP Handler is FastCGI\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\nNote: This script assumes that the group is the same as the username if your PHP is compiled to run as FastCGI. If this is different you need to modify it manually by editing this script or checking out forks on GitHub.\nUsage: bash ${0##*/} drupal_path user_name\n"

    echo "Refer to https://www.Drupal.org/node/244924"

  4. @GreenSkunk GreenSkunk revised this gist Nov 3, 2016. 1 changed file with 14 additions and 14 deletions.
    28 changes: 14 additions & 14 deletions fix-drupal8-permissions.sh
    Original file line number Diff line number Diff line change
    @@ -4,36 +4,36 @@ path=${1%/}
    user=${2}
    group=${2}

    help="\nHelp: This script is used to fix permissions of a drupal installation\nyou need to provide the following arguments:nt 1) Path to your drupal installationnt 2) Username of the user that you want to give files/directories ownershipn\nNote: This script assumes that the group is the same as the username if your PHP is compiled to run as FastCGI. If this is different you need to modify it manually by editing this script or checking out forks on GitHub\nUsage: bash ${0##*/} drupal_path user_name\n"
    help="\nHelp: This script is used to fix permissions of a drupal 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\nNote: This script assumes that the group is the same as the username if your PHP is compiled to run as FastCGI. If this is different you need to modify it manually by editing this script or checking out forks on GitHub.\nUsage: bash ${0##*/} drupal_path user_name\n"

    echo "Refer to https://www.Drupal.org/node/244924"

    if [ -z "${path}" ] || [ ! -d "${path}/sites" ] || [ ! -f "${path}/core/core.api.php" ]; then
    echo "Please provide a valid drupal path"
    echo -e $help
    exit
    echo "Please provide a valid drupal path"
    echo -e $help
    exit
    fi

    if [ -z "${user}" ] || [ "`id -un ${user} 2> /dev/null`" != "${user}" ]; then
    echo "Please provide a valid user"
    echo -e $help
    exit
    echo "Please provide a valid user"
    echo -e $help
    exit
    fi

    cd $path;

    echo -e "Changing ownership of all contents of "${path}" :n user => "${user}" t group => "${group}"n"
    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 "755"..."
    find . -type d -exec chmod u=rwx,go=rx {} ;
    find . -type d -exec chmod u=rwx,go=rx {} \;
    echo -e "Changing permissions of all files inside "${path}" to "644"...n"
    find . -type f -exec chmod u=rw,go=r {} ;
    find . -type f -exec chmod u=rw,go=r {} \;

    cd $path/sites;
    cd sites ;

    echo "Changing permissions of "files" directories in "${path}/sites" to "775"..."
    find . -type d -name files -exec chmod ug=rwx,o=rx '{}' ;
    find . -type d -name files -exec chmod ug=rwx,o=rx '{}' \;
    echo "Changing permissions of all files inside all "files" directories in "${path}/sites" to "664"..."
    find . -name files -type d -exec find '{}' -type f ; | while read FILE; do chmod ug=rw,o=r "$FILE"; done
    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 "${path}/sites" to "775"..."
    find . -name files -type d -exec find '{}' -type d ; | while read DIR; do chmod ug=rwx,o=rx "$DIR"; done
    find . -name files -type d -exec find '{}' -type d \; | while read DIR; do chmod ug=rwx,o=rx "$DIR"; done
  5. @GreenSkunk GreenSkunk revised this gist Nov 1, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions fix-drupal8-permissions.sh
    Original file line number Diff line number Diff line change
    @@ -35,5 +35,5 @@ echo "Changing permissions of "files" directories in "${path}/sites" to "775"...
    find . -type d -name files -exec chmod ug=rwx,o=rx '{}' ;
    echo "Changing permissions of all files inside all "files" directories in "${path}/sites" to "664"..."
    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 "${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 directories inside all "files" directories in "${path}/sites" to "775"..."
    find . -name files -type d -exec find '{}' -type d ; | while read DIR; do chmod ug=rwx,o=rx "$DIR"; done
  6. @GreenSkunk GreenSkunk renamed this gist Nov 1, 2016. 1 changed file with 14 additions and 11 deletions.
    25 changes: 14 additions & 11 deletions fix-drupal-permissions.sh → fix-drupal8-permissions.sh
    Original file line number Diff line number Diff line change
    @@ -2,10 +2,13 @@

    path=${1%/}
    user=${2}
    group="www-data"
    help="nHelp: This script is used to fix permissions of a drupal installationnyou need to provide the following arguments:nt 1) Path to your drupal installationnt 2) Username of the user that you want to give files/directories ownershipnNote: "www-data" (apache default) is assumed as the group the server is belonging to, if this is different you need to modify it manually by editing this scriptnnUsage: (sudo) bash ${0##*/} drupal_path user_namen"
    group=${2}

    if [ -z "${path}" ] || [ ! -d "${path}/sites" ] || [ ! -f "${path}/modules/system/system.module" ]; then
    help="\nHelp: This script is used to fix permissions of a drupal installation\nyou need to provide the following arguments:nt 1) Path to your drupal installationnt 2) Username of the user that you want to give files/directories ownershipn\nNote: This script assumes that the group is the same as the username if your PHP is compiled to run as FastCGI. If this is different you need to modify it manually by editing this script or checking out forks on GitHub\nUsage: bash ${0##*/} drupal_path user_name\n"

    echo "Refer to https://www.Drupal.org/node/244924"

    if [ -z "${path}" ] || [ ! -d "${path}/sites" ] || [ ! -f "${path}/core/core.api.php" ]; then
    echo "Please provide a valid drupal path"
    echo -e $help
    exit
    @@ -21,16 +24,16 @@ 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"...n"
    find . -type f -exec chmod u=rw,g=r,o= {} ;
    echo "Changing permissions of all directories inside "${path}" to "755"..."
    find . -type d -exec chmod u=rwx,go=rx {} ;
    echo -e "Changing permissions of all files inside "${path}" to "644"...n"
    find . -type f -exec chmod u=rw,go=r {} ;

    cd $path/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 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
    echo "Changing permissions of "files" directories in "${path}/sites" to "775"..."
    find . -type d -name files -exec chmod ug=rwx,o=rx '{}' ;
    echo "Changing permissions of all files inside all "files" directories in "${path}/sites" to "664"..."
    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 "${path}/sites" to "770"..."
    find . -name files -type d -exec find '{}' -type d ; | while read DIR; do chmod ug=rwx,o= "$DIR"; done
  7. @owzXEi owzXEi created this gist Apr 3, 2012.
    36 changes: 36 additions & 0 deletions fix-drupal-permissions.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #!/bin/bash

    path=${1%/}
    user=${2}
    group="www-data"
    help="nHelp: This script is used to fix permissions of a drupal installationnyou need to provide the following arguments:nt 1) Path to your drupal installationnt 2) Username of the user that you want to give files/directories ownershipnNote: "www-data" (apache default) is assumed as the group the server is belonging to, if this is different you need to modify it manually by editing this scriptnnUsage: (sudo) bash ${0##*/} drupal_path user_namen"

    if [ -z "${path}" ] || [ ! -d "${path}/sites" ] || [ ! -f "${path}/modules/system/system.module" ]; then
    echo "Please provide a valid drupal path"
    echo -e $help
    exit
    fi

    if [ -z "${user}" ] || [ "`id -un ${user} 2> /dev/null`" != "${user}" ]; then
    echo "Please provide a valid user"
    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"...n"
    find . -type f -exec chmod u=rw,g=r,o= {} ;

    cd $path/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 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
    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