Bash script to Automate the Virtual Host creation process!!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script to Automate the Virtual Host creation process!!
# 1  
Old 03-14-2011
Bash script to Automate the Virtual Host creation process!!

Hi all,

This is my sample code in /etc/httpd/conf.d/applications.conf file currently we are creating subdomain mannually for every new subdomain. I want to automate this process througs bash script , how its possible.

Code:
<VirtualHost *:80>
ServerName google.com
ServerAlias google.com
DocumentRoot "/usr/local/tomcat/webapps/google"
<Directory "/usr/local/tomcat/webapps/google">
DirectoryIndex index.jsp index.html
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
JkOptions +ForwardURICompat
JkMount /google* ajp13
RewriteEngine On
RewriteLogLevel 5
RewriteLog logs/internal_rewrite_log
RewriteCond %{REQUEST_URI} !^/google/.*\.(jpe?g|gif|png|css|js)
RewriteRule ^(.*)$ /google/$1 [L,PT]
</VirtualHost>

In this code which i marked BOLD that content only we'll change for
every sub domain. while manually doing this most of the times errors are
coming to avoid that i need to automate this process. please guide me
to write the script for this...



Thanks and Regards

V.Anish Kumar.
# 2  
Old 03-14-2011
Code:
#!/bin/sh

NAME="$1"
cat <<EOF
<VirtualHost *:80>
ServerName ${NAME}.com
ServerAlias ${NAME}.com
DocumentRoot "/usr/local/tomcat/webapps/${NAME}"
<Directory "/usr/local/tomcat/webapps/${NAME}">
DirectoryIndex index.jsp index.html
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
JkOptions +ForwardURICompat
JkMount /${NAME}* ajp13
RewriteEngine On
RewriteLogLevel 5
RewriteLog logs/internal_rewrite_log
RewriteCond %{REQUEST_URI} !^/${NAME}/.*\.(jpe?g|gif|png|css|js)
RewriteRule ^(.*)\$ /${NAME}/\$1 [L,PT]
</VirtualHost>
EOF

Code:
$ ./automate.sh google
<VirtualHost *:80>
ServerName google.com
ServerAlias google.com
DocumentRoot "/usr/local/tomcat/webapps/google"
<Directory "/usr/local/tomcat/webapps/google">
DirectoryIndex index.jsp index.html
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
JkOptions +ForwardURICompat
JkMount /google* ajp13
RewriteEngine On
RewriteLogLevel 5
RewriteLog logs/internal_rewrite_log
RewriteCond %{REQUEST_URI} !^/google/.*\.(jpe?g|gif|png|css|js)
RewriteRule ^(.*)$ /google/$1 [L,PT]
</VirtualHost>

$ ./automate.sh fnord
<VirtualHost *:80>
ServerName fnord.com
ServerAlias fnord.com
DocumentRoot "/usr/local/tomcat/webapps/fnord"
<Directory "/usr/local/tomcat/webapps/fnord">
DirectoryIndex index.jsp index.html
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
JkOptions +ForwardURICompat
JkMount /fnord* ajp13
RewriteEngine On
RewriteLogLevel 5
RewriteLog logs/internal_rewrite_log
RewriteCond %{REQUEST_URI} !^/fnord/.*\.(jpe?g|gif|png|css|js)
RewriteRule ^(.*)$ /fnord/$1 [L,PT]
</VirtualHost>

$ ./automate.sh fnord > fnord.conf
$

Note how $ became \$ in the here-document. That, and google becoming ${NAME}, was all I needed to change in it.
# 3  
Old 03-14-2011
Hi dude,

Its Work great thanks. :-) but if i append 2 domain means it display continously without any space.
Code:
<VirtualHost *:80>
ServerName biju.com
ServerAlias biju.com
DocumentRoot "/usr/local/tomcat/webapps/biju"
<Directory "/usr/local/tomcat/webapps/biju">
DirectoryIndex index.jsp index.html
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
JkOptions +ForwardURICompat
JkMount /biju* ajp13
RewriteEngine On
RewriteLogLevel 5
RewriteLog logs/internal_rewrite_log
RewriteCond %{REQUEST_URI} !^/biju/.*\.(jpe?g|gif|png|css|js)
RewriteRule ^(.*)$ /biju/$1 [L,PT]
</VirtualHost>
<VirtualHost *:80>
ServerName anish.com
ServerAlias anish.com
DocumentRoot "/usr/local/tomcat/webapps/anish"
<Directory "/usr/local/tomcat/webapps/anish">
DirectoryIndex index.jsp index.html
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
JkOptions +ForwardURICompat
JkMount /anish* ajp13
RewriteEngine On
RewriteLogLevel 5
RewriteLog logs/internal_rewrite_log
RewriteCond %{REQUEST_URI} !^/anish/.*\.(jpe?g|gif|png|css|js)
RewriteRule ^(.*)$ /anish/$1 [L,PT]
</VirtualHost>

Is there any option to separate this like this.

Code:
<VirtualHost *:80>
ServerName biju.com
ServerAlias biju.com
DocumentRoot "/usr/local/tomcat/webapps/biju"
<Directory "/usr/local/tomcat/webapps/biju">
DirectoryIndex index.jsp index.html
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
JkOptions +ForwardURICompat
JkMount /biju* ajp13
RewriteEngine On
RewriteLogLevel 5
RewriteLog logs/internal_rewrite_log
RewriteCond %{REQUEST_URI} !^/biju/.*\.(jpe?g|gif|png|css|js)
RewriteRule ^(.*)$ /biju/$1 [L,PT]


</VirtualHost>
<VirtualHost *:80>
ServerName anish.com
ServerAlias anish.com
DocumentRoot "/usr/local/tomcat/webapps/anish"
<Directory "/usr/local/tomcat/webapps/anish">
DirectoryIndex index.jsp index.html
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
JkOptions +ForwardURICompat
JkMount /anish* ajp13
RewriteEngine On
RewriteLogLevel 5
RewriteLog logs/internal_rewrite_log
RewriteCond %{REQUEST_URI} !^/anish/.*\.(jpe?g|gif|png|css|js)
RewriteRule ^(.*)$ /anish/$1 [L,PT]
</VirtualHost>

i want display like this.
# 4  
Old 03-14-2011
Code:
#!/bin/sh


while [ ! -z "$1" ]
do
        NAME="$1"
        cat <<EOF
<VirtualHost *:80>
ServerName ${NAME}.com
ServerAlias ${NAME}.com
DocumentRoot "/usr/local/tomcat/webapps/${NAME}"
<Directory "/usr/local/tomcat/webapps/${NAME}">
DirectoryIndex index.jsp index.html
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
JkOptions +ForwardURICompat
JkMount /${NAME}* ajp13
RewriteEngine On
RewriteLogLevel 5
RewriteLog logs/internal_rewrite_log
RewriteCond %{REQUEST_URI} !^/${NAME}/.*\.(jpe?g|gif|png|css|js)
RewriteRule ^(.*)\$ /${NAME}/\$1 [L,PT]
</VirtualHost>

EOF
        shift
done

Code:
$ ./automate.sh fleeb garbl braak
<VirtualHost *:80>
ServerName fleeb.com
ServerAlias fleeb.com
DocumentRoot "/usr/local/tomcat/webapps/fleeb"
<Directory "/usr/local/tomcat/webapps/fleeb">
DirectoryIndex index.jsp index.html
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
JkOptions +ForwardURICompat
JkMount /fleeb* ajp13
RewriteEngine On
RewriteLogLevel 5
RewriteLog logs/internal_rewrite_log
RewriteCond %{REQUEST_URI} !^/fleeb/.*\.(jpe?g|gif|png|css|js)
RewriteRule ^(.*)$ /fleeb/$1 [L,PT]
</VirtualHost>

<VirtualHost *:80>
ServerName garbl.com
ServerAlias garbl.com
DocumentRoot "/usr/local/tomcat/webapps/garbl"
<Directory "/usr/local/tomcat/webapps/garbl">
DirectoryIndex index.jsp index.html
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
JkOptions +ForwardURICompat
JkMount /garbl* ajp13
RewriteEngine On
RewriteLogLevel 5
RewriteLog logs/internal_rewrite_log
RewriteCond %{REQUEST_URI} !^/garbl/.*\.(jpe?g|gif|png|css|js)
RewriteRule ^(.*)$ /garbl/$1 [L,PT]
</VirtualHost>

<VirtualHost *:80>
ServerName braaak.com
ServerAlias braaak.com
DocumentRoot "/usr/local/tomcat/webapps/braaak"
<Directory "/usr/local/tomcat/webapps/braaak">
DirectoryIndex index.jsp index.html
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
JkOptions +ForwardURICompat
JkMount /braaak* ajp13
RewriteEngine On
RewriteLogLevel 5
RewriteLog logs/internal_rewrite_log
RewriteCond %{REQUEST_URI} !^/braaak/.*\.(jpe?g|gif|png|css|js)
RewriteRule ^(.*)$ /braaak/$1 [L,PT]
</VirtualHost>

