Skip to content

Instantly share code, notes, and snippets.

@jamesog
Created October 23, 2016 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesog/573fbfff1056adee5fb77f8ab2eb3504 to your computer and use it in GitHub Desktop.
Save jamesog/573fbfff1056adee5fb77f8ab2eb3504 to your computer and use it in GitHub Desktop.
siteadmin.sh - ancient Apache vhost management script
#!/bin/sh
# siteadmin.sh
# Created 2005/01/07 by James O'Gorman <james@netinertia.co.uk>
#
# This script automatically creates directory structures and config file
# entries needed for a new website.
# It can also be used to clean up (remove) those entries when a website is
# no longer needed.
#
# Current website structure is as follows:
# Create /home/$user/www - this is where all domain sites will be stored
# Under $HOME/www create the top-level domain directory (e.g. example.com)
# Under TLD, create the subdomain, followed by the htdocs and logs directories
# Create a directory entry for the top-level domain in /www/sites/$domain
# Symlink the subdomain from the user's wwwdir to /www/sites/$domain - this is
# in case different users have different subdomains under the same domain.
#
# A default Apache vhost entry is added to the named config file. Alterations to
# this vhost entry can be made if necessary, it won't affect the removal part of
# the script, so long as the # Begin and # End lines are still there.
cfgfile=/usr/local/etc/apache22/Includes/vhosts.conf
wwwroot=/www/sites
#userwwwroot=/home/owner/www # Defined below
usage () {
cat << EOF
Usage: $0 [-o <owner>] [-d <domain>] [-s <subdomain>] [-p <port>] [-D]
<owner> UserID of the site owner (must be in /etc/passwd)
<domain> Top level of site domain (e.g. example.com)
<subdomain> Subdomain (e.g. www, secure)
<port> Port the website should listen on; usually 80 or 443
-D Delete a site
EOF
exit
}
checkuser () {
pw=`pw usershow $1`
return $?
}
rmsite () {
sed -e "/# Begin ${subdomain}.${domain}/,/# End ${subdomain}.${domain}/ d" \
-i.bak $cfgfile
read -p "Remove all site directories and links? " yesno
case $yesno in
[yY]|[yY][eE][sS])
echo Removing ${userwwwroot}/${domain}/${subdomain}
rm -rf ${userwwwroot}/${domain}/${subdomain}
if [ x`ls ${userwwwroot}/${domain}` = "x" ]; then
echo ..Removing empty directory ${userwwwroot}/${domain}
rmdir ${userwwwroot}/${domain}
fi
echo Removing ${wwwroot}/${domain}/${subdomain}
rm ${wwwroot}/${domain}/${subdomain}
if [ x`ls ${wwwroot}/${domain}` = "x" ]; then
echo ..Removing empty directory ${wwwroot}/${domain}
rmdir ${wwwroot}/${domain}
fi
;;
*)
echo Site directories left intact
;;
esac
exit
}
while getopts o:d:s:p:Dh args
do
case $args in
o)
owner=$OPTARG ;;
d)
domain=$OPTARG ;;
s)
subdomain=$OPTARG ;;
p)
port=$OPTARG ;;
D)
delsite=yes ;;
h)
usage ;;
esac
done
[ -z $owner ] && read -p "Owner: " owner
if ! checkuser $owner; then
exit 67
fi
[ -z $domain ] && read -p "Domain: " domain
[ -z $subdomain ] && read -p "Subdomain: " subdomain
: ${port:="80"}
userwwwroot=/home/${owner}/www
# Check if we're deleting an existing site
[ x$delsite = "xyes" ] && rmsite
# Create a site if we're not deleting
mkdir -m 0775 -p ${userwwwroot}/${domain}/${subdomain}/htdocs
mkdir -m 0775 -p ${userwwwroot}/${domain}/${subdomain}/logs
chown -R ${owner}:wwwadmins ${userwwwroot}
mkdir -m 0775 -p ${wwwroot}/${domain}
chgrp wwwadmins ${wwwroot}/${domain}
ln -s ${userwwwroot}/${domain}/${subdomain} ${wwwroot}/${domain}
cat << EOF >> $cfgfile
# Begin ${subdomain}.${domain}
<VirtualHost *:${port}>
ServerName ${subdomain}.${domain}
ServerAdmin webmaster@${domain}
DocumentRoot ${wwwroot}/${domain}/${subdomain}/htdocs
ErrorLog ${wwwroot}/${domain}/${subdomain}/logs/error_log
CustomLog "|/usr/local/sbin/cronolog -S ${wwwroot}/${domain}/${subdomain}/logs/access_log ${wwwroot}/${domain}/${subdomain}/logs/access_log.%Y%m%d" combined
</VirtualHost>
# End ${subdomain}.${domain}
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment