Skip to content

Instantly share code, notes, and snippets.

@florentb
Created July 26, 2011 13:22
Show Gist options
  • Save florentb/1106743 to your computer and use it in GitHub Desktop.
Save florentb/1106743 to your computer and use it in GitHub Desktop.
bash script - apache, vhost, website
#!/bin/bash
VHOST_CONF=/etc/apache2/sites-available/
ROOT_UID=0
NOTROOT=87
WWW_ROOT=/home/web/
# owner of the site directory
WEBUSER=web
# check if is root
if [ "$UID" -ne "$ROOT_UID" ]
then
echo “You must be root to run this script.”
exit $NOTROOT
fi
if [ -n "$1" ]
then
DOMAIN=$1
else
echo "You must provide a full domain name for this site, i.e. ‘example.com’ "
echo -n "Run this script like ./a2createsite example.com"
exit
fi
# create document root site folder
su $WEBUSER -c "mkdir -p ${WWW_ROOT}www.$DOMAIN/httpdocs"
# vhost file content
# rewrite mod must be enabled
CONF="<VirtualHost *:80>\n\nServerName www.$DOMAIN\nServerAlias $DOMAIN\nDocumentRoot ${WWW_ROOT}www.$DOMAIN/httpdocs\n\nRewriteEngine On\nRewriteCond %{HTTP_HOST} ^$DOMAIN\nRewriteRule ^(.*)$ http://www.$DOMAIN\$1 [R=permanent,L]\n\n<Directory ${WWW_ROOT}www.$DOMAIN/httpdocs>\n\tOrder Deny,Allow\n\tAllow from all\n\tOptions -Indexes\n</Directory>\n\n</VirtualHost>"
# write the vhost config file
echo -e $CONF > ${VHOST_CONF}www.$DOMAIN
# enable site configuration
cd $VHOST_CONF
a2ensite www.$DOMAIN > /dev/null
echo "$DOMAIN was created. In 5 seconds your apache will be reloaded"
sleep 5
/etc/init.d/apache2 reload
echo "Done"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment