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.

<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.

#!/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
$ ./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.

Hi dude,

Its Work great thanks. :slight_smile: but if i append 2 domain means it display continously without any space.

<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.

<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.

#!/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
$ ./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!

Hi dude,

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

With Regards
Anish Kumar.V

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.