#!/bin/bash
[[ $# -ge 2 ]] || { echo '!!! newvhost <vhost> <user>'; exit 1; }
vhost=$1
user=$2
apache_dir="/etc/httpd/rincewind"
home_dir="/home/www"
vhost_conf="${apache_dir}/vhosts/${vhost}.conf"
vhost_link="${apache_dir}/registry/50_${vhost}.conf"
vhost_dir="${home_dir}/${vhost}"
user_vhost_dir="/home/${user}/www"
user_vhost_link="${user_vhost_dir}/${vhost}"
apache_user="http"
cat <<EOF
Will create:
- ${vhost_conf} owned by ${apache_user}:${apache_user}; u=rw,g=r,o=
- ${vhost_link} -> ${vhost_conf}
- ${vhost_dir}/{logs,htdocs} owned by ${user}:${user}; u=rwx,g=rx,o=
- ${vhost_dir}/logs/{access,error}.log owned by ${apache_user}:${user}, u=rw,g=r,o=
- ${user_vhost_dir} owned by ${user}:${user}; u=rwx,g=rx,o=
- ${user_vhost_link} -> ${vhost_dir}
Will also add "${apache_user}" user to "${user}" group and do graceful restart.
CTRL+C to abort. ENTER to proceed.
EOF
read
# sanity checks
id ${user} >/dev/null 2>&1 || { echo "!!! User ${user} does not exist."; exit 1; }
#[[ `id -u ${user}` -ge 1000 ]] || { echo " ! UID too low: `id -u ${user}` (should be >=1000)."; exit 1; }
[[ ! -d ${vhost_dir} ]] || { echo "!!! ${vhost_dir} already exists."; exit 1; }
[[ ! -f ${vhost_conf} ]] || { echo "!!! ${vhost_conf} already exists."; exit 1; }
mkdir -v -p ${vhost_dir}/{logs,htdocs}
chown -vR ${user}:${user} ${vhost_dir}
touch ${vhost_dir}/logs/{error,access}.log
chown -v ${apache_user}:${user} ${vhost_dir}/logs/*.log
chmod -vR u=rwX,g=rX,o= ${vhost_dir}
cat > ${vhost_conf} <<EOF
<VirtualHost *:80>
SuexecUserGroup ${user} ${user}
ServerName ${vhost}
DocumentRoot ${vhost_dir}/htdocs
ErrorLog ${vhost_dir}/logs/error.log
CustomLog ${vhost_dir}/logs/access.log combined
<Directory "${vhost_dir}/htdocs">
Order allow,deny
Allow from all
AllowOverride All
</Directory>
</VirtualHost>
EOF
chown -v ${apache_user}:${apache_user} ${vhost_conf}
chmod -v u=rw,g=r,o= ${vhost_conf}
ln -v -s ${vhost_conf} ${vhost_link}
mkdir -v -p ${user_vhost_dir}
ln -s -v ${vhost_dir} ${user_vhost_link}
chown -vR ${user}:${user} ${user_vhost_dir}
chmod -vR u=rwX,g=rX,o= ${user_vhost_dir}
usermod -a -G ${user} http
rc.d reload apache
echo 'Done.'