Skip to content

Instantly share code, notes, and snippets.

@thoroc
Created December 14, 2015 14:06
Show Gist options
  • Save thoroc/8b161bd9a191d73a8f66 to your computer and use it in GitHub Desktop.
Save thoroc/8b161bd9a191d73a8f66 to your computer and use it in GitHub Desktop.
Bash script to automatically set the permissions for Symfony2 based on the official docs (current version 2.8)
#!/bin/bash
#
# script to automatically set the permission for the cache and logs folder
# for a new symfony2 project.
# original code taken from the documentation pages:
# http://symfony.com/doc/current/book/installation.html
BASEFOLDER="";
if [ "$1" != "" ]; then
BASEFOLDER="$1";
else
JSON=$(<composer.json); # echo "$JSON";
VERSION=$(echo $JSON|php -r 'echo json_decode(file_get_contents("php://stdin"))->require->{"symfony/symfony"};')
if [ $(echo $VERSION | cut -c 1-2) != "3" ]; then
BASEFOLDER="app"
else
BASEFOLDER="var"
fi
fi
if [ ! -d "$(pwd)/$BASEFOLDER" ]; then
echo "folder $BASEFOLDER does not exist on PATH: $(pwd)"
exit 1;
fi
## check to see if we are running as root first.
## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2;
exit 1;
fi
USER=$(whoami);
HTTPDUSER=$(ps axo user,comm | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1)
OS=$(uname -s)
CACHE="$BASEFOLDER/cache"
LOGS="$BASEFOLDER/logs"
FILENAME=$(pwd)
# Determine what the mount point for the path is:
MOUNT_POINT=$(df -P $FILENAME | tail -n 1 | awk '{print $6}')
# Get the mount options for the path:
MOUNT_OPTS=$(awk '$2=="'$MOUNT_POINT'" { print $4 }' /proc/mounts)
# Check to see if acl is one of the mount points:
#echo $MOUNT_OPTS | tr , \\\n | grep '^acl$' -q
`rm -rf var/cache/*`
`rm -rf var/logs/*`
if [ $? -eq 0 ]; then
echo "ACLs enabled"
case "$OS" in
Darwin)
echo 'Mac OS X'
chmod +a "$HTTPDUSER allow delete,write,append,file_inherit,directory_inherit" $CACHE $LOGS;
chmod +a "$USER allow delete,write,append,file_inherit,directory_inherit" $CACHE $LOGS;
;;
Linux)
echo 'Linux'
setfacl -R -m u:"$HTTPDUSER":rwX -m u:$USR:rwX $CACHE $LOGS;
setfacl -dR -m u:"$HTTPDUSER":rwX -m u:$USER:rwX $CACHE $LOGS;
;;
CYGWIN*|MINGW32*|MSYS*)
echo 'MS Windows'
;;
# Add here more strings to compare
# See https://en.wikipedia.org/wiki/Uname#Examples
*)
echo 'other OS'
;;
esac
echo "permissions for $CACHE & $LOGS successfully set."
exit 1;
else
echo "ACLs disabled"
# umask(0002)
exit 1;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment