Created
July 26, 2011 13:22
-
-
Save florentb/1106743 to your computer and use it in GitHub Desktop.
bash script - apache, vhost, website
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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