UnixServerAdmin

Server Administration & Management

tomcat_httpsd.conf_virtual-hosting-Secure for Java + PHP

We can run java and PHP based websites on same servers using SSL Certificate,
Here are six websites, inwhich four are based on Java and two are based on PHP

1. https://revenue.unixserveradmin.com – Java
2. https://revenue.unixserveradmin.com – Java
3. https://revenue.unixserveradmin.com – Java
4. https://revenue.unixserveradmin.com – Java
5. https://noc.unixserveradmin.com – PHP
6. https://online.unixserveradmin.com – PHP

# vim /etc/httpd/conf/httpd.conf

Line No. 201 to 212

#############################################################
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/conf/worker.properties
JkLogFile /var/log/httpd/mod_jk.log
JkLogLevel info
JkLogStampFormat “[%a %b %d %H:%M:%S %Y] ”
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat “%w %V %T”
JkEnvVar SSL_CLIENT_V_START
JkMount /revenue* tomcat1
JkMount /mis* tomcat2
JkMount /sms* tomcat3
JkMount /sla* tomcat4

#############################################################
For Java Hosting
#############################################################

NameVirtualHost *:443

<VirtualHost *:443>
ServerAdmin info@unixserveradmin.com
ServerName revenue.unixserveradmin.com
ServerAlias http://www.revenue.unixserveradmin.com
RewriteEngine On
RewriteLog logs/apache-mod_rewrite
RewriteRule ^/(.*)$ /revenue/$1[L,PT]
JkMount /* tomcat1
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log commo
SSLEngine on
SSLCertificateFile  /etc/pki/tls/certs/localhost.crt
SSLCertificateKeyFile  /etc/pki/tls/private/localhost.key
SSLCertificateChainFile  /etc/pki/tls/certs/ca-bundle.crt
</VirtualHost>

<VirtualHost *:443>
ServerAdmin info@unixserveradmin.com
ServerName mis.unixserveradmin.com
ServerAlias http://www.mis.unixserveradmin.com
RewriteEngine On
RewriteLog logs/apache-mod_rewrite
RewriteRule ^/(.*)$ /mis/$1[L,PT]
JkMount /* tomcat2
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
SSLEngine on
SSLCertificateFile  /etc/pki/tls/certs/localhost.crt
SSLCertificateKeyFile  /etc/pki/tls/private/localhost.key
SSLCertificateChainFile  /etc/pki/tls/certs/ca-bundle.crt
</VirtualHost>

<VirtualHost *:443>
ServerAdmin info@unixserveradmin.com
ServerName sms.unixserveradmin.com
ServerAlias http://www.sms.unixserveradmin.com
RewriteEngine On
RewriteLog logs/apache-mod_rewrite
RewriteRule ^/(.*)$ /sms/$1[L,PT]
JkMount /* tomcat3
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
SSLEngine on
SSLCertificateFile  /etc/pki/tls/certs/localhost.crt
SSLCertificateKeyFile  /etc/pki/tls/private/localhost.key
SSLCertificateChainFile  /etc/pki/tls/certs/ca-bundle.crt
</VirtualHost>

<VirtualHost *:443>
ServerAdmin info@unixserveradmin.com
ServerName sla.unixserveradmin.com
ServerAlias http://www.sla.unixserveradmin.com
RewriteEngine On
RewriteLog logs/apache-mod_rewrite
RewriteRule ^/(.*)$ /sms/$1[L,PT]
JkMount /* tomcat4
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
SSLEngine on
SSLCertificateFile  /etc/pki/tls/certs/localhost.crt
SSLCertificateKeyFile  /etc/pki/tls/private/localhost.key
SSLCertificateChainFile  /etc/pki/tls/certs/ca-bundle.crt
</VirtualHost>

#############################################################
For PHP Hosting
#############################################################

<VirtualHost *:443>
ServerAdmin info@unixserveradmin.com
DocumentRoot /var/www/html/noc/
ServerName noc.unixserveradmin.com
ServerAlias http://www.noc.unixserveradmin.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
SSLEngine on
SSLCertificateFile  /etc/pki/tls/certs/localhost.crt
SSLCertificateKeyFile  /etc/pki/tls/private/localhost.key
SSLCertificateChainFile  /etc/pki/tls/certs/ca-bundle.crt
</VirtualHost>

<VirtualHost *:443>
ServerAdmin info@unixserveradmin.com
DocumentRoot /var/www/html/online/
ServerName online.unixserveradmin.com
ServerAlias http://www.online.unixserveradmin.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
SSLEngine on
SSLCertificateFile  /etc/pki/tls/certs/localhost.crt
SSLCertificateKeyFile  /etc/pki/tls/private/localhost.key
SSLCertificateChainFile  /etc/pki/tls/certs/ca-bundle.crt
</VirtualHost>

#############################################################

Advertisements

April 10, 2014 Posted by | Apache, Tomcat | , | Leave a comment

tomcat_httpd.conf_virtual-hosting for Java + PHP

We can run java and PHP based websites on same servers, Here are six websites, in which four are based on Java and two are based on PHP

1. http://revenue.unixserveradmin.com – Java
2. http://revenue.unixserveradmin.com – Java
3. http://revenue.unixserveradmin.com – Java
4. http://revenue.unixserveradmin.com – Java
5. http://noc.unixserveradmin.com – PHP
6. http://online.unixserveradmin.com – PHP

# vim /etc/httpd/conf/httpd.conf

Line No. 201 to 212

#############################################################
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/conf/worker.properties
JkLogFile /var/log/httpd/mod_jk.log
JkLogLevel info
JkLogStampFormat “[%a %b %d %H:%M:%S %Y] ”
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat “%w %V %T”
JkEnvVar SSL_CLIENT_V_START
JkMount /revenue* tomcat1
JkMount /mis* tomcat2
JkMount /sms* tomcat3
JkMount /sla* tomcat4

#############################################################
For Java Hosting
#############################################################

Listen 80
NameVirtualHost *:80

<VirtualHost *:80>
ServerAdmin info@unixserveradmin.com
ServerName revenue.unixserveradmin.com
ServerAlias http://www.revenue.unixserveradmin.com
RewriteEngine On
RewriteLog logs/apache-mod_rewrite
RewriteRule ^/(.*)$ /revenue/$1[L,PT]
JkMount /* tomcat1
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
ServerAdmin info@unixserveradmin.com
ServerName mis.unixserveradmin.com
ServerAlias http://www.mis.unixserveradmin.com
RewriteEngine On
RewriteLog logs/apache-mod_rewrite
RewriteRule ^/(.*)$ /mis/$1[L,PT]
JkMount /* tomcat2
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
ServerAdmin info@unixserveradmin.com
ServerName sms.unixserveradmin.com
ServerAlias http://www.sms.unixserveradmin.com
RewriteEngine On
RewriteLog logs/apache-mod_rewrite
RewriteRule ^/(.*)$ /sms/$1[L,PT]
JkMount /* tomcat3
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
ServerAdmin info@unixserveradmin.com
ServerName sla.unixserveradmin.com
ServerAlias http://www.sla.unixserveradmin.com
RewriteEngine On
RewriteLog logs/apache-mod_rewrite
RewriteRule ^/(.*)$ /sms/$1[L,PT]
JkMount /* tomcat4
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>

#############################################################
For PHP Hosting
#############################################################

<VirtualHost *:80>
ServerAdmin info@unixserveradmin.com
DocumentRoot /var/www/html/noc/
ServerName noc.unixserveradmin.com
ServerAlias http://www.noc.unixserveradmin.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>

<VirtualHost *:80>
ServerAdmin info@unixserveradmin.com
DocumentRoot /var/www/html/online/
ServerName online.unixserveradmin.com
ServerAlias http://www.online.unixserveradmin.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>

#############################################################

March 30, 2014 Posted by | Apache, Tomcat | , | Leave a comment

worker.properties_virtual-hosting

##############################################################
# workers to contact, that’s what you have in your httpd.conf
# here are four different tomcat server, running on different ports
# tomcat1 for revenue portal
# tomcat2 for mis portal
# tomcat3 for sms portal
# tomcat for sla portal
##############################################################

worker.list=tomcat1, tomcat2, tomcat3, tomcat4

# setup tomcat1
worker.tomcat1.port=8009
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13

# setup tomcat2
worker.tomcat2.port=8109
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13

# setup tomcat3
worker.tomcat3.port=8209
worker.tomcat3.host=localhost
worker.tomcat3.type=ajp13

# setup tomcat4
worker.tomcat4.port=8309
worker.tomcat4.host=localhost
worker.tomcat4.type=ajp13
##############################################################

# worker.list –> Describe the workers that are available to Apache via a list
# ajp13 –> This type of worker represents a running Tomcat instance

March 20, 2014 Posted by | Apache, Tomcat | , | Leave a comment

How to hide web server version in Tomcat

Add the following attributes to the <Connector> in Tomcat’s server.xml to hide web server version in Tomcat at line no- 73 :-

# vim apache-tomcat/conf/server.xml

<Connector port=”8080″ protocol=”HTTP/1.1″
connectionTimeout=”20000″
redirectPort=”8443″
server=”Tomcat” />

April 7, 2013 Posted by | Security, Tips & Tricks, Tomcat | , , , , | 1 Comment

How to enable gzip compression in Tomcat

Add the following attributes to the <Connector> in Tomcat’s server.xml to enable gzip compression of responses at line no.- 70 :-

# vim apache-tomcat/conf/server.xml

    compression=”on” compressionMinSize=”2048″
    compressableMimeType=”text/html,text/xml,text/csv,text/css,text/javascript”

Before

<Connector port=”8080″ protocol=”HTTP/1.1″
                 connectionTimeout=”20000″
                 redirectPort=”8443″ /

After

<Connector port=”8080″ protocol=”HTTP/1.1″
               connectionTimeout=”20000″
               redirectPort=”8443″
               compression=”on” compressionMinSize=”2048″
               compressableMimeType=”text/html,text/xml,text/csv,text/css,text/javascript” />

March 23, 2013 Posted by | Tomcat | , | 1 Comment

How to enable Tomcat for a domain in cPanel

To enable .jsp pages for a domain

# /scripts/addservlets –domain=example.com

To disable .jsp pages for a domain

# /scripts/remservelets –domain=example.com

In some cases, these scripts won’t work. Manually check the setting for the domain.

1. Open up the Apache configuration file /usr/local/apache/conf/httpd.conf

# vim /usr/local/apache/conf/httpd.conf

2. Check if the directive given below in the virtual host entry of domain, if not add it.

———————————————————————————————————————–
Include “/usr/local/apache/conf/userdata/std/2/username/domainname/*.conf”
———————————————————————————————————————–

3. Now navigate to the directory

# cd /usr/local/apache/conf/userdata/std/2/

4. check whether the folder named username(replace username with appropriate username) exist if not, create it.

# mkdir username
# cd username
# cd example

5. If the directory “example” is not present, create it

6. Check for the file cp_jkmount.conf here, if it is not present, create it with the following contents
——————————————–
 <IfModule mod_jk.c>
      JkMount /*.jsp ajp13
      JkMount /servlet/* ajp13
    JkMount /servlets/* ajp13
    JkMount /*.do ajp13
 </IfModule>
——————————————–

7. Check the file /usr/local/jakarta/tomcat/conf/server.xml Add the entry for host http://www.example.com

# vim /usr/local/jakarta/tomcat/conf/server.xml

——————————————————————————————————-
 <Host name=”example” appBase=”/home/username/public_html”>
    <Alias>www.example</Alias>
      <Context path=”” reloadable=”true”
               docBase=”/home/username/public_html” debug=”1″/>
      <Context path=”/manager” debug=”0″ privileged=”true”
        docBase=”/usr/local/jakarta/tomcat/server/webapps/manager”>
      </Context>
     </Host>
——————————————————————————————————-

8. Restart the services

# /usr/local/jakarta/tomcat/bin/shutdown.sh

# /usr/local/jakarta/tomcat/bin/startup.sh

# /scripts/restartsrv_tomcat

9. Restart Apache

October 19, 2012 Posted by | cPanel, Tomcat | , , | Leave a comment

tomcat_service.sh

#!/bin/bash
# script for tomcat restart script ##
# tomcat_service.sh #########
export BASE=/usr/local/apache-tomcat/bin
prog=apache-tomcat-7.0.16

stat() {
if [ `ps auxwwww|grep $prog|grep -v grep|wc -l` -gt 0 ]
then
echo Tomcat is running.
else
echo Tomcat is not running.
fi
}

case “$1” in
start)

if [ `ps auxwwww|grep $prog|grep -v grep|wc -l` -gt 0 ]
then
echo Tomcat seems to be running. Use the restart option.
else
$BASE/startup.sh 2>&1 > /dev/null
fi
stat
;;
stop)
$BASE/shutdown.sh 2>&1 > /dev/null
if [ `ps auxwwww|grep $prog|grep -v grep|wc -l` -gt 0 ]
then
for pid in `ps auxwww|grep $prog|grep -v grep|tr -s ‘ ‘|cut -d ‘ ‘ -f2`
do
kill -9 $pid 2>&1 > /dev/null
done
fi
stat
;;
restart)

if [ `ps auxwwww|grep $prog|grep -v grep|wc -l` -gt 0 ]
then
for pid in `ps auxwww|grep $prog|grep -v grep|tr -s ‘ ‘|cut -d ‘ ‘ -f2`
do
kill -9 $pid 2>&1 > /dev/null
done
fi
$BASE/startup.sh 2>&1 > /dev/null
stat
;;
status)
stat
;;
*)
echo “Usage: tomcat start|stop|restart|status”
esac

June 16, 2012 Posted by | Shell Script, Tomcat | , | Leave a comment

index.jsp for tomcat cluster with HA

<%@ page language=”java” %>
<HTML>
<HEAD>
<TITLE>Login using jsp</TITLE>
</HEAD>
<BODY>
<h1><font color=”red”>Index Page by Tomcat-2 Node-2</font></h1>
<h2><font color=”blue”>This is test page of Tomcat-2 of NODE-2</font></h2>
<table> align=”centre” border=”1″
<h2>></h2>
<tr>
<td>Session ID –> </td>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on –> </td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</BODY>
</HTML>

March 23, 2012 Posted by | Apache, Cluster, Tomcat | , , | Leave a comment

worker.properties_cluster

##############################################################
# workers to contact, that’s what you have in your httpd.conf
worker.list=loadbalancer

# setup tomcat1
worker.tomcat1.port=8109
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1

# setup tomcat2
worker.tomcat2.port=8209
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1

# setup tomcat3
worker.tomcat3.port=8309
worker.tomcat3.host=localhost
worker.tomcat3.type=ajp13
worker.tomcat3.lbfactor=1

# setup the load-balancer
worker.loadbalancer.type=lb
worker.loadbalancer.method=R/S/T/B
worker.loadbalancer.balance_workers=tomcat1,tomcat2,tomcat3
worker.loadbalancer.sticky_session=True
#worker.loadbalancer.sticky_session_force=True

# Status worker for managing load balancer
worker.status.type=status
##################################################################

worker.list –> Describe the workers that are available to Apache via a list

ajp13 –> This type of worker represents a running Tomcat instance

lb –> used for load balancing

status –> display useful information about how the load among the various Tomcat workers is distributed

Sticky sessions are an important feature if you rely on jSessionIDs and are not using any session-replication layer. If sticky_session is True a request always gets routed back to the node which assigned this jSessionID.
If that host should get disconnected, crash or become unreachable otherwise the request will be forwarded to another host in the cluster.

R :- Request –> If method is set to Request the balancer will use number of requests to find the best worker. Accesses will be distributed according to the lbfactor in a sliding time window. This is the default value and should be working well for most applications.

S :- Session –> If method is set to Session the balancer will use number of sessions to find the best worker. Accesses will be distributed according to the lbfactor in a sliding time window. Because the balancer does not keep any state, it actually does not know the number of sessions. Instead it counts each request without a session cookie or URL encoding as a new session. This method will neither know, when a session is being invalidated, nor will it correct its load numbers according to session timeouts or worker failover. This method should be used, if sessions are your limiting resource, e.g. when you only have limited memory and your sessions need a lot of memory.

T :- Traffic –> If set to Traffic the balancer will use the network traffic between JK and Tomcat to find the best worker. Accesses will be distributed according to the lbfactor in a sliding time window. This method should be used, if network to and from the backends is your limiting resource.

B :- Busyness –> If set to Busyness the balancer will pick the worker with the lowest current load, based on how many requests the worker is currently serving. This number is divided by the workers lbfactor, and the lowest value (least busy) worker is picked. This method is especially interesting, if your request take a long time to process, like for a download application.
##############################################################

March 19, 2012 Posted by | Apache, Cluster, Tomcat | , , | Leave a comment

tomcat_ssl.conf_cluster

# vim /etc/httpd/conf.d/ssl.conf

Line No. 83 to 93

##################################################################
# LoadModule jk_module modules/mod_jk.so
# JkWorkersFile /etc/httpd/conf/worker.properties
JkLogFile /var/log/httpd/mod_jk.log
JkLogLevel info
JkLogStampFormat “[%a %b %d %H:%M:%S %Y] ”
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat “%w %V %T”
JkEnvVar SSL_CLIENT_V_START
Jkmount /* loadbalancer
# JkMount /examples/*.jsp worker1
##################################################################

March 17, 2012 Posted by | Apache, Cluster, Tomcat | , , | Leave a comment

tomcat_httpd.conf_cluster

# vim /etc/httpd/conf/httpd.conf

Line No. 201 to 212

##################################################################
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/conf/worker.properties
JkLogFile /var/log/httpd/mod_jk.log
JkLogLevel info
JkLogStampFormat “[%a %b %d %H:%M:%S %Y] “
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat “%w %V %T”
JkEnvVar SSL_CLIENT_V_START
Jkmount /* loadbalancer
# JkMount /examples/*.jsp worker1
##################################################################

March 15, 2012 Posted by | Apache, Cluster, Tomcat | , , | Leave a comment

Tomcat Configuration Cluster

###################################################################
Horizontal Tomcat Clustering ( on Multiple Physical Machine, single Tomcat)

Browser <—> Hardware Server-1(tomcat1), Hardware Server-2(tomcat2), Hardware Server-3(tomcat3)

Grouping multiple physical servers into a cluster.

A horizontal cluster consists of a cluster of servers that are exposed to browser clients as a single virtual server.
Horizontal clusters help to increase application scalability, performance, and robustness

###################################################################
Vertical Tomcat Clustering ( on Single Physical Machine, Multiple Tomcat)

Browser <—> Hardware Server (tomcat1, tomcat2, tomcat3)

A vertical cluster is like a horizontal cluster, except that rather than use several server machines linked together, vertical clusters use a single machine with multiple CPUs. Vertical clusters help to increase scalability on multiprocessor computers since they distribute work to several processes. Each process runs on a different CPU.

Clustering Tomcat Servlet Engines is interesting for two reasons: load balancing and failover.

###################################################################
Redirection requests to the mod_jk load balancer

###################################################################
Tomcat Configuration

The Tomcat instance must listen on the same port as is specified in the corresponding worker’s section in worker.properties.

The Engine jvmRoute property should correspond to the worker name in the worker.properties file or
the load balancer will not be able to handle stickyness.

<Engine name=”Catalina” defaultHost=”localhost” jvmRoute=”tomcat1″>

###################################################################

———————————————————————————-
Topics ————————– Tomcat1 —- Tomcat2 —- Tomcat3 —-
———————————————————————————-
Server Shutdown Port —–   8105 ——– 8205 ———– 8305 ——-
———————————————————————————-
Server Connector Port —– 8081 ——- 8082 ————- 8083 ——-
———————————————————————————-
AJP Connector Port ——– 8109 ——- 8209 ————- 8309 ——-
———————————————————————————-
jvmRoute ——————- tomcat1 —- tomcat2 —– tomcat3 —-
———————————————————————————-

—————————————————————————————————————–
# vim /usr/local/apache-tomcat/conf/server.xml

Line-22 :- <Server port=”8205″ shutdown=”SHUTDOWN”> # Server Shutdown Port, By Default 8005
Line-70 :- <Connector port=”8082″ protocol=”HTTP/1.1″ # Server Connector Port, By Default 8080
Line-91 :- <Connector port=”8209″ protocol=”AJP/1.3″ redirectPort=”8443″ /> # AJP Connector Port, By Default 8009
Line-103 :- <Engine name=”Catalina” defaultHost=”localhost” jvmRoute=”node1″> #
—————————————————————————————————————–

—————————————————————————————————————–
# vim /usr/local/apache-tomcat/conf/web.xml

Line-23 :- <Context distributable=”true” />

To indicate to a Servlet Container that the application can be clustered, a Servlet 2.4 standard <distributable/> element is placed into the applications deployment descriptor (web.xml). If this element is not added, the session maintained by this application across the three Tomcat instances will not be shared. You can also add it to the Context element.
—————————————————————————————————————–

March 13, 2012 Posted by | Cluster, Tomcat | , | Leave a comment

How to rotate tomcat logs

# cat /etc/logrotate.d/tomcat

/usr/local/tomcat/logs/catalina.out {
compress
copytruncate
create 644 root root
rotate 30
size 1024M
}

/usr/local/tomcat/logs/localhost.*.log {
compress
copytruncate
create 644 root root
rotate 30
size 1024M
}

/usr/local/tomcat/logs/localhost.*.log {
compress
copytruncate
create 644 root root
rotate 30
size 128M
}

/usr/local/tomcat/logs/host-manager.*.log {
compress
copytruncate
create 644 root root
rotate 30
size 1M
}

/usr/local/tomcat/logs/catalina.*.log {
compress
copytruncate
create 644 root root
rotate 30
size 1M
}

/usr/local/tomcat/logs/localhost_access_log.*.txt {
compress
copytruncate
create 644 root root
rotate 30
size 24M
}

daily :- rotates the catalina.out daily
rotate 7 :- keeps at most 7 log files
compress :- compresses the rotated files
size 10M :- rotates if the size of catalina.out is bigger than 10M
copytruncate :- Truncate the original log file in place after creating a copy, instead of moving the old log file and optionally creating a new one, It can be used when some program can not be told to close its logfile and thus might continue writing (appending) to the previous log file forever. Note that there is a very small time slice between copying the file and truncating it, so some logging data might be lost. When this option is used, the create option will have no effect, as the old log file stays in place.

January 17, 2012 Posted by | Tips & Tricks, Tomcat | , , | 1 Comment

How to configure Tomcat to run as a Service

We here is following steps to run Tomcat as a service and create a simple Start/Stop/Restart script, as well as to start Tomcat at boot. Change to the /etc/init.d directory and create a script called ‘tomcat’ as shown below.

# cd /etc/init.d  

# vi tomcat  

——————————————————————–
#!/bin/bash  
# description: Tomcat Start Stop Restart  
# processname: tomcat  
# chkconfig: 234 20 80  
JAVA_HOME=/usr/local/jdk1.6.0_27
export JAVA_HOME  
PATH=$JAVA_HOME/bin:$PATH  
export PATH  
CATALINA_HOME=/usr/local/apache-tomcat-7.0.11
case $1 in  
start)  
sh $CATALINA_HOME/bin/startup.sh  
;;   
stop)     
sh $CATALINA_HOME/bin/shutdown.sh  
;;   
restart)  
sh $CATALINA_HOME/bin/shutdown.sh  
sh $CATALINA_HOME/bin/startup.sh  
;;   
esac      
exit 0
——————————————————————–

The above script is simple and contains all of the basic elements you will need to get going. As you can see, we are simply calling the startup.sh and shutdown.sh scripts located in the Tomcat bin directory (/usr/local/apache-tomcat-7.0.11/bin). You can adjust your script according to your needs and, in subsequent posts, we’ll look at additional examples. CATALINA_HOME is the Tomcat home directory (/usr/local/apache-tomcat-7.0.11)

Now, set the permissions for your script to make it executable:

# chmod 755 /etc/init.d/tomcat  

We now use the chkconfig utility to have Tomcat start at boot time. In my script above, I am using chkconfig: 234 20 80. 234 are the run levels and 20 and 80 are the stop and start priorities respectively. You can adjust as needed.

# chkconfig –add tomcat  

# chkconfig –level 234 tomcat on  

Verify it:

# chkconfig –list tomcat  
tomcat          0:off   1:off   2:on    3:on    4:on    5:off   6:off

December 3, 2011 Posted by | Java, Shell Script, Tips & Tricks, Tomcat | , , , , | Leave a comment

Tomcat Application Server

Apache Tomcat (or Jakarta Tomcat or simply Tomcat) is an open source servlet container developed by the Apache Software Foundation (ASF). Tomcat implements the Java Servlet and the JavaServer Pages (JSP) specifications from Oracle Corporation, and provides a “pure Java” HTTP web server environment for Java code to run.

Tomcat should not be confused with the Apache web server, which is a C implementation of an HTTP web server; these two web servers are not bundled together, although they are frequently used together as part of a server application stack. Apache Tomcat includes tools for configuration and management, but can also be configured by editing XML configuration files.

Components :- Tomcat was released with Catalina (a servlet container), Coyote (an HTTP connector) and Jasper (a JSP engine).

Catalina :–> is Tomcat’s servlet container. Catalina implements Sun Microsystems’ specifications for servlet and JavaServer Pages (JSP). In Tomcat, a Realm element represents a “database” of usernames, passwords, and roles (similar to Unix groups) assigned to those users. Different implementations of Realm allow Catalina to be integrated into environments where such authentication information is already being created and maintained, and then utilize that information to implement Container Managed Security as described in the Servlet Specification.

Coyote :–> is Tomcat’s HTTP Connector component that supports the HTTP 1.1 protocol for the web server or application container. Coyote listens for incoming connections on a specific TCP port on the server and forwards the request to the Tomcat Engine to process the request and send back a response to the requesting client.

Jasper :–> is Tomcat’s JSP Engine. Tomcat 5.x uses Jasper 2, which is an implementation of the Sun Microsystems’s JavaServer Pages 2.0 specification. Jasper parses JSP files to compile them into Java code as servlets (that can be handled by Catalina). At runtime, Jasper detects changes to JSP files and recompiles them.

November 9, 2011 Posted by | Java, Tomcat | , , | Leave a comment

Application Servers

An application server is a software framework that provides an environment in which applications can run, no matter what the applications are or what they do.It is dedicated to the efficient execution of procedures (programs, routines, scripts) for supporting the construction of applications.

The term was originally used when discussing early client–server systems to differentiate servers that run SQL services and middleware servers from file servers.

Later, the term took on the meaning of Web applications, but has since evolved further into that of a comprehensive service layer. An application server acts as a set of components accessible to the software developer through an API defined by the platform itself. For Web applications, these components are usually performed in the same machine where the Web server is running, and their main job is to support the construction of dynamic pages.

However, present-day application servers target much more than just Web page generation: they implement services like clustering, fail-over, and load-balancing, so developers can focus on implementing the business logic.

Normally the term refers to Java application servers. When this is the case, the application server behaves like an extended virtual machine for the running applications, transparently handling connections to the database on one side, and connections to the Web client on the other. Other uses of the term may refer to the services that a server makes available or the  computer hardware on which the services run.

Java Application Servers

The Web modules include servlets, JavaServer Pages and Enterprise JavaBeans. Business logic resides in Enterprise JavaBeans – a modular server component providing many features, mostly improving application scalability. The Hibernate project offers an EJB-3 container implementation for the JBoss application server. Tomcat from Apache,The EAServer is from Sybase inc.

A Java Server Page (JSP) (a servlet from Java – the Java equivalent of a CGI script) executes in a Web container. JSPs provide a way to create HTML pages by embedding references to the server logic within the page. HTML coders and Java programmers can work side by side by referencing each other’s code from within their own.

The application servers mentioned above mainly serve Web applications. Some application servers target networks other than web-based ones: Session Initiation Protocol servers, for instance, target telephony networks.

Web Server is a sub set but the Application server is a super set and hence encompasses the past server information that is been stored in database.

Microsoft .NET Framework

Microsoft positions their middle-tier applications and services infrastructure in the Windows Server operating system and the .NET Framework technologies in the role of an application server.

Advantages of application servers

Data and code integrity :–> By centralizing business logic on an individual server or on a small number of server machines, updates and upgrades to the application for all users can be guaranteed. There is no risk of old versions of the application accessing or manipulating data in an older, incompatible manner.

Centralized configuration :–> Changes to the application configuration, such as a move of database server, or system settings, can take place centrally.

Security :–> A central point through which service-providers can manage access to data and portions of the application itself counts as a security benefit, devolving responsibility for authentication away from the potentially insecure client layer without exposing the database layer.

Performance :–> By limiting the network traffic to performance-tier traffic the client–server model improves the performance of large applications in heavy usage environments.

Total Cost of Ownership (TCO) :–> In combination, the benefits above may result in cost savings to an organization developing enterprise applications. In practice, however, the technical challenges of writing software that conforms to that paradigm, combined with the need for software distribution to distribute client code, somewhat negate these benefits.

Transaction Support :–> A transaction represents a unit of activity in which many updates to resources (on the same or distributed data sources) can be made atomic (as an indivisible unit of work). End-users can benefit from a system-wide standard behaviour, from reduced time to develop, and from reduced costs. As the server does a lot of the tedious code-generation, developers can focus on business logic.

November 8, 2011 Posted by | Apache, Java, Tomcat | , , , | Leave a comment

Servlets and Applets

Servlets executes on Servers and have no graphical user interface.

Applets executes on browser, have graphical user interface. An applet is a small server side application that can be loaded and controlled on the browser by the client application. An applet has limited access to resources in order to ensure security.

November 7, 2011 Posted by | Java, Tomcat | , , , | Leave a comment

java.lang.OutOfMemoryError: PermGen space

While accessing the .jsp pages or restarting tomcat on the server you face the error “java.lang.OutOfMemoryError: PermGen space”.  The error occurs due to the memry outage for  the tomcat service on the server. By default the tomcat uses 128 MB of memroy. You can increase the memory with the steps below and get rid off the errors.

HTTP Status 500 –
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Servlet execution threw an exception
root cause
java.lang.OutOfMemoryError: PermGen space
note The full stack trace of the root cause is available in the Apache Tomcat/
Apache Tomcat

1) Edit file /usr/local/jakarta/tomcat/bin/catalina.sh

# vi /usr/local/jakarta/tomcat/bin/catalina.sh

2) Add following line into the catalina.sh file.

JAVA_OPTS=”-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms512m -Xmx1024m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:+DisableExplicitGC”

Which will look like

=======================================================================
# JSSE_HOME (Optional) May point at your Java Secure Sockets Extension
# (JSSE) installation, whose JAR files will be added to the
# system class path used to start Tomcat.
#
# CATALINA_PID (Optional) Path of the file which should contains the pid
# of catalina startup java process, when start (fork) is used
#
# $Id: catalina.sh 609438 2008-01-06 22:14:28Z markt $
# —————————————————————————–

JAVA_OPTS=”-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms512m -Xmx1024m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:+DisableExplicitGC”

# OS specific support. $var _must_ be set to either true or false.
cygwin=false
os400=false
darwin=false
=======================================================================

3) Save and exit

4) Restart Tomcat

October 30, 2011 Posted by | Tomcat | | Leave a comment

Error occurred during initialization of VM Could not reserve enough space for object heap Could not create the Java virtual machine

The following error is occur due to memory size assigned to Tomcat service is full.

———————————————————————————————————————–
Error occurred during initialization of VM Could not reserve enough space for object heap Could not create the Java virtual machine.
———————————————————————————————————————–

You need to run following commands from Shell

# cd /usr/local/jakarta/tomcat/bin/

# export _JAVA_OPTIONS=”-Xms20m -Xmx64m -XX:MinHeapFreeRatio=20 -XX:MaxHeapFreeRatio=40 -XX:NewSize=10m -XX:MaxNewSize=10m -XX:SurvivorRatio=6 -XX:TargetSurvivorRatio=80 -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled”

Once you run above command restart the Tomcat service

#./shutdown.sh

#./startup.sh

#./catalina.sh

October 8, 2011 Posted by | Java, Tomcat | , | Leave a comment

workers.properties

A file that describes the host(s) and port(s) used by the workers (Tomcat processes). A sample workers.properties can be found under the /etc/httpd/conf/ directory.

# vi /etc/httpd/conf/workers.properties

worker.list=jetty
worker.jetty.port=8009
worker.jetty.host=localhost “OR” X.X.X.X
worker.jetty.type=ajp13
worker.jetty.lbfactor=1

September 21, 2011 Posted by | Java, Tomcat | , , | Leave a comment