Created
June 4, 2012 03:42
-
-
Save gizzmo/2866207 to your computer and use it in GitHub Desktop.
script to ease creating virtualhosts on ubuntu.
This file contains hidden or 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 | |
#================================================================================ | |
# virtualhost | |
# | |
# A fancy little script to setup a new virtualhost in Ubuntu based upon the | |
# excellent virtualhost (V1.04) script by Patrick Gibson <[email protected]> for OS X. | |
# | |
# This script has been tested on Ubuntu 7.10 (Gutsy Gibbon) with Apache2(!) and | |
# probably works on Debian as well, but this has not been tested (yet). If you use | |
# this script on other Linux distributions and can confirm it to work I would like to hear | |
# from you. Just send an email to Bjorn Wijers <[email protected]> with more info | |
# | |
# USAGE: | |
# | |
# CREATE A VIRTUAL HOST: | |
# sudo ./virtualhost <name> | |
# where <name> is the one-word name you'd like to use. (e.g. mysite) | |
# | |
# Note that if "virtualhost" is not in your PATH, you will have to write | |
# out the full path to where you've placed: eg. /usr/bin/virtualhost <name> | |
# | |
# REMOVE A VIRTUAL HOST: | |
# sudo ./virtualhost --delete <site> | |
# | |
# where <site> is the site name you used when you first created the host. | |
# | |
# | |
#======= SCRIPT VARIABLES ======== | |
# | |
# By default, this script places files in /home/[you]/Projects. If you would like | |
# to change this, like to how Apache on Ubuntu does things by default, uncomment the | |
# following line: | |
# | |
#DOC_ROOT_PREFIX="/var/www" | |
# | |
# Set the virtual host configuration directory | |
# | |
APACHE_VIRTUAL_HOSTS_AVAILABLE="/etc/apache2/sites-available" | |
# | |
# | |
#======= DO NOT EDIT BELOW THIS lINE UNLESS YOU KNOW WHAT YOU ARE DOING ======== | |
if [ `whoami` != 'root' ]; then | |
echo "You must be running with root privileges to run this script." | |
exit | |
fi | |
if [ -z $USER -o $USER = "root" ]; then | |
if [ ! -z $SUDO_USER ]; then | |
USER=$SUDO_USER | |
else | |
echo "ALERT! Your root shell did not provide your username." | |
exit | |
fi | |
fi | |
if [ -z $DOC_ROOT_PREFIX ]; then | |
DOC_ROOT_PREFIX="/home/$USER/Projects" | |
fi | |
usage() | |
{ | |
cat <<__EOT | |
Usage: sudo virtualhost <name> | |
sudo virtualhost --help | |
sudo virtualhost --list | |
sudo virtualhost --delete <name> | |
Where <name> is the name fof the host. (e.g. mysite.dev) | |
__EOT | |
exit 1 | |
} | |
if [ -z $1 ]; then | |
usage | |
else | |
if [ $1 = '--help' -o $1 = 'help' ]; then | |
usage | |
elif [ $1 = '--list' ]; then | |
LIST=0 | |
elif [ $1 = "--delete" ]; then | |
if [ -z $2 ]; then | |
usage | |
else | |
VIRTUALHOST=$2 | |
DELETE=0 | |
fi | |
else | |
VIRTUALHOST=$1 | |
fi | |
fi | |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
# List virtualhosts installed | |
# | |
if [ ! -z $LIST ]; then | |
echo "Listing virtualhosts found in $APACHE_VIRTUAL_HOSTS_AVAILABLE/" | |
for i in $APACHE_VIRTUAL_HOSTS_AVAILABLE/*; do | |
name=${i:${#APACHE_VIRTUAL_HOSTS_AVAILABLE}+1} | |
doc_root=`grep DocumentRoot $i | awk '{print $2}'` | |
echo "$name" | |
echo "${doc_root}" | |
echo | |
done | |
exit 1 | |
fi | |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
# Delete the virtualhost if that's the requested action | |
# | |
if [ ! -z $DELETE ]; then | |
if [ -e $APACHE_VIRTUAL_HOSTS_AVAILABLE/$VIRTUALHOST ]; then | |
# Confirm the action | |
echo "Deleting virtualhost, \"$VIRTUALHOST\". THIS CANNOT BE UNDONE!" | |
echo -n " ... Continue? [y/n]: " | |
read continue | |
case $continue in | |
n*|N*) exit | |
esac | |
# Find project home | |
DOCUMENT_ROOT=`grep DocumentRoot $APACHE_VIRTUAL_HOSTS_AVAILABLE/$VIRTUALHOST | awk '{print substr($2, 0, length($2)-6)}'` | |
if [ -d $DOCUMENT_ROOT ]; then | |
echo -n "- Delete virtualhost folder '$DOCUMENT_ROOT'? [y/n]: " | |
read resp | |
case $resp in | |
y*|Y*) | |
echo -n " - Deleting folder... " | |
if rm -rf $DOCUMENT_ROOT ; then | |
echo "done" | |
else | |
echo "Could not delete $DOCUMENT_ROOT" | |
fi | |
;; | |
n*|N*) | |
echo " - Folder not deleted." | |
esac | |
else | |
echo "- Unable to find Document Root." | |
fi | |
echo -n "- Deleting virtualhost file... " | |
a2dissite $VIRTUALHOST 1>/dev/null 2>/dev/null | |
rm $APACHE_VIRTUAL_HOSTS_AVAILABLE/$VIRTUALHOST | |
echo "done" | |
echo -n "- Restarting Apache... " | |
apachectl graceful 1>/dev/null 2>/dev/null | |
echo "done" | |
else | |
echo "The virtualhost '$VIRTUALHOST' does not exist." | |
fi | |
exit | |
fi | |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
# Test that the virtualhost name is valid (starts with a number or letter) | |
# | |
if ! echo $VIRTUALHOST | grep -q -E '^[A-Za-z0-9]+' ; then | |
echo "Sorry, '$VIRTUALHOST' is not a valid host name to use. It must start with a letter or number." | |
exit 1 | |
fi | |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
# Check for already existing virtual host | |
# | |
if [ -e $APACHE_VIRTUAL_HOSTS_AVAILABLE/$VIRTUALHOST ]; then | |
echo -n "The virtualhost \"$VIRTUALHOST\" already exists. Do you want to replace this configuration? [y/n]: " | |
read resp | |
case $resp in | |
n*|N*) exit | |
;; | |
esac | |
fi | |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
# Check for already existing folder | |
# | |
if [ -d $DOC_ROOT_PREFIX/$VIRTUALHOST ]; then | |
echo -n "\"$DOC_ROOT_PREFIX/$VIRTUALHOST\" already exists, Use it as your DocumentRoot? [y/n]: " | |
else | |
echo -n "Use \"$DOC_ROOT_PREFIX/$VIRTUALHOST\" as your DocumentRoot? [y/n]: " | |
fi | |
read resp | |
case $resp in | |
n*|N*) | |
while : ; do | |
if [ -z $FOLDER ]; then | |
echo -n "- Enter new folder name (located in $DOC_ROOT_PREFIX): " | |
read FOLDER | |
else | |
break | |
fi | |
done | |
;; | |
*) FOLDER=$VIRTUALHOST | |
;; | |
esac | |
# Create the base folder structure | |
echo -n "Creating base folder structure... " | |
su $USER -c " | |
mkdir -p $DOC_ROOT_PREFIX/$FOLDER/public" | |
echo "done" | |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
# Create a default index.html if there isn't already one there | |
# | |
if [ ! -e $DOC_ROOT_PREFIX/$FOLDER/public/index.html -a ! -e $DOC_ROOT_PREFIX/$FOLDER/public/index.php ]; then | |
cat <<__EOL >$DOC_ROOT_PREFIX/$FOLDER/public/index.html | |
<!doctype html> | |
<title>Welcome to $VIRTUALHOST</title> | |
<style> | |
p { margin-bottom: 5px; } | |
pre { margin: 0 20px; } | |
</style> | |
<h1>Congratulations!</h1> | |
<p>If you are reading this in your web browser, then the only logical conclusion is that the <b><a href="http://$VIRTUALHOST/">http://$VIRTUALHOST/</a></b> virtualhost was setup correctly. :)</p> | |
<p>You can find the configuration file for this virtual host in:</p> | |
<pre>$APACHE_VIRTUAL_HOSTS_AVAILABLE/$VIRTUALHOST</pre> | |
<p>You will need to place all of your website files in:</p> | |
<pre>$DOC_ROOT_PREFIX/$FOLDER</pre> | |
__EOL | |
chown $USER:$USER $DOC_ROOT_PREFIX/$FOLDER/public/index.html | |
fi | |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
# Create a default virtualhost file | |
# | |
echo -n "Creating virtualhost file... " | |
cat <<__EOF >$APACHE_VIRTUAL_HOSTS_AVAILABLE/$VIRTUALHOST | |
<VirtualHost *:80> | |
ServerName $VIRTUALHOST | |
#ServerAlias *.$VIRTUALHOST | |
ServerAdmin webmaster@$VIRTUALHOST | |
DocumentRoot $DOC_ROOT_PREFIX/$FOLDER/public | |
<Directory $DOC_ROOT_PREFIX/$FOLDER/public/> | |
AllowOverride All | |
Order allow,deny | |
Allow from all | |
</Directory> | |
# | |
# LogLevel: Control the number of messages logged to the error_log. | |
# Possible values include: debug, info, notice, warn, error, crit, | |
# alert, emerg. | |
# | |
Loglevel warn | |
ErrorLog \${APACHE_LOG_DIR}/$FOLDER-error.log | |
CustomLog \${APACHE_LOG_DIR}/$FOLDER-access.log combined | |
</VirtualHost> | |
__EOF | |
chown $USER:$USER $APACHE_VIRTUAL_HOSTS_AVAILABLE/$VIRTUALHOST | |
# Enable the virtual host | |
a2ensite $VIRTUALHOST 1>/dev/null 2>/dev/null | |
echo "done" | |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
# Restart apache for the changes to take effect | |
# | |
echo -n "Restarting Apache... " | |
apache2ctl graceful 1>/dev/null 2>/dev/null | |
echo "done" | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment