Скриптик чтоб новые виртуальные хосты создавать в apache2
#!/bin/bash
# Script to add virtual host in apache2
# ---------- CONSTANTS -----------------
# apache path
apache_path="/etc/apache2/"
# virtual hosts directories path
vhdirpath="/home/anton/htdocs"
availabledir="sites-available"
enableddir="sites-enabled"
myAskYN()
{
local AMSURE
if [ -n "$1" ] ; then
read -n 1 -p "$1 (y/[a]): " AMSURE
else
read -n 1 AMSURE
fi
echo "" 1>&2
if [ "$AMSURE" = "y" ] ; then
return 0
else
return 1
fi
}
RestartServer()
{
# restart apache
echo "Restart apache2"
service apache2 reload
}
#control existing virtual hosts directory
HostDirControl ()
{
if [ -d $vhdirpath ]; then
echo "Virtual hosts directory exists"
else
echo "Virtual hosts directory not exists"
mkdir $vhdirpath
chmod +x $vhdirpath
echo "Virtual hosts directory created"
fi
}
# Create new virtual host
MakeHost ()
{
# config file content
echo "<VirtualHost *:80>
ServerAdmin webmaster@$1
ServerName $1
DocumentRoot $2/$1
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory $2/$1>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog $2/$1/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog $2/$1/access.log combined
</VirtualHost>" > "$apache_path$availabledir/$1"
echo "Configuration file in '$apache_path$availabledir' created"
# make virtual host directory id not exists
if [ -d "$2/$1"]; then
echo "Virtual host directory allready exists"
else
mkdir "$2/$1"
chmod 777 -R "$2/$1"
echo "Virtual host directory created in '$2/$1'"
fi
# set the virtual host directory rights to 777
a2ensite $1
echo "New virtualhost file link created"
# insert new virtual host into /etc/hosts
# get the content of hosts file
hostscontent=`cat /etc/hosts`
# output the content into the hosts
echo "127.0.0.1 $1
$hostscontent" > "/etc/hosts"
RestartServer
}
# Remove virtual host
DeleteHost ()
{
myAskYN "Do you realy want to remove virtual host $1?" || exit
rm "$apache_path$availabledir/$1"
rm "$apache_path$enableddir/$1"
echo "Virtual host $1 successfully removed"
myAskYN "Do you want to remove virtual host scripts directory $1?" || exit
rm "$vhdirpath/$1"
echo "Scripts directory of virtual host $1 successfully removeds"
RestartServer
}
# Update virtual host pool
PoolUpdate()
{
# watching in host directory
for vhdir in "$vhdirpath/*"
do
if [ -d "$vhdirpath/$vhdir" ]
then
if [ -f "$apache_path$availabledir/$vhdir" && -f "$apache_path$enableddir/$vhdir" ]
echo "virtualhost $vhdir is active"
then
MakeHost $vhdir
fi
fi
done
}
# modes
if [ "$1" == "new" ]; then
echo $1
if [ -n $2 ]; then
HostDirControl
MakeHost $2 $vhdirpath
else
echo "You must enter the url of new virtual host"
fi
elif [ "$1" == "drop" ]; then
HostDirControl
DeleteHost $2
elif [ "$1" == "update" ]; then
HostDirControl
PoolUpdate
else
echo "vhost v.1.0
Virtual hosts manager for Apache2
Synthax:
vhost new <hostname> - create new virtual host with adress <hostname>
vhost drop <hostname> - remove virtual host with adress <hostname>
vhost update - automaticly remove or create virtual host if threre is in a host directory
(c) magnuz 2013
"
fi
Работает примерно так
sudo vhost new test.ru
опции - new drop и update. Опция new работает полностью, drop пока не удаляет из hosts, update тоже пока не доделана.