Monday, January 31, 2011

Apache, Tomcat cluster, load balancing using mod_jk

Apache

The Apache HTTP Server Project is a collaborative software development effort aimed at creating a robust, commercial-grade, feature ful, and freely-available source code implementation of an HTTP (Web) server. The project is jointly managed by a group of volunteers located around the world, using the Internet and the Web to communicate, plan, and develop the server and its related documentation. This project is part of the Apache Software Foundation. In addition, hundreds of users have contributed ideas, code, and documentation to the project. This file is intended to briefly describe the history of the Apache HTTP Server and recognize the many contributors. 

Tomcat

Apache Tomcat is an open source software implementation of the Java Servlet and JavaServer Pages technologies. The Java Servlet and Java Server Pages specifications are developed under the Java Community Process.
Apache Tomcat is developed in an open and participatory environment and released under the Apache License version 2. Apache Tomcat is intended to be a collaboration of the best-of-breed developers from around the world. We invite you to participate in this open development project.

 Download the required software 

  • Apache 2.x Binaries 
  • Tomcat 6.x Binaries
  • The JK module
Installing of Apache and Tomcat   please refer previous posting of installing of Apache and Tomcat

load the mod_jk.so module 
LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel info
below DOcument Root
JkMount /*.jsp loadbalancer
JkMount /servlet/* loadbalancer
And create worker.properties file in /etc/httpd/conf/ directory
#
# workers.properties 
#

# In Unix, we use forward slashes:
ps=/

# list the workers by name

worker.list=tomcat1, tomcat2, loadbalancer

# ------------------------
# First tomcat server
# ------------------------
worker.tomcat1.port=8019
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13

# Specify the size of the open connection cache.
#worker.tomcat1.cachesize

#
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
#  ----> lbfactor must be > 0
#  ----> Low lbfactor means less work done by the worker.
worker.tomcat1.lbfactor=100


# ------------------------
# Second tomcat server
# ------------------------
worker.tomcat2.port=8029
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13

# Specify the size of the open connection cache.
#worker.tomcat2.cachesize

#
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
#  ----> lbfactor must be > 0
#  ----> Low lbfactor means less work done by the worker.
worker.tomcat2.lbfactor=100


# ------------------------
# Load Balancer worker
# ------------------------

#
# The loadbalancer (type lb) worker performs weighted round-robin
# load balancing with sticky sessions.
# Note:
#  ----> If a worker dies, the load balancer will check its state
#        once in a while. Until then all work is redirected to peer
#        worker.
worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=tomcat1, tomcat2

#
# END workers.properties
#
 
 
and goto the tomcat server1 and edit the file /opt/tomcat1/conf/server.xml
 
edit  line 
 

                                                   to

 

             to
     
 
and goto the tomcat server2 and edit the file /opt/tomcat2/conf/server.xml

to

 


  
             to
     
 
 
In  apache server
 
NameVirtualHost *


ServerName localhost1
JkMount /*.jsp tomcat1
JkMount /servlet/* tomcat1


ServerName localhost2
JkMount /*.jsp tomcat2
JkMount /servlet/* tomcat2
 
 
And save the files and restart the both tomcat1 and tomcat2 server and apache start
and in browser
http://localhost
 
apache index.html will dispaly
and http://localhost/index.jsp
 
request go to server1 and continue if will server load heavy
 it will redirect to server2 or in case of fail over 

Redirecting Request from apache to tomcat Server using Mod jk

Apache

The Apache HTTP Server Project is a collaborative software development effort aimed at creating a robust, commercial-grade, feature ful, and freely-available source code implementation of an HTTP (Web) server. The project is jointly managed by a group of volunteers located around the world, using the Internet and the Web to communicate, plan, and develop the server and its related documentation. This project is part of the Apache Software Foundation. In addition, hundreds of users have contributed ideas, code, and documentation to the project. This file is intended to briefly describe the history of the Apache HTTP Server and recognize the many contributors. 

Tomcat

Apache Tomcat is an open source software implementation of the Java Servlet and Java Server Pages technologies. The Java Servlet and Java Server Pages specifications are developed under the Java Community Process.
Apache Tomcat is developed in an open and participatory environment and released under the Apache License version 2. Apache Tomcat is intended to be a collaboration of the best-of-breed developers from around the world. We invite you to participate in this open development project.

using mod_proxy.so and mod_proxy_ajp.so


load module in Apache
edit the file /etc/httpd/conf/httpd.conf
uncomment or add

Download Mod_jk binary from
wget http://www.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/linux

workers.properties File

  • Create the workers.properties in your apache configuration directory. Example:
    touch /etc/apache2/workers.properties
    
  • Using your favorite editor (I like to use vi), edit the file and add the following information (tailor it to suit your system):
       workers.tomcat_home=/opt/tomcat6
       workers.java_home=/opt/java
       ps=/
       worker.list=worker1
    
       worker.worker1.port=8009
       worker.worker1.host=127.0.0.1
       worker.worker1.type=ajp13
       worker.worker1.lbfactor=1
    

Virtual Host File


This is probably the most vital step that is missing in a lot of how-to's. The JkMount directive must be added to the virtual host file containing port 80. The virtual host definition may also be present in your apache2.conf or httpd.conf file. If not, create a new file called something like default.conf, add the contents below, and put default.conf somewhere in which it can be properly loaded by apache2.conf (or httpd.conf).
    
       ServerName your.hostname.com
    
       # Send servlet for context /jsp-examples to worker named worker1
       JkMount /jsp-examples worker1
    
       # Send JSPs for context /jsp-examples/* to worker named worker1
       JkMount /jsp-examples/* worker1
    
     
     

save the file  and restart the httpd server

and goto tomcat install directory

conf directory and edit server.xml

and restart the tomcat server

Test It


To test your setup, type http://localhost/jsp-examples. It should also work with http://localhost:8080/jsp-examples.  

it will redirect to tomcat jsp-examples  home page

Creating Secure Site in Tomact and Apache

Apache Tomcat

Apache Tomcat is an open source software implementation of the Java Servlet and Java Server Pages technologies. The Java Servlet and Java Server Pages specifications are developed under the Java Community Process.
Apache Tomcat is developed in an open and participatory environment and released under the Apache License version 2. Apache Tomcat is intended to be a collaboration of the best-of-breed developers from around the world. We invite you to participate in this open development project. 

Installation of Apache Tomcat

Downloads the tar file for

http://apache.cs.utah.edu/tomcat/tomcat-6/v6.0.30/bin/apache-tomcat-6.0.30.tar.gz

tar -zxvf  Apache-tomcat-6.0.30.tar.gz
cp -r   tomcat /opt/tomcat

and write a start up scripts
create a file tomcat in /etc/init.d  give chmod 777 permissions to tomcat file
copy the 

#!/bin/bash
# chkconfig: - 10 20
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
PATH=/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/bin:$PATH:$HOME/bin:./
export PATH
 export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/jre
export CLASSPATH=/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/lib/tools.jar:/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/lib/dt.jar:./
export CATALINA_HOME=/opt/tomcat/
export CATALINA_TMPDIR=/opt/tomcat/temp
unset USERNAME

start()
{
cd /opt/tomcat6/bin/
sh startup.sh
}
stop()
{
cd /opt/tomcat/bin/
sh shutdown.sh
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "start | stop | restart"
esac

save the file

and  start tomcat  /etc/init.d/tomcat start
and stop tomcat /etc/init.d/tomcat stop

http://localhost:8080

to create Secure site https

goto 
cd /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/bin

keytool -genkey -alias name-keypass admin123  -keystore domainnamecom.bin -storepass admin123
Note:keypass  and storepass  must be same password
it generate a file with name domainnamecom.bin in same directory
copy the file domainnamecom.bin to /opt/tomcat/webapps

and Goto cd /opt/tomcat/conf

and edit server.xml

and uncommmnet

   
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" keystoreFile="../webapps/domainnamecom.bin" keystorePass="admin123"  />

and save the server.xml
and restart the tomcat server

https://localhost:8443


Apache Server

The Apache HTTP Server Project is a collaborative software development effort aimed at creating a robust, commercial-grade, feature ful, and freely-available source code implementation of an HTTP (Web) server. The project is jointly managed by a group of volunteers located around the world, using the Internet and the Web to communicate, plan, and develop the server and its related documentation. This project is part of the Apache Software Foundation. In addition, hundreds of users have contributed ideas, code, and documentation to the project. This file is intended to briefly describe the history of the Apache HTTP Server and recognize the many contributors. 

Compiling and Installing
Downloads source tar http://httpd.apache.org/download.cgi
tar -zxvf  httpd-NN.tar
cd httpd
./configure --prefix=PREFIX
make
make install

configuration file is in 
/etc/httpd/conf/httpd.conf


to start
/etc/init.d/httpd start
to stop
 /etc/init.d/httpd stop 

and in browser 

http://localhost


To Create secure site 
install mod_ssl and open_ssl

then generate a key file
 openssl genrsa -out /etc/httpd/ssl/domain.com.key 1024

and using the key file generate certiifcate for 999 days

openssl req -new -key /etc/httpd/ssl/domain.com.key -x509 -out /etc/httpd/ssl/domainname.crt -days 999

and edit the /etc/httpd/conf/httpd.conf

 copy the lines

DocumentRoot "/var/www/html/ssl_doc_root/"
ServerAdmin you@mycorp.com
ServerName servername
SSLEngine On
SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
SSLCertificateFile /etc/httpd/ssl/domain.crt
SSLCertificateKeyFile /etc/httpd/ssl/domain.com.key
SetEnvIf User-Agent ".*MSIE.*" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0


save the file
restart the httpd server


and in browser

https://localhost
 

Thursday, January 20, 2011

FreeSwitch Installation Steps in centos and Debain

     Freeswitch
  
FreeSWITCH is a scalable open source cross-platform telephony platform designed to route and interconnect popular communication protocols using audio, video, text or any other form of media.  It was created in 2006 to fill the void left by proprietary commercial solutions. FreeSWITCH also provides a stable telephony platform on which many telephony applications can be developed using a wide range of free tools.

FreeSWITCH was originally designed and implemented by Anthony Minessale with the help of Brian West and Michael Jerris. All 3 are former developers of the popular Asterisk open source PBX.  The project was initiated to focus on several design goals including modularity, cross-platform support, scalability and stability. Today, many more developers and users contribute to the project on a daily basis.

We support various communication technologies such as Skype, SIP, H.323 and GoogleTalk making it easy to interface with other open source PBX systems such as sipXecs, Call Weaver, Bayonne, YATE or Asterisk.

FreeSWITCH supports many advanced SIP features such as presence/BLF/SLA as well as TCP TLS and sRTP. It also can be used as a transparent proxy with and without media in the path to act as a SBC (session border controller) and proxy T.38 and other end to end protocols.

FreeSWITCH supports both wide and narrow band codecs making it an ideal solution to bridge legacy devices to the future. The voice channels and the conference bridge module all can operate at 8, 12, 16, 24, 32 or 48 kilohertz and can bridge channels of different rates. The G.729 codec is also available under a commercial license.

FreeSWITCH builds natively and runs standalone on several operating systems including Windows, Max OS X, Linux, BSD and Solaris on both 32 and 64 bit platforms.

FreeSWITCH supports FAX, both over audio and T.38, and can gateway between the two.


Centos Or Redhat 
Installation Steps

yum install subversion autoconf automake libtool gcc-c++ ncurses-devel make expat-devel zlib zlib-devel
Dependencies For Additional Modules in freeswitch
yum install nano unixODBC-devel openssl-devel gnutls-devel libogg-devel libvorbis-devel curl-devel
Installation git in centos
yum install gettext-devel expat-devel curl-devel zlib-devel openssl-devel
 
wget http://kernel.org/pub/software/scm/git/git-1.6.1.tar.gz
untar the git source code
 
tar xvfz git-1.6.1.tar.gz 
cd git-1.6.1.tar.gz
make prefix=/usr/local all
make prefix=/usr/local install

Downloads the source of Freeswitch from git and install

cd /usr/src/
git clone git://git.freeswitch.org/freeswitch.git
cd /usr/src/freeswitch
./bootstrap.sh
./configure
make install
make cd-sounds-install
make cd-moh-install
it install  default modules of Freeswitch if you needed additional modules
go to /usr/src/freeswitch 
edit the modules.conf
uncomment the modules need
and 
make install
 
 
In Debian
apt-get install autoconf automake g++ git-core libjpeg62-dev libncurses5-dev libtool make
cd /usr/src/
git clone git://git.freeswitch.org/freeswitch.git
cd /usr/src/freeswitch
./bootstrap.sh
./configure
make install
make cd-sounds-install
make cd-moh-install
it install  default modules of Freeswitch if you needed additional modules
go to /usr/src/freeswitch 
edit the modules.conf
uncomment the modules need
and 
make install
Start and stop commands
start
/usr/local/freeswitch/bin/freeswitch -nc  ;non console
stop 
/usr/local/freeswitch/bin/freeswitch -stop 
start up scripts
create a file in /etc/init.d/freeswitch
#!/bin/sh
#
# freeswitch.sh - startup script for freeswitch on FreeBSD
#
# This goes in /usr/local/etc/rc.d and gets run at boot-time.

case "$1" in

    start)
    if [ -x /usr/local/freeswitch/bin/freeswitch ] ; then
        echo -n " freeswitch"
        /usr/local/freeswitch/bin/freeswitch -nc &
    fi
    ;;

    stop)
    if [ -x /usr/local/freeswitch/bin/freeswitch ] ; then
        echo -n " freeswitch"
        /usr/local/freeswitch/bin/freeswitch -stop &
    fi
    ;;

    *)
    echo "usage: $0 { start | stop }" >&2
    exit 1
    ;;

esac 
and give permissions of 
chmod 755
chkconfig --add freeswitch
chkconfig --level 5 freeswitch on
chkconfig --list freeswitch
 


Read the File With Comma separte and copy the files to directory using Perl script

To read the File and copy the files to the directorywith comma separate need the the Package

  FileHandle.


#!/usr/bin/perl

use FileHandle;

if (@ARGV != 2) {
    print  " the argument is invalided \n"   ;
  exit 1;
}

$file_name = shift;
$output_path = shift;

print "$file_name ,$output_path \n";
$dir1 = "/opt/test";

open (FILE, "$file_name") or die "Cannot open $file_name $!\n";

@fields;
while () {
      chomp;
    @fields = split(/\,/);
   
      foreach my $item (@fields) {
      #print "$item  \n";
   }

    if ($#fields < 1) {
        print " Error in file format \n";
        next;
    }
   
   $file_id = $fields[4];
    print  " $file_id \n";        

    `cp -r "$dir1/$file_id" "$output_path/$file_id" `;

 }
close (FILE);



give permissons to file of script and

perl test.pl new.txt  /opt/test/copy

new.txt file format
1,wer,2,root,welcome.txt
2,wee,3,root,welcome1.log
3,333,4,root,sysd.log

Reading the Csv File and inser into Database using the Perl Script

To read the CSV File or Xls file the following packages is needed

Spreadsheet::ParseExcel::Simple
Spreadsheet::ParseExcel
DBI

the perl script


#!/usr/bin/perl

use DBI;
use Spreadsheet::ParseExcel::Simple;
use Spreadsheet::ParseExcel;

my $workbook = Spreadsheet::ParseExcel::Workbook->Parse('Agent Details.xls');

#==================================================
#==================================================
foreach $sheet (@{$workbook->{Worksheet}}) {

       foreach $col ($sheet->{MinCol} .. $sheet->{MaxCol}) {
              
               if ($sheet->{Cells}[0][$col]->{Val} eq "COL1") {
                       $col1= $col;
               }
               if ($sheet->{Cells}[0][$col]->{Val} eq "COL2") {
                       $col2= $col;
               }
               if ($sheet->{Cells}[0][$col]->{Val} eq "COL3") {
                       $col3= $col;
               }
               if ($sheet->{Cells}[0][$col]->{Val} eq "COL4") {
                       $col4= $col;
               }
       }
#==================================================
#==================================================
       foreach $row ($sheet->{MinRow}+1 .. $sheet->{MaxRow}) {

                  $col_1 = $sheet->{Cells}[$row][$col1]->{Val};
                  $col_2 = $sheet->{Cells}[$row][$col2]->{Val};
                  $col_3 = $sheet->{Cells}[$row][$col3]->{Val};
                  $col_4 = $sheet->{Cells}[$row][$col4]->{Val};

#==================================================
#==================================================
                if( $col_1!= ' ' && $col_2!= ' ' && col_3 !=' ' && col_4 != ' ')
                {
                    $dbh = DBI->connect("DBI:mysql:databasename:localhost:3306", "username", "password")
                     or die "Couldn't connect to database: " . DBI->errstr;
                    $stmt = "INSERT INTO databasename.tablename (col1,col2,col3,col4) values ( '$col_1' ,'$col_2','$col_3','$col_4');";
                    $affected_rows = $dbh->do($stmt);
                }
       }
}
#==================================================