Script generatore di Host Virtuali Web - Link alla Guida in Italiano
Script to generate Web Virtual Host
Wanting to set up a virtual Web server on my physical server in my intranet, with Mandriva I found a very winding road with lots of configuration scripts to create and modify or rewrite and finally there is a high risk that the whole thing does not work, and that is that's why I decided to create a bash script suitable and easy to use.
These notes allow you to create all the virtual servers that you want in a single PC.
Useful to emulate Web pages locally or for many Web servers running on the network.
Before proceeding to describe the script, you better know how to create a physical server in your network with a local physical IP.
Open a terminal and we logged in as Root and paste the following lines:
Code: Select all
urpmi task-lamp -a -y
urpmi ssh -a -y
urpmi drakwizard -y
urpme proftpd -y
urpmi pure-ftpd- -a -y
ifconfig eth0 | awk '/inet/ {print $2}'> /file.txt; i=$(cat /file.txt);rm -f /file.txt;i=${i:5};echo $i | awk '/addr/ {print $1}' > /etc/ip; i=$(cat /etc/ip)
ns=$(echo $HOSTNAME)
echo; echo -n "Name for the physical server: ["${ns}"]-->"; read nsf
if [ ! $nsf ]; then
nsf=$ns
fi
echo "HOSTNAME="$nsf >> /etc/sysconfig/network
export HOSTNAME=${nsf}
echo $nsf > /etc/hostname
echo "<html><body><h1>It works!</h1>" > /var/www/html/index.html
echo "<p>This is the default web page for this server.</p>" >> /var/www/html/index.html
echo "<p>The web server software is running but no content has been added, yet.</p>" >> /var/www/html/index.html
echo "<p>Welcome to http://$nfs</p>" >> /var/www/html/index.html
echo "<p>IP for the FTP service: $nfs</p>" >> /var/www/html/index.html
echo "<p>Physical location of $nsf: /var/www/html/</p>" >> /var/www/html/index.html
echo "</body></html>" >> /var/www/html/index.html
echo $i" "$nfs >> /etc/hosts
After the reboot run the following script (mkvhost) to generate the virtual host:
Code: Select all
#! /bin/bash
# Script is to be saved in: /usr/sbin/ of server
echo $0
echo "##########################################################################"
echo "# MKVHOST - (c)-2011 - Vr. 1.1 - /14/07/2011 #"
echo "# Automatic procedure to create a Virtual Host in Web server. #"
echo "##########################################################################"
# We read the counter of virtual hosts
if [ /etc/httpd/conf/nvh ]; then
nh=$(cat /etc/httpd/conf/nvh)
else
nh="0"
fi
# We increment the counter of virtual hosts
nh=$(expr $nh + 1)
# We identify the name assigned to the server PC;
# Of good practice on all versions of Linux, except on Mandriva,
# It is written in /etc/hostname
if [ ! -f /etc/hostname ]; then
echo; echo "ERROR: This computer does not have a name or the server HTTPD is missing!"
echo -n "Do you want to assign a name to this server (y / n)? [s] -->"; read sn
if [ ! $sn ]; then
sn="s"
else
sn="n"
fi
if [ $sn == "s" ]; then
ns=$(echo $HOSTNAME)
echo; echo -n "Name for the physical server: ["${ns}"]-->"; read nsf
if [ ! $nsf ]; then
nsf=$ns
fi
echo "HOSTNAME="$nsf >> /etc/sysconfig/network
export HOSTNAME=${nsf}
echo $nsf > /etc/hostname
echo; echo "Rerun the command: MKVHOST"
fi
exit 1
fi
# We identify the IP number and write it along with the name of the server in /etc/hosts
ifconfig eth0 | awk '/inet/ {print $2}'> /file.txt; i=$(cat /file.txt);rm -f /file.txt;i=${i:5};echo $i | awk '/addr/ {print $1}' > /etc/ip; i=$(cat /etc/ip)
echo $i" "$(cat /etc/hostname)
# We generate the configuration for your virtual servers with virtual IP
# Are then required: the name of the administrator of the new host,
# the name for the host and the last digit of the new virtual IP to be generated
# Obviously the user must already exist on your PC as an account of the physical server
echo "You're creating the virtual server N°: "$nh
echo -n "Virtual server name to be generated: -->"; read vserver
nsip=$i
ni=${nsip:0:9}
echo -n " VIrtual Host's IP number ${ni}.### (IP: Only final part): -->"; read nvip
nvip=${ni}"."${nvip}
echo $nvip
echo; echo -n "Username: -->"; read utente
echo
webdir="/home/"${utente}"/web/"${vserver}
# Checking the existence of the user
if [ ! "/home/"${utente} ]; then
echo "User: ${utente} It does not exist."
exit 1
fi
# Creation of the structure, and sample index.html page in the new virtual server
mkdir -p ${webdir}
echo "<html><body><h1>It works!</h1>" > ${webdir}/index.html
echo "<p>This is the default web page for this server.</p>" >> ${webdir}/index.html
echo "<p>The web server software is running but no content has been added, yet.</p>" >> ${webdir}/index.html
echo "<p>Welcome to http://$nsip</p>" >> ${webdir}/index.html
echo "<p>Virtual Server: $vserver</p>" >> ${webdir}/index.html
echo "<p>Virtual IP: $nvip</p>" >> ${webdir}/index.html
echo "<p>IP for the FTP service: $nsip</p>" >> ${webdir}/index.html
echo "<p>Owner user: $utente</p>" >> ${webdir}/index.html
echo "<p>Physical directory on the server: /home/$utente/web/$vserver/</p>" >> ${webdir}/index.html
echo "</body></html>" >> ${webdir}/index.html
chmod -R 777 /home/${utente}/web
chown -R ${utente}:${utente} /home/${utente}/web
echo "Generating: VirtualHost N° "$nh
echo $nh > /etc/httpd/conf/nvh
# Editing the configuration file /bin/vhost
# if it does not already exist is created
# It is used to enable hosts based on IP
if [ ! /bin/vhost ]; then
echo "#! /bin/bash" > /bin/vhost
echo "##############" >> /bin/vhost
echo "# /bin/vhost #" >> /bin/vhost
echo "#################################" >> /bin/vhost
echo "# Procedura generata da MKVHOST #" >> /bin/vhost
echo "#################################" >> /bin/vhost
echo "# script to run on bootup to bind additional IP addresses to the" >> /bin/vhost
echo "# ethernet interface." >> /bin/vhost
echo "" >> /bin/vhost
cd /bin
chmod 777 vhost
cd /
# Entering the command to run the vhosts script in /etc/rc.d/rc.local
# so you turn on all the virtual hosts with the launch of the PC
echo "vhost" >> /etc/rc.d/rc.local
fi
echo "echo \"Assign IP: ${nvip} to Virtual Hosts: ${vserver}\"" >> /bin/vhost
echo "ifconfig eth0:"$nh" add $nvip" >> /bin/vhost
# Editing the configuration file: /etc/httpd/conf/vhosts.d/vhosts.conf
# if it does not already exist is created
# This file is set to configure the new Virtual Host with the path of the web directory
if [ ! /etc/httpd/conf/vhosts.d/vhosts.conf ]; then
echo "########################################" > /etc/httpd/conf/vhosts.d/vhosts.conf
echo "# /etc/httpd/conf/vhosts.d/vhosts.conf #" >> /etc/httpd/conf/vhosts.d/vhosts.conf
echo "########################################" >> /etc/httpd/conf/vhosts.d/vhosts.conf
echo "# Procedura generata da MKVHOST #" >> /etc/httpd/conf/vhosts.d/vhosts.conf
echo "#################################" >> /etc/httpd/conf/vhosts.d/vhosts.conf
fi
echo "" >> /etc/httpd/conf/vhosts.d/vhosts.conf
echo "# NOME VIRTUAL HOST: "$vserver >> /etc/httpd/conf/vhosts.d/vhosts.conf
echo "# PROPRIETARIO: "$utente >> /etc/httpd/conf/vhosts.d/vhosts.conf
echo "NameVirtualHost ${nvip}" >> /etc/httpd/conf/vhosts.d/vhosts.conf
echo "<VirtualHost ${nvip}>" >> /etc/httpd/conf/vhosts.d/vhosts.conf
echo " ServerName ${vserver}" >> /etc/httpd/conf/vhosts.d/vhosts.conf
echo " ServerAlias www.${vserver}" >> /etc/httpd/conf/vhosts.d/vhosts.conf
echo " ErrorLog logs/sick-internals-error_log" >> /etc/httpd/conf/vhosts.d/vhosts.conf
echo " ServerPath /home/${utente}/web" >> /etc/httpd/conf/vhosts.d/vhosts.conf
echo " DocumentRoot /home/${utente}/web/${vserver}" >> /etc/httpd/conf/vhosts.d/vhosts.conf
echo " <Directory \"\">" >> /etc/httpd/conf/vhosts.d/vhosts.conf
echo " Options Indexes FollowSymLinks MultiViews" >> /etc/httpd/conf/vhosts.d/vhosts.conf
echo " AllowOverride None" >> /etc/httpd/conf/vhosts.d/vhosts.conf
echo " Order allow,deny" >> /etc/httpd/conf/vhosts.d/vhosts.conf
echo " Allow from all" >> /etc/httpd/conf/vhosts.d/vhosts.conf
echo " </Directory>" >> /etc/httpd/conf/vhosts.d/vhosts.conf
echo "</VirtualHost>" >> /etc/httpd/conf/vhosts.d/vhosts.conf
echo $nvip" "$vserver >> /etc/hosts
echo; echo "Add the following line in \"/etc/hosts\" of Client PC."
echo; echo $nvip" "$vserver" www."$vserver
echo; echo "Restart the HTTPD server:"
# Restart the HTTPD server
service httpd restart
# Activate virtual hosts manually
vhost
echo; echo "Edit from the browser: http://"$nvip
This script, I recommend you to save it in /usr/sbin/ with the name: mkvhost
Of course, set the execute permissions:
Code: Select all
chmod 777 /usr/sbin/mkvhost
Attach summary of instructions with steps to follow:
Each virtual host has a virtual IP in addition to its name.
Instructions for the configurations:
1) install Mandriva on your PC without to select the server part during the installation
2) assign a static IP like: 192.168.0.XX 192.168.1.xx or (depending on your router) to your PC
3) perform a normal update of packages from repository
4) reboot
5) open the console and you log in as Root
6) Install and configure a physical server (as above)
7) Turn off all ports on the firewall from the Control Panel -> Security
8) restart the system and already has the physical server with HTTP: / / <ip server> and FTP anonymous FTP: / / <ip server>
9) run the command from the console and set DRAKWIZARD SSH (if desired)
10) always with a console as root run the command: MKVHOST to generate the first virtual server
So you can create as many virtual servers you want with its virtual IP for each different user.
Each registered user to the server via a client-ftp enters its own space and manages their virtual servers.
MySQL is installed: you have to activate the administrator user as:
Code: Select all
mysqladmin -u <utente> password <password>
Let me know how you found with this script!
Bye