Skip to content

Instantly share code, notes, and snippets.

@drAlberT
Last active February 3, 2016 09:43
Show Gist options
  • Save drAlberT/4686c09c8c332fa109f5 to your computer and use it in GitHub Desktop.
Save drAlberT/4686c09c8c332fa109f5 to your computer and use it in GitHub Desktop.
Simple bash script for docroot permissions setting on Linux
#!/bin/bash
#
# @author Emiliano Gabrielli <[email protected]>
# @license MIT
#
BASE_DIR="${BASE_DIR:=.}"
WRT_DIRS="${WRT_DIRS:=}"
HTTPD_USR=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1`
HTTPD_GRP=`id -ng ${HTTPD_USR}`
USR="${USR:=webmaster}"
GRP="${GRP:=$HTTPD_GRP}"
SUDO="${USESUDO//*/sudo}"
SETFACL=$(which setfacl)
if [[ ! -x "${SETFACL}" ]]; then
echo "You must have setfacl installed"
exit 127
fi
# tree-wide dir perms
${SUDO} chown -R "${USR}":"${GRP}" "${BASE_DIR}"
${SUDO} chmod -R u=rwX,g=rX,o= "${BASE_DIR}"
# httpd read only dirs
${SUDO} ${SETFACL} -nR -m u:"${USR}":rwX -m u:"${HTTPD_USR}":rX "${BASE_DIR}"
${SUDO} ${SETFACL} -d -nR -m u:"${USR}":rwX -m u:"${HTTPD_USR}":rX "${BASE_DIR}"
# httpd writeable dirs
for d in ${WRT_DIRS}
do
${SUDO} ${SETFACL} -nR -m u:"${HTTPD_USR}":rwX "${BASE_DIR}/${d}"
${SUDO} ${SETFACL} -d -nR -m u:"${HTTPD_USR}":rwX "${BASE_DIR}/${d}"
done
# vim: set ts=4 sw=4 sts=4 ai ft=sh :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment