Skip to content

Instantly share code, notes, and snippets.

@benjamin-dk
Last active December 18, 2015 01:29
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 benjamin-dk/5704488 to your computer and use it in GitHub Desktop.
Save benjamin-dk/5704488 to your computer and use it in GitHub Desktop.
Linux bash script for setting up a virtual host on an Apache web server
#! /bin/bash
#
# =======================
# Siteup Script 0.1
# Written by Command Line Idiot
# http://commandlineidiot.com
# You may use, modify, and redistribute this script freely
# Released: August 2007
# =======================
# =======================
# set functions
# =======================
# make_index is the function to create a basic index.html file
# within the documents directory of the new domain. The variable
# for domain name is passed into the file at $dname. You can alter
# any of the code between the terms _EOF_ and it will be reflected
# in the index.html file.
# I have outcommented this function... /Benjamin
# function make_index
# {
# cat <<- _EOF_
# <html>
# <head><title>$dname</title></head>
# <body>welcome to $dname</body>
# </html>
# _EOF_
# }
# make_vhost is the function to create a config file that
# Apache2 can interpret. The variable for the domain name is passed
# into the file at $dname, and the system-wide variable for username
# is passed into the file at $usname. You may wish to replace the
# ServerAdmin email address with your own email address. You may alter
# any of the code between the terms _EOF_ to build your own preferred
# standard config file.
# NOTE: modified by benjamin@benway.dk feb. 2012
function make_vhost
{
sudo cat <<- _EOF_
<VirtualHost *>
ServerAdmin $usname@localhost
ServerName $dname
ServerAlias *.$dname
DirectoryIndex index.html index.htm index.php
DocumentRoot /home/$usname/workspace/web/$dname/
<Directory /home/$usname/workspace/web/$dname>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog /home/$usname/workspace/web/logs/$dname/error.log
CustomLog /home/$usname/workspace/web/logs/$dname/access.log combined
</VirtualHost>
_EOF_
}
# =======================
# header
# =======================
clear
echo "*** Site Setup ***"
echo "Be sure that the file paths and vhost details in this script are matching the ones on your system!"
echo "If you're not sure, press CTRL-C to abort and inspect the bash script and modify it if needed."
# =======================
# set domain name variable and get user names
# =======================
echo -n "==> Enter new domain name (e.g. domain.com): "
read dname
echo -n "==> Enter the name of the main user (typically your name): "
read usname
echo "You entered $usname as the main user name"
echo -n "==> Enter the name of the webserver user (typically www-data): "
read webusname
echo "You entered $webusname as the webserver user name"
echo "Do you want to create a new directory for the web files? Y/N"
read newwebdir
echo "Setting up files for $dname"
# =======================
# create needed directories
# =======================
if [ "$newwebdir" == "Y" ]
then
mkdir -vp /home/$usname/workspace/web/$dname
fi
mkdir -vp /home/$usname/workspace/web/logs/$dname
touch /home/$usname/workspace/web/logs/$dname/access.log
echo "created /home/$usname/workspace/web/logs/$dname/access.log"
touch /home/$usname/workspace/web/logs/$dname/error.log
echo "created /home/$usname/workspace/web/logs/$dname/error.log"
chown $usname:$webusname /home/$usname/workspace/web/logs/$dname/*
chown $usname:$webusname /home/$usname/workspace/web/$dname
# =======================
# build index.html file
# =======================
# make_index > /home/$usname/$dname/index.html
# echo "created /home/$usname/$dname/index.html"
# =======================
# build vhost config file
# =======================
make_vhost > /etc/apache2/sites-available/$dname
/usr/sbin/a2ensite $dname
echo "created and activated /etc/apache2/sites-available/$dname"
# =======================
# exit
# =======================
echo "*** Finished setting up files for $dname. Goodbye!"
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment