Help In Linux

Linux Technical Blog
August 7, 2012

apache create multiple virtual hosts script | shell | bash

Hello,

The following script will help you to create upto 5 websites in apache with basic configuration you may increase the number to N by adding N numbers for value a. Please make sure that you have taken a backup of /etc/httpd/conf/httpd.conf file before you proceed, and this script will create a user too so make sure you understand what it is doing.

#!/bin/bash
#! Shell script to add multiple domains/websites for apache

echo "Make sure that you took backup of your /etc/httpd/conf/httpd.conf file before proceeding"

for a in 1 2 3 4 5
do
echo "Enter the name of the domain you wish to create"
read a
touch /etc/httpd/conf/httpd.conf
echo "Enter the username of the website you wish to create"
read username
/usr/sbin/useradd $username
echo "Enter the password for the user $username"
/usr/bin/passwd $username
echo "Enter the website path in the server"
echo "Example /home/$username/public_html"
read i
mkdir -p "$i"
chmod 750 /home/$username
echo "Enter the IP address to which the domain should listen to"
read ip
echo "<VirtualHost $ip:80>" >> /etc/httpd/conf/httpd.conf
echo "ServerAdmin webmaster@$a" >> /etc/httpd/conf/httpd.conf
echo "DocumentRoot $i" >> /etc/httpd/conf/httpd.conf
echo "ServerName $a" >> /etc/httpd/conf/httpd.conf
echo "ErrorLog logs/$a-error_log" >> /etc/httpd/conf/httpd.conf
echo "CustomLog logs/$a-access_log common" >> /etc/httpd/conf/httpd.conf
echo "</VirtualHost>" >> /etc/httpd/conf/httpd.conf
done





echo "Please make sure that you have set apache user to read permission at /home/user folder"

Thank you for viewing helpinlinux.com

Leave a Comment



Protected by WP Anti Spam