LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-14-2011, 02:11 PM   #1
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Rep: Reputation: 10
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'l change for every subdomain. 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.
 
Old 03-14-2011, 05:24 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
All you need to look up is how to pass a variable to a script (ie:
Code:
makeNewHost google.com
getting the "google.com" inside the script)

You can then refer to this easily inside the script as follows:

Code:
echo JkMount /${HOST}* ajp13
echo RewriteEngine On
Hope this helps,
 
Old 03-14-2011, 05:58 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,341
Blog Entries: 55

Rep: Reputation: 3538Reputation: 3538Reputation: 3538Reputation: 3538Reputation: 3538Reputation: 3538Reputation: 3538Reputation: 3538Reputation: 3538Reputation: 3538Reputation: 3538
Give the script the domain name as argument and redirect output to a configuration file:
Code:
#!/bin/bash
[ $# -ne 1 ] && exit 1 || DOMAINNAME="$1"
cat << EOF
<VirtualHost *:80>
ServerName ${DOMAINNAME}
ServerAlias ${DOMAINNAME}
DocumentRoot "/usr/local/tomcat/webapps/${DOMAINNAME//\.*/}"
<Directory "/usr/local/tomcat/webapps/${DOMAINNAME//\.*/}">
 DirectoryIndex index.jsp index.html
 Options Indexes FollowSymLinks
 AllowOverride None
 Order allow,deny
 Allow from all
</Directory>
JkOptions +ForwardURICompat
JkMount /${DOMAINNAME//\.*/}* ajp13
RewriteEngine On
RewriteLogLevel 5
RewriteLog logs/internal_rewrite_log
RewriteCond %{REQUEST_URI} !^/${DOMAINNAME//\.*/}/.*\.(jpe?g|gif|png|css|js)
RewriteRule ^(.*)\$ /google/\$1 [L,PT]
</VirtualHost>
EOF
exit 0
 
Old 03-14-2011, 10:03 PM   #4
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Original Poster
Rep: Reputation: 10
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

Hi unSpawn,

Using this script i can able to automate the virtual host entry process. Thanks a lot for your replies.

With Regards
Anish Kumar.V
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script Commands to Automate Server or Host Telnet Login Session jakesjohn Linux - Software 4 02-08-2010 11:32 PM
Shell Script Commands to Automate Server or Host Telnet Login Session jakesjohn Linux - General 2 02-08-2010 11:28 PM
How to program shell script to automate mass user account creation? EsAsher Linux - General 2 06-30-2007 08:41 AM
Automate Samba logon with a bash script? achtung_linux Linux - Networking 6 10-22-2006 02:36 AM


All times are GMT -5. The time now is 04:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration