ronisaha
8/19/2014 - 5:33 AM

Set folder permision to web-server user

Set folder permision to web-server user

#!/bin/bash

E_BAD_ARGS=85
E_BAD_PATH=-1

if [ ! -n "$1" ]
then
  echo "Usage: $0 dir1 dir2 etc."
  exit ${E_BAD_ARGS}
fi

ALL_DIRECTORIES=${@%/}

#Get the user name for webserver
HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | grep -v apache- | head -1 | cut -d\  -f1`

echo

#Make sure all directories exists
for arg in "$@"
do
    DIRECTORY_PATH=${arg%/}

    if [[ "${DIRECTORY_PATH}" = /* ]]
    then
       echo "Only relative path are allowed"
       exit ${E_BAD_PATH}
    else
       #echo "Creating : ${DIRECTORY_PATH}"
       mkdir -p "${DIRECTORY_PATH}"
    fi
done

echo "Changing permission for : ${ALL_DIRECTORIES}"

sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX ${ALL_DIRECTORIES}
sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX ${ALL_DIRECTORIES}

exit 0