Skip to content

Instantly share code, notes, and snippets.

@snapo
Created January 23, 2014 17:47
Show Gist options
  • Save snapo/8583333 to your computer and use it in GitHub Desktop.
Save snapo/8583333 to your computer and use it in GitHub Desktop.
Create Apache vhosts with default page
#! /bin/bash
#
# =======================
# MiJa Apache Vhost Script
# Vers. 0.000001pre alpha :-)
# Scripts create vhosts on a debian/ubuntu Apache installation
# Can be easy adopted to centos and suse/redhat
# Use it from shell with: ./mija.sh
# enter a domain my-new-website.com
# =======================
function make_index
{
cat <<- _EOF_
<html>
<head><title>$dname</title></head>
<body>welcome to $dname</body>
</html>
_EOF_
}
function make_vhost
{
cat <<- _EOF_
<VirtualHost *>
ServerAdmin $USER@$dname
ServerName $dname
ServerAlias *.$dname
DirectoryIndex index.html index.htm index.php
DocumentRoot /var/www/$dname/www/
<Directory /var/www/$dname/www/>
Options -Indexes -FollowSymLinks -MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/www/$dname/log/$dname-error.log
CustomLog /var/www/$dname/log/$dname-access.log combined
</VirtualHost>
_EOF_
}
# =======================
# header
# =======================
clear
echo "*** Site Setup ***"
# =======================
# set domain name variable
# =======================
echo -n "==> Enter new domain name (domain.com): "
read dname
echo "Setting up files for $dname"
# =======================
# create needed directories
# =======================
mkdir -vp /var/www/$dname/www
mkdir -vp /var/www/$dname/log
echo "" > /var/www/$dname/log/$dname-access.log
echo "created /var/www/$dname/log/$dname-access.log"
echo "" > /var/www/$dname/log/$dname-error.log
echo "created /var/www/$dname/log/$dname-error.log"
# =======================
# build index.html file
# =======================
make_index > /var/www/$dname/www/index.html
echo "created /var/www/$dname/www/index.html"
# =======================
# build vhost config file
# =======================
make_vhost > /etc/apache2/sites-available/$dname.conf
ln -s /etc/apache2/sites-available/$dname.conf /etc/apache2/sites-enabled/$dname.conf
echo "created /home/$USER/www-config/sites-available/$dname.conf"
chown -R www-data:www-data /var/www/$dname
echo "set apache owner to /var/www/$dname"
/etc/init.d/apache2 force-reload
echo "Reload Apache2 Config"
# =======================
# 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