Saturday 29 July 2017

Tomcat14-Troubleshooting-Logs

Troubleshooting Tomcat using log files

  • We have already seen how to troubleshoot tomcat instance based on the catalina.out log
  • By default tomcat writes some different logs as defined in the catalina script into the logs directory
  • Here we can see some other logs
    • localhost.YYYY-MM-DD.log
      • This log contains information about the server and the application on that server
      • We can see information like contextInitialized() and contextDestroyed()
      • Everytime we start or stop applications will be logged here as the context Initialized or Destroyed
    • localhost_access_log.YYYY-MM-DD.txt
      • This log contains information about the access from clients to our web application
      • Here we can see each and every action took place while navigating through the client. We can see the activities done through the client.
      • From this log we can log how many users accessing what information.
    • host-manager.YYYY-MM-DD.log
      • This log will say how and when the HostManager is accessed from the GUI
    • manager.YYYY-MM-DD.log
      • This log will say how and when the Manager App is accessed from the GUI

Sunday 23 July 2017

Tomcat13-Troubleshooting-Catalina-Logs

Troubleshooting Tomcat using Catalina log

  • We can get the catalina logs in $CATALINA_HOME/logs
  • Here we'll have a catalina.out
  • We can see some huge info in that log file. If we want to what exactly happens since the start of tomcat then we need to log that into a new file.
  • By default we have log rotation enabled on this logs.
  • For now let us stop tomcat and nullify the catalina.out file and then start tomcat to have some new info in the catalina.out
# service tomcat stop
# cd $CATALINA_HOME/logs
# :> catalina.out (or) cat /dev/null > catalina.out
# ll catalina.out
-rw-r----- 1 root root 0 Feb 18 16:19 catalina.out

# service tomcat start
# ll catalina.out
-rw-r----- 1 root root 17341 Feb 18 16:21 catalina.out
  • We can see the log being written after the start of tomcat.
  • Now we can notice the instance giving some information about the starting of tomcat, about the configuration for the startup, path of the servlet instance for which the log is responsible for, when the instance and the webapplications have started.
  • Also as we are not hosting any production applications on it we don't have any other information logged continuously to it as there is no running transaction on it.
  • And the last line in this log at this moment tells us how much time the tomcat instance has took to startup.
  • We can see how the issues can be logged in this log file.
  • As we previously configured another tomcat instance with some different port numbers let us change the config file of the first instance to use the connector port as 8010
  • Let us stop the tomcat instance, do the above change, start the second instance and then start the first instance.
     # cd $CATALINA_HOME
     # ./bin/catalina.sh stop
     # :> logs/catalina.out
     # vi conf/server.xml
    
    • Change port 8009 with 8010 and save the config file
     # cd $CATALINA_HOME2
     # ./bin/catalina.sh start
     # ps -ef | grep catalina | grep -v grep
     # cd $CATALINA_HOME
     # ./bin/catalina.sh start
     # ps -ef | grep catalina | grep -v grep
    
    • We can see 2 tomcat instances running, let us check the catalina.out log for the errors. We will see the following error in the log.
     18-Feb-2017 16:39:33.802 SEVERE [main] org.apache.catalina.core.StandardService.initInternal Failed to initialize connector [Connector
     [AJP/1.3-8010]]
     org.apache.catalina.LifecycleException: Failed to initialize component [Connector[AJP/1.3-8010]]
       at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:112)
       at org.apache.catalina.core.StandardService.initInternal(StandardService.java:549)
       at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)
       at org.apache.catalina.core.StandardServer.initInternal(StandardServer.java:875)
       at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)
       at org.apache.catalina.startup.Catalina.load(Catalina.java:606)
       at org.apache.catalina.startup.Catalina.load(Catalina.java:629)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       at java.lang.reflect.Method.invoke(Method.java:498)
       at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:311)
       at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:494)
     Caused by: org.apache.catalina.LifecycleException: Protocol handler initialization failed
       at org.apache.catalina.connector.Connector.initInternal(Connector.java:970)
       at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)
       ... 12 more
     Caused by: java.net.BindException: Address already in use
       at sun.nio.ch.Net.bind0(Native Method)
       at sun.nio.ch.Net.bind(Net.java:433)
       at sun.nio.ch.Net.bind(Net.java:425)
       at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
       at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
       at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:210)
       at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:972)
       at org.apache.tomcat.util.net.AbstractJsseEndpoint.init(AbstractJsseEndpoint.java:237)
       at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:558)
       at org.apache.catalina.connector.Connector.initInternal(Connector.java:968)
       ... 13 more
    
    • Here we need to see the below lines
     org.apache.catalina.core.StandardService.initInternal Failed to initialize connector [Connector
         [AJP/1.3-8010]]
     Caused by: java.net.BindException: Address already in use
    
    • This tells us that the application is failed to initialize the AJP Connector Port on 8010 as it is addressed already in use
    • Now to fix this let us stop tomcat and change the port back to 8009 in the $CATALINA_HOME/conf/server.xml
    • After this start both the instances and can notice that we don't have any issues in either of the instance

Friday 21 July 2017

Tomcat12-Troubleshooting-Java-Exceptions

