Skip to content

Instantly share code, notes, and snippets.

@auipga
Created August 21, 2018 07:51
Show Gist options
  • Save auipga/b5fb91ac8046665869a3b4fbd64b5788 to your computer and use it in GitHub Desktop.
Save auipga/b5fb91ac8046665869a3b4fbd64b5788 to your computer and use it in GitHub Desktop.
Setting up or Fixing File Permissions (Symfony)
#!/usr/bin/env bash
# see http://symfony.com/doc/3.4/setup/file_permissions.html
function show_help
{
# General help text
cat << EOF
Script for Setting up or Fixing File Permissions for Symfony applications
Usage: sfperm <option>
Arguments:
[1|+a] : Using ACL on a system that supports chmod +a (macOS)
[2|--setfacl] : Using ACL on a system that supports setfacl (Linux/BSD)
[3|--chown]|--no-acl : chmod dirs 775, files 664 + chown to HTTPDUSER
[4|--allow-anything|--no-chown] : chmod dirs 777, files 666
[-a|--auto] : Automatically detect best method
[-h|--help] : Help text, this one more or less
EOF
}
set -e
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)
sudo=
var_dir="var web/var"
# 2. Using ACL on a System that Supports chmod +a (macOS)
# http://symfony.com/doc/3.4/setup/file_permissions.html#using-acl-on-a-system-that-supports-chmod-a-macos
function set_permissions_with_chmod_a()
{
rm -rf var/cache/* var/logs/* var/sessions/*
$sudo chmod +a "$HTTPDUSER allow delete,write,append,file_inherit,directory_inherit" ${var_dir}
$sudo chmod +a "$(whoami) allow delete,write,append,file_inherit,directory_inherit" ${var_dir}
}
# 3. Using ACL on a System that Supports setfacl (Linux/BSD)
# http://symfony.com/doc/3.4/setup/file_permissions.html#using-acl-on-a-system-that-supports-setfacl-linux-bsd
function set_permissions_with_setfacl()
{
# if this does not work, try adding '-n' option
$sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX ${var_dir}
$sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX ${var_dir}
}
function set_permissions_with_chown()
{
$sudo chown -R "$HTTPDUSER":"$HTTPDUSER" ${var_dir}
$sudo find web/var var -type d | xargs sudo chmod -R 775
$sudo find web/var var -type f | xargs sudo chmod -R 664
}
function set_permissions_with_chmod_777()
{
$sudo find web/var var -type d | xargs $sudo chmod -R 777
$sudo find web/var var -type f | xargs $sudo chmod -R 666
}
case $1 in
-a|--auto)
echo not yet implemented
exit 1
;;
1|+a)
set_permissions_with_chmod_a
exit 0
;;
2|--setfacl)
set_permissions_with_setfacl
exit 0
;;
3|--no-acl|--chown)
set_permissions_with_chown
exit 0
;;
4|--no-chown|--allow-anything)
set_permissions_with_chmod_777
exit 0
;;
-h|--help)
show_help
exit 0
;;
*)
show_help "${i}"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment