Skip to content

Instantly share code, notes, and snippets.

@pateketrueke
Last active December 27, 2015 17:59
Show Gist options
  • Save pateketrueke/7366588 to your computer and use it in GitHub Desktop.
Save pateketrueke/7366588 to your computer and use it in GitHub Desktop.
Adds and removes custom vhosts on Apache2
#!/bin/sh
MYSELF="$(whoami)"
WWWDIR="$HOME/www"
DOMAIN=$1
VHOSTDIR="/usr/local/apache2/conf/vhosts"
if [ "$MYSELF" != "root" ]; then
echo "You must execute this as root"
else
if [ "$HOME" = "/root" ]; then
echo "You can't add sites as root"
exit 1
else
if [ ! -d "$VHOSTDIR" ]; then
mkdir -p "$VHOSTDIR"
fi
if [ -z "$DOMAIN" ]; then
for SUBDIR in `ls $WWWDIR | grep '\.'`; do
ARG1="$(basename $SUBDIR)"
echo "$($0 $ARG1)"
done
else
printf "\rChecking $DOMAIN ... "
if [ ! -d "$WWWDIR/$DOMAIN" ]; then
echo "FAIL (missing directory)"
exit 1
fi
if [ -f "$WWWDIR/$DOMAIN/vhost.conf" ]; then
cp "$WWWDIR/$DOMAIN/vhost.conf" "$VHOSTDIR/$DOMAIN.conf"
else
CONF="$(cat <<EOF
<VirtualHost *:80>
ServerName $DOMAIN
ServerAlias *.$DOMAIN
DocumentRoot $WWWDIR/$DOMAIN
<Directory $WWWDIR/$DOMAIN>
Options -Indexes
AllowOverride all
Require all granted
</Directory>
</VirtualHost>
EOF
)"
echo "$CONF" > "$VHOSTDIR/$DOMAIN.conf"
fi
apachectl restart
echo "OK"
fi
fi
fi
#!/bin/sh
MYSELF="$(whoami)"
WWWDIR="$HOME/www"
DOMAIN=$1
VHOSTDIR="/usr/local/apache2/conf/vhosts"
if [ "$MYSELF" != "root" ]; then
echo "You must execute this as root"
else
if [ "$HOME" = "/root" ]; then
echo "You can't remove sites as root"
exit 1
else
if [ -z "$DOMAIN" ]; then
echo "You must specify a domain"
exit 1
else
printf "\rRemoving $DOMAIN ... "
if [ ! -f "$VHOSTDIR/$DOMAIN.conf" ]; then
echo "FAIL (missing vhost)"
exit 1
fi
rm "$VHOSTDIR/$DOMAIN.conf"
apachectl restart
echo "OK"
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment