Wednesday, November 23, 2011

Apache Module mod_vhost_alias


Hello everybody,
mod_vhost_alias provides for dynamically configured mass virtual hosting. According to Apache website, http://httpd.apache.org/docs/2.0/mod/mod_vhost_alias.html, this module creates dynamically configured virtual hosts, by allowing the IP address and/or the Host: header of the HTTP request to be used as part of the pathname to determine what files to serve. This allows for easy use of a huge number of virtual hosts with similar configurations.

The following explanation shows how to configure apache with this module. Then, I wrote a script to automate creation of mass virtual hosting.

  • Untar the installation file of apache and change your directory to apache directory
  • Run this command: ./configure –prefix=$HOME/apache –enable-modules=mod_vhost_alias
  • make
  • make install

Here is the script:

#!/bin/bash
clear
echo "******************************************************"
echo "This is a script for making multiple virtual hosts."
echo "Scripted by: Khosro Taraghi"
echo "Modified date: Nov-23-2011"
echo "******************************************************"
echo ""
echo ""
reset_server=0
end_of_script=""
while ([ "$end_of_script" != "n" ] && [ "$end_of_script" != "y" ])
do
echo -n "Press y to continue or press n to exit (y/n): "
read end_of_script
if ([ "$end_of_script" != "n" ] && [ "$end_of_script" != "y" ])
then
echo "The input is incorrect!!!"
fi
done
if [ "$end_of_script" == "y" ]
then
echo -n "Please enter the path to your Server Root (Absolute path): "
read sroot
sroot=$(echo $sroot | sed 's/\/$//')
while ([ ! -d "$sroot" ])
do
echo -n "Directory $sroot does not exist!! Please try again: "
read sroot
sroot=$(echo $sroot | sed 's/\/$//')
done
echo -n "Please enter the path to your Document Root (Absolute path): "
read droot
droot=$(echo $droot | sed 's/\/$//')
while ([ ! -d "$droot" ])
do
echo -n "Directory $droot does not exist!! Please try again: "
read droot
droot=$(echo $droot | sed 's/\/$//')
done
sed -i 's/^#.*NameVirtualHost/NameVirtualHost/' $sroot/conf/httpd.conf
fi
while [ "$end_of_script" != "n" ]
do
echo -n "Please enter the name of your Server Name (for example, www.example.com): "
read sname
echo "<VirtualHost *:80>" >> $sroot/conf/httpd.conf
echo "ServerAdmin khosro@example.com" >> $sroot/conf/httpd.conf
mkdir -p $droot/$sname
echo "VirtualDocumentRoot \"$droot/%0\"" >> $sroot/conf/httpd.conf
echo "DirectoryIndex index.html" >> $sroot/conf/httpd.conf
echo "ServerName $sname" >> $sroot/conf/httpd.conf
echo "ErrorLog logs/$sname-error_log" >> $sroot/conf/httpd.conf
echo "CustomLog logs/$sname-access_log common" >> $sroot/conf/httpd.conf
alias_vhost=""
while ([ "$alias_vhost" != "n" ] && [ "$alias_vhost" != "y" ])
do
echo -n "Do you want to add an alias to another virtual host (y/n): "
read alias_vhost
if ([ "$end_of_script" != "n" ] && [ "$end_of_script" != "y" ])
then
echo "The input is incorrect!!! Enter y for yes or n for no!"
fi
done
if [ "$alias_vhost" == "y" ]
then
echo -n "Please enter the name of Server Name that you want to make an alias to $sname, (for example, www.example2.com) : "
read alias_name
echo "AliasMatch ^/(.*)$ $droot/$alias_name/index.html" >> $sroot/conf/httpd.conf
fi
redirect=""
while ([ "$redirect" != "n" ] && [ "$redirect" != "y" ])
do
echo -n "Do you want to make a redirect to another virtual host (y/n): "
read redirect
if ([ "$redirect" != "n" ] && [ "$redirect" != "y" ])
then
echo "The input is incorrect!!! Enter y for yes or n for no!"
fi
done
if [ "$redirect" == "y" ]
then
echo -n "Please enter the name of Server Name that you want to make a Redirect to $sname, (for example, www.example2.com) : "
read redirect_name
echo "RedirectMatch permanent ^/$ http://$redirect_name" >> $sroot/conf/httpd.conf
fi
echo "</VirtualHost>" >> $sroot/conf/httpd.conf
echo "<html><title>$sname</title>This is a webpage that belongs to $sname.</html>" >> $droot/$sname/index.html
echo ""
echo "The Virtual Host <$sname> was added successfully."
echo "Press any key to continue..."
read
echo "127.0.0.1 $sname" >> /etc/hosts
service NetworkManager restart
clear
reset_server=1
end_of_script=""
while ([ "$end_of_script" != "n" ] && [ "$end_of_script" != "y" ])
do
echo -n "Press y to continue or press n to exit (y/n): "
read end_of_script
if ([ "$end_of_script" != "n" ] && [ "$end_of_script" != "y" ])
then
echo "The input is incorrect!!!"
fi
done
done
if [ $reset_server -eq 1 ]
then
sed -i '/combined/s/^LogFormat "/LogFormat "%V /' $sroot/conf/httpd.conf
sed -i '/common/s/^LogFormat "/LogFormat "%V /' $sroot/conf/httpd.conf
$sroot/bin/apachectl stop 1>/dev/null
$sroot/bin/apachectl start 1>/dev/null
fi


Regards,
Khosro Taraghi

No comments:

Post a Comment