Troubleshooting Common Java Exceptions

  • Below is an example of NullPointer Exception
     Exception in thread "main" java.lang.NullPointerException
      at com.example.myproject.Object1.getValue(Object1.java:16)
      at com.example.myproject.Object2.getNumericValue(Object2.java:25)
      at com.example.myproject.Object3.main(Object3.java:14)
    
    • In this case we need to navigate to the file Object1 and review the method called getValue() to determine what happened
    • The remaining lines give the further information about how the exception was arrived at.
  • Most of the times we'll have something different to the above, but the above given can be taken as a basic example of how to find out the issue.
  • Let us look at the below log once
     Exception in thread "main" java.lang.NullPointerException
      at com.example.myproject.Object1.getValue(Object1.java:16)
      at com.example.myproject.Object2.getNumericValue(Object2.java:25)
      at com.example.myproject.Object3.main(Object3.java:14)
     Caused by: java.lang.IllegalReferenceException
      at com.example.myproject.Object1.getParentValue(Object1.java:22)
    
    • This means that we have a NullPointer exception in Object1 at the getValue() method occured as Object1 and getParentValue() method thrown a IllegalReferenceException and this is the initial cause of the problem.
    • If we have multiple Caused by sections then the lowest is the initial cause of the problem.

Thursday 20 July 2017

Tomcat11-Tuning-Tomcat-Ports

Tuning Tomcat Ports

  • To have the tomcat to be hosted in secured way we need to run it using the SSL certs.
  • By default we will have a section for this.
  • All we need to do is that we need to uncomment the Connector section with the SSL details.
  • In the same way we can change the port numbers to any other open port numbers.
  • We might need to change the port numbers to secure our tomcat application.

Wednesday 19 July 2017

Tomcat10-Basic-Clustering

Basic Clustering

  • Till now we have 1 tomcat instance running and now we'll learn how to loadbalance multiple tomcat instances.
  • For this let us have another tomcat instance configured first.
  • Go to the /opt directory and copy tomcat8 to tomcat8-n2
# cp -prv tomcat8 tomcat8-n2
  • Now we know that we are using some ports for tomcat say 8080, 8005, 8009
  • For our additional tomcat instance we need to change the ports. If we don't then we might need to experience port conflict
  • I am going to edit server.xml config file in the new node and increment the port numbers by 1
# cd /opt/tomcat8-n2/conf/
# vi server.xml

8005 -> 8006
8080 -> 8081
8443 -> 8444
8009 -> 8010
  • Now you need to add your new Catalina home, base and temp directories
  • First you need to change the catalina.sh script and redirect the above 3 variables to the new variables
  • Edit the catalina.sh script and add the following lines
# vi /opt/tomcat-n2/bin/catalina.sh

CATALINA_BASE=$CATALINA_BASE2
CATALINA_HOME=$CATALINA_HOME2
CATALINA_TMPDIR=$CATALINA_TMPDIR2
  • Now edit /etc/profile file and set our new directories
# vi /etc/profile

CATALINA_BASE2=/opt/tomcat8-n2
CATALINA_HOME2=/opt/tomcat8-n2
CATALINA_TMPDIR2=/opt/tomcat8-n2/temp
  • Now we need to edit /etc/nginx/vhost.d/default.conf file to add the upstream directive to loadbalance
  • Edit that file and add the following at the top of the file
upstream myTomcat {
 server localhost:8080;
 server localhost:8081;
}
  • Now we need to change the proxy_pass
proxy_pass http://myTomcat;

Saturday 15 July 2017

Tomcat09-Proxy-Connection

Proxy Connection for Tomcat

  • For proxy we are using Nginx, so let us install nginx
While installing nginx I got issue with "libunwind" package. So I've manually downloaded that package and installed it.
# wget ftp://195.220.108.108/linux/centos/7.3.1611/os/x86_64/Packages/libunwind-1.1-5.el7_2.2.x86_64.rpm && rpm -ivh libunwind-1.1-5.el7_2.2.x86_64.rpm

# yum install nginx -y
  • Now for our default configuration files let us have a directory
# cd /etc/nginx
# mkdir vhosts.d
  • Now we need to change the configuration as per our requirement
    • Find the include statement for the config files and add another for our new directory
    • After that remove the server definition under the include statement as we'll have it in our new config
# vi nginx.conf

 include /etc/nginx/vhost.d/*.conf;
  • Now lets go to the vhost.d directory and create a new config file
    • Copy the sample server definition after deleting the existing server definition in the main config file
    • Paste it in the new config file and modify as per your requirements
# cd vhost.d/
# vi default.conf

server {
 listen 80;

 root /opt/tomcat8/webapps/hello/;
 index index.php index.html index.htm;

 server_name localhost;

 location / {
  try_files $uri $uri/ /index.html;
 }

 location ~ \.php$ {
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $remote_addr;
  proxy_set_header Host $host;
  proxy_pass http://127.0.0.1:8080;
 }

 location ~ /\.ht {
  deny all;
 }
}
  • Start Nginx
  • Verify that you are able to hit the webapps/hello without any issue.
# elinks http://localhost

Friday 7 July 2017

Tomcat08-Deploying_WebApp

Deploying Web Application on Tomcat

  • For deploying a web application oon tomcat we should have .war file of the application
  • Once we have the .war file then we need to put it in webapps directory
  • After copying the .war file restart tomcat
  • Another way of deploying application on tomcat is through the Manager GUI
  • Go to the deploy section
  • Browse and select your application
  • Click on Deploy
  • Now to use your application you need to use your url like below
http://< IP / HOSTNAME of Tomcat >:<PORT>/<APPLICATION_NAME_WITHOUT_WAR EXTENSION>

Ex:

http://dev02.linux-library.com:8080/jenkins