Simple bash script to create virtual hosts in apache

Wrote this bash script that automates the task of creating apache site config file and updating host file for the newly created host. Don’t forget to restart the apache server after the file is created and host file entry is made.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
#asks password so that it can go and perform action in root directories
su
echo "Please give the domain name :"
read domain

echo "Please give the IP address :"
read ipaddrs

if [ -d "/etc" ]; then
  if [ -d "/etc/apache2" ]; then
    if [ -d "/etc/apache2/sites-available" ]; then
    
        if [ -f "/etc/apache2/sites-available/"$domain".conf" ]; then
            
            echo "SITE ALREADY EXISTS"
            
        else
            echo "CREATE NEW SITE CONFIG FILE"
            
            cd "/etc/apache2/sites-available"
            touch $domain".conf"
            
            
            echo "CREATE ENTRY INSIDE THE SITE CONFIG FILE"
            
            echo "<VirtualHost *:80>" >> $domain".conf"
            echo '\t DocumentRoot "/home/'$domain'/public"' >> $domain".conf"
            echo "\t ServerName "$domain  >> $1".conf"
            echo "\t ServerAlias www."$domain >> $1".conf"
            
            echo '\t <Directory "/home/'$domain'/public">' >> $domain".conf"
                echo '\t\t allow from all' >> $domain".conf"
                echo '\t\t order allow,deny' >> $domain".conf"
                echo '\t\t AllowOverride All' >> $domain".conf"
            echo '\t </Directory>' >> $domain".conf"
            
            echo "</VirtualHost>"  >> $domain".conf"
            
            echo "CREATE HOST FILE ENTRY"
            cd "../../"
            echo $ipaddrs $domain >> hosts
            
            cd "../home"
			echo "CREATE HOME DIRECTORY"
			mkdir $domain
			cd $domain
			mkdir "public"
			cd "public"
			touch index.html
			echo "Welcome to "$domain >> index.html

			echo "HOST SETUP SUCCESSFULLY. PLEASE RESTART APACHE."
        fi
    else
        echo "ERROR: '/etc/apache2/sites-available' does not exists";
    fi
  fi
fi
Advertisements
Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s