#!/bin/bash # Run this script with "(sudo) bash ". # Exit on error. set -e UNDER_PATH=${1:-`pwd`} OWNER=${2:-www-data} OWNER_GROUP=${3:-`id -gn $OWNER`} PRIVATE_DIRS="data tmp sessions" PUBLIC_DIRS="public" printf 'Create site directories in "%s" owned by "%s" with group "%s"... Is this correct? OK = cd /var/www && sudo bash %s ./mysite.com OK = sudo bash %s /var/www/mysite.com AVOID = sudo bash %s /var/www/mysite.com/site2.com & are optional, both default to www-data user/group. The parent directory must already exist, this script will NOT recursively create directories. Press ctrl+c to cancel or enter to continue...' \ "$UNDER_PATH" "$OWNER" "$OWNER_GROUP" "$0" "$0" "$0" read [[ "$UNDER_PATH" == "/" ]] && ( printf "Do you really want to create this folder in your root path? Press ctrl+c to cancel or hit enter to confirm... " \ "$UNDER_PATH" read ) [[ -z "$OWNER_GROUP" ]] && ( printf '\nNo group for user "%s"! ' "$OWNER" exit 1 ) printf 'Creating folders with user "%s" and group "%s"... ' "$OWNER" "$OWNER_GROUP" mkdir "$UNDER_PATH" cd "$UNDER_PATH" mkdir ".test" chown "$OWNER":"$OWNER_GROUP" .test || ( printf 'Failed change permissions of test folder :(. -> Check the user and/or group exist! -> You may need to be root or use sudo to run this script. ' exit 1 ) [[ -d ".test" ]] && rm -R ".test" # Create the private & public folders then set permissions... for private_folder in $PRIVATE_DIRS; do mkdir -v "$private_folder" chown -v "$OWNER":"$OWNER_GROUP" "$private_folder" chmod -cR 750 "$private_folder" chmod -cR u+s,g+s,o+s "$private_folder" done for public_folder in $PUBLIC_DIRS; do mkdir -v "$public_folder" chown -v "$OWNER":"$OWNER_GROUP" "$public_folder" chmod -cR 755 "$public_folder" chmod -cR u+s,g+s,o+s "$public_folder" done