Create Apache 2 vhost
#!/bin/bash
# The scipt automates the proccess of creation of virtual hosts on Apache 2 web server
# NOTE:
# 1. The script requires root previleges
# 2. The script allowes .htaccess override by default
# 3. The v-host directory will be dedecated to user and group www-data
# 4. The script edits /etc/hosts
# USAGE:
# 1. Run the scrip as root: sudo ./create_vhost.sh
# 2. Enter the virtual host name: If you enter "example.local" it will be accessable at http://example.local
# @author Samuil Banti
# @copyright (C) 2015 - Samuil Banti
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Configurations
web_dir="/var/www/"
sites_available="/etc/apache2/sites-available/"
sites_enabled="/etc/apache2/sites-enabled"
# Request root privileges
[ "$(whoami)" != "root" ] && exec sudo -- "$0" "$@"
# Request v-host name
echo "Enter the new virtual host name: "
read virtual_domain_name
# Validate the v-host name
if [ -d "$web_dir$virtual_domain_name" ]; then
echo "Directory '$web_dir$virtual_domain_name already' exists!"
exit
fi
if [ -f "$sites_available$virtual_domain_name.conf" ]; then
echo "File '$sites_available$virtual_domain_name' already exists!"
exit
fi
#Create v-host config file
echo "<VirtualHost *:80>" >> $sites_available$virtual_domain_name.conf
echo "DocumentRoot $web_dir$virtual_domain_name" >> $sites_available$virtual_domain_name.conf
echo "ServerName $virtual_domain_name" >> $sites_available$virtual_domain_name.conf
echo "ServerAlias $virtual_domain_name" >> $sites_available$virtual_domain_name.conf
echo " <Directory />" >> $sites_available$virtual_domain_name.conf
echo " Options FollowSymLinks" >> $sites_available$virtual_domain_name.conf
echo " AllowOverride All" >> $sites_available$virtual_domain_name.conf
echo " </Directory>" >> $sites_available$virtual_domain_name.conf
echo " <Directory $web_dir>" >> $sites_available$virtual_domain_name.conf
echo " Options Indexes FollowSymLinks MultiViews" >> $sites_available$virtual_domain_name.conf
echo " AllowOverride All" >> $sites_available$virtual_domain_name.conf
echo " Order allow,deny" >> $sites_available$virtual_domain_name.conf
echo " allow from all" >> $sites_available$virtual_domain_name.conf
echo " </Directory>" >> $sites_available$virtual_domain_name.conf
echo "</VirtualHost>" >> $sites_available$virtual_domain_name.conf
ln -s $sites_available$virtual_domain_name.conf $sites_enabled
# Create the root directory:
mkdir -m 777 "$web_dir$virtual_domain_name"
echo "Hello world" > "$web_dir$virtual_domain_name/index.php"
# Set the user to www-data
chown -R www-data:www-data /var/www/
# Create record in the hosts file:
echo "127.0.0.1 $virtual_domain_name" >> /etc/hosts
# Enable th virtual host
a2ensite $virtual_domain_name
/etc/init.d/apache2 restart
echo "The script was executed."
exit
Download...