Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Setup Magento permissions/ownership following the recommendations of the official article : http://devdocs.magento.com/guides/m1x/install/installer-privileges_after.html
#!/bin/bash
#Check root access
if [ "$UID" != 0 ]
then
echo "This shell script must be run as root."
exit 2
fi
#Variables declaration
GROUP_ACCESS=
WEB_USER=
WEB_GROUP=
ROOT_DIR='.' #Web root directory
VAR_DIR="$ROOT_DIR/var/"
MEDIA_DIR="$ROOT_DIR/media/"
INCLUDES_DIR="$ROOT_DIR/includes"
if [ -n "$1" ] && [ -d "$1" ]
then
ROOT_DIR="$1"
fi
#Functions declaration
check_magento_dir()
{
if [ ! -f "$1/app/Mage.php" ]
then
error 'Magento installation directory not found!'
fi
}
error()
{
echo "$@" 1>&2
exit 1
}
warning()
{
echo "$@" 1>&2
}
set_user_perms()
{
find "$ROOT_DIR" \( -path "$VAR_DIR" -o -path "$MEDIA_DIR" -o -path "$INCLUDE_DIR" \) -type d \! -perm 500 -exec chmod 500 {} \;
find "$ROOT_DIR" -type f \! -perm 400 -exec chmod 400 {} \;
find "$VAR_DIR" -type d \! -perm 700 -exec chmod 700 {} \;
find "$VAR_DIR" -type f \! -perm 600 -exec chmod 600 {} \;
find "$MEDIA_DIR" -type d \! -perm 700 -exec chmod 700 {} \;
find "$MEDIA_DIR" -type f \! -perm 600 -exec chmod 600 {} \;
if [ -e "$INCLUDES_DIR" ] && [ $(stat -c "%a" "$INCLUDES_DIR") != 700 ]
then
chmod 700 "$INCLUDES_DIR"
fi
if [ -e "$INCLUDES_DIR/config.php" ] && [ $(stat -c "%a" "$INCLUDES_DIR/config.php") != 600 ]
then
chmod 600 "$INCLUDES_DIR/config.php"
fi
}
set_user_and_group_perms()
{
find "$ROOT_DIR" \( -path "$VAR_DIR" -o -path "$MEDIA_DIR" -o -path "$INCLUDE_DIR" \) -type d \! -perm 550 -exec chmod 550 {} \;
find "$ROOT_DIR" -type f \! -perm 440 -exec chmod 440 {} \;
find "$VAR_DIR" -type d \! -perm 770 -exec chmod 770 {} \;
find "$VAR_DIR" -type f \! -perm 660 -exec chmod 660 {} \;
find "$MEDIA_DIR" -type d \! -perm 770 -exec chmod 770 {} \;
find "$MEDIA_DIR" -type f \! -perm 660 -exec chmod 660 {} \;
if [ -e "$INCLUDES_DIR" ] && [ $(stat -c "%a" "$INCLUDES_DIR") != 770 ]
then
chmod 770 "$INCLUDES_DIR"
fi
if [ -e "$INCLUDES_DIR/config.php" ] && [ $(stat -c "%a" "$INCLUDES_DIR/config.php") != 660 ]
then
chmod 660 "$INCLUDES_DIR/config.php"
fi
}
chown_magento_dir()
{
#if [ $(stat -c "%U" "$ROOT_DIR") != "$WEB_USER" ] && [ $(stat -c "%G" "$ROOT_DIR") != "$WEB_GROUP" ]
#then
chown -R "$WEB_USER:$WEB_GROUP" "$ROOT_DIR"
#fi
}
#Main
check_magento_dir "$ROOT_DIR"
while true
do
read -p "Enter a valid Web User account: " user
if id -u "$user" > /dev/null 2>&1
then
WEB_USER=$user
break;
else
warning "No such user: $user"
fi
done
while true
do
read -p "Enter a valid Web Group account: " group
if getent group "$group" > /dev/null 2>&1
then
WEB_GROUP=$group
break;
else
warning "No such group: $group"
fi
done
read -r -p "Allow group access ? [y/N] " answer
if [[ $answer =~ ^([yY][eE][sS]|[yY])$ ]]
then
GROUP_ACCESS=true
fi
echo -n "Changing ownership of : $ROOT_DIR ..."
chown_magento_dir
echo "Done!"
echo -n "Setting permissions for : $ROOT_DIR ..."
if [ -z $GROUP_ACCESS ]
then
set_user_perms
else
set_user_and_group_perms
fi
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.