$ ./automate.sh fleeb garbl braak > filetocreate.conf
$ ./automate.sh fleeb garbl braak >> filetoappendto.conf
$

Note the difference between > and >> . If you use a single > you will overwrite the file, not append!
# 5  
Old 03-15-2011
Hi dude,

It works like a charm..!! thanks alot for your replies :-).


With Regards
Anish Kumar.V
# 6  
Old 07-12-2011
Hi Corona,

I have implemented this script work nice but i need just one addition in it can you help me with the same.
I need it to auto reload httpd is it possible.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How To Write my Bash Script To Automate it?

Hello guys, I need some help. I am new in bash and I don't know how to automate the following script. head -2 out1 > 1.fasta sed ‘1,2 d' out1 > out2 rm out1 head -2 out2 > 2.fasta sed ‘1,2 d' out2 > out1 rm out2 head -2 out2 > 3.fasta sed '1,2 d' out2 > out1 rm out2 .......... (3 Replies)
Discussion started by: dellia222
3 Replies

2. Open Source

Help with writing Shell Script to automate process using multiple commands

Hello! Need help to write a Linux script that can be run from windows using command/Cygwin/any other way. I am new to scripting, actually i am trying to automate server health check like free disk space, memory along with few services status, if any services is not running then start services ,... (7 Replies)
Discussion started by: Sayed Ibrahim
7 Replies

3. Shell Programming and Scripting

Automate OTPW login to ssh via bash script

Hello everyone. I'm a Linux novice trying out a lot of bash scripting lately, as it is so very addictive. Lately I have been setting up one of my boxes remotely and have been hardening it as much as possible. Please allow me to explain the scenario, as it does tend to become a little... (1 Reply)
Discussion started by: instro
1 Replies

4. Shell Programming and Scripting

Script to automate recovery process

Hello All! First post... I am working on a script that is used to recover a crashed drive from an rsync backup. I'm down to the place where I need to create all of the directories in /mnt where I will then mount each of the volumes and begin the restore process to each volume... I have... (3 Replies)
Discussion started by: RogerBaran
3 Replies

5. UNIX for Dummies Questions & Answers

Automate sftp process using script

Hi, guys, I am trying to automate a sftp process using "expect" method (since the key authentication method is disabled in my company network, there is no helping it). In order to try, I type in the command manually: sftp @ > << EOF >cd >ls -l >EOF >Connecting to @servername password: ... (3 Replies)
Discussion started by: warmboy610
3 Replies

6. Shell Programming and Scripting

Writing a bash script using host

Im trying to write a script using the host command but its not working properly. I cant understand what Im doing wrong. When I use it at the command prompt, it works fine. But its being used actually in the script, it says its not found: 2 SERVFAIL. Can anyone help me? Here's what I have so far: no... (6 Replies)
Discussion started by: relsha
6 Replies

7. UNIX for Advanced & Expert Users

Can we Automate the User creation and setting password through a script in solaris 10

Hi, I am using Solaris 10 OS and Bash shell.Is there any way can we automate User creation and setting passwords through a script or any freeware tool. Advance thanks for your response. (1 Reply)
Discussion started by: muraliinfy04
1 Replies

8. Shell Programming and Scripting

Need help in creating a shell script to automate svnstats process

Hi, I am trying to create a shell script to automate the following process of getting svn stats:- Step1:- cd to checkout location. Note that the checked code have multiple modules in respective folders Step2:- Execute this command inside each module:- svn log -v --xml >... (0 Replies)
Discussion started by: d8011
0 Replies

9. Solaris

Change hostID of Solaris 10 virtual/guest machine installed by Virtual Box 4.1.12 on Windows-XP host

Trying to set or modify the randomly set hostID of a Solaris 10 virtual/guest machine that I installed on a Windows-XP host machine (using Virtual Box 4.1.12). I was able to set/modify the hostname of the Solaris 10 virtual/guest machine during installation as well as via the Virtual Box... (4 Replies)
Discussion started by: Matt_VB
4 Replies

10. Shell Programming and Scripting

Error in script to automate the daily monitoring process of UNIX server and it's proc

hi friends, I am trying to automate the daily monitoring process of UNIX server and it's processes. the script are below i executed the above script using ksh -x monitortest1.sh in root login . It shows error at some lines . 1. i logged in using root ,but it... (8 Replies)
Discussion started by: rdhaprakasam
8 Replies
Login or Register to Ask a Question