Java Tomcat Linux VPS hosting experience and Apache Tomcat Setup instructions

A beginners journey - The Search for a good hosting plan

This article is for anyone who is looking for an affordable and reliable Apache Tomcat hosting. My journey started a few weeks back when I first decided to host my Apache Tomcat web application on the internet. Well any lookup for term "Java Tomcat hosting" on google just got lots of paid company links and with little or no information as far as what is reliable, affordable and easy to use. The first thing I noticed is all the ads mention hosting available anywhere from $1.99 onwards with full 24/7 support. But after digging deeper I realized that most are PHP hosting and don't include Java support. After spending much time on the internet you will realize that you have very few choices and it cost more to host Java applications than PHP.

As a beginner my main objective was to keep the cost down as much as possible until traffic on my site increases and there is need for more capacity. With little knowledge of hosting my own website it seemed like a no brainer that I must go with shared hosting to keep myself far from the ins and outs of the technical know-how and just concentrate on hosting my site. So a few days later and doing hundreds of research on google I decided on a hosting company(name omitted) and took the dive. Well the next couple of weeks after that was a nightmare.

The hosting company provided support whenever I called then up. It took me a few days to realize the fact that calling is the best option, if you use chat, email or just open a ticket well nothing happens. The support guys were nice but they had little or no knowledge of Tomcat config. The basic setup they did was to redirect all my JSP files to Tomcat and everything else was processed by Apache Web server. After going back and forth for almost a week with the tech support I was able to get all my request processed thru Tomcat not just JSP files. Now I was all happy and ready to test my application on the Tomcat server.

After I deployed my first application manually, no idea why automatic WAR deployment wasn't setup, it didn't work. Now how do I look into the Tomcat error logs? Well guess what there is no way, all you have a shared Tomcat. So now you have to modify your application to add some logging such a log4j or send the stacktrace back in response. After I figured out all the issues(basically JDBC connections to MySQL) with my application it was time for some testing and you guess right I ran into another issue. This time "java.lang.OutOfMemoryError: PermGen space" error showed up. My small application was for testing only and had a simple SQL select statement. I had no clue what my Tomcat heap size was and the site stopped responding. Well a call to the tech support fixed the issue, they just restarted the Tomcat server. Well for the next few days the site stopped responding every few hours and I have to call tech support to restart my Tomcat. Well at that point I decided enough is enough and it's time to look for an alternate solution. The answer from the tech support is basically get a private JVM but costs even more money.

The final destination

Again it's time for google. But google is not of much help as most of the searches gets you hosting company links not reviews from actual people what worked especially for Java Tomcat setup. After considering the pros and cons I decided to give VPS (Virtual private server) a chance. It will cost a little more than a shared hosting and almost same as Private JVM but provides full freedom to do whatever setup you want. Its more like owning your own machine. Another reason to have a VPS is basically you can install the latest version of Tomcat whereas the default most companies provide is Tomcat 5.5 for shared hosting. Going thru various hosting company VPS plans I decided to go with Plesk Virtual Server Hosting for Business from myhosting.com, they have linux VPS plans for less than $20 a month. And for that money we get relatively better specs than some other plans. Also in future if there is need for more juice to the machine you can upgrade whatever components you want such as hard disk, memory, bandwidth etc.

You can do your own research and choose your own hosting company depending on your requirement. The lesson here is to avoid the shared hosting plan for Java Tomcat applications as you will get very little support for a Tomcat Server and you may know better than most of tech support guys. So just install Tomcat Server yourself and have full control as if its your own computer. Well signing up for linux VPS hosting from myhosting.com scared me at first as I am not a linux person. All the tomcat development I have done so far was on my Windows machine. But I choose the linux plan over Windows as linux plans are much affordable with higher specs and that is true with any hosting company. Now if you are like me with little or no knowledge of linux then don't worry its not that hard, this article will get you setup in no time.

The objective here to get a VPS linux hosting plan as its cheaper and then install Tomcat 6 (current default is 5.5 for most hosting companies) and pass all web request thru Apache web server. Basically the setup is standard configuration with Apache Web server in front of your Tomcat server. In my setup I have created a Apache virtual host and a Tomcat virtual host and connected them. That way I can host multiple sites on my Tomcat server. So lets get started ...

Step 1: Sign up for VPS hosting plan

As mentioned earlier I did sigup for Plesk VPS for Business plan with myhosting.com, by no means take that as my recommendation to signup with myhosting as I have not spent enough time with them to really give any input other than the fact so far my experience with them has been nice. In case you decide to go with myhosting then you can click on the banner link above to save an Additional 20% of any VPS HOSTING. Their buiness plan comes with parallel Plesk Panel. If you choose the cPanel administration or signup with another hosting company some parts of this article may not be relevant but you will still get the idea how to setup up the Tomcat. I choose not to take the cPanel yet to keep my cost down, one of my objectives. The signup process was smooth and within 2 hrs I received a courtesy call that everything has been setup. Also they schedule a second call with the Tech support if you have any questions.

Step 2: Login to VPS hosting plan

Java Tomcat hosting experience with myhosting

Step 3: Hosting control panel

After you login you are basically inside the hosting control panel. On the left side you will see the Configuration & Admin section, click on the VPS and then click on VPS Management. You will see a screen similar to this. Some values have been omitted for privacy.
Java Tomcat hosting experience with myhosting
There are a few things to note down on this screen as indicated by the colored arrows...
  • The green arrow points to your VPS root password.
  • The red arrow is the IP address of your VPS server.
  • The blue arrow points to the URL for the VPS server control panel.
You can click on the link and it will take you to the control panel where you can start, stop or restart your VPS. You also have access to the file manager, system service, logs etc. I will no go into details for that as that is outside the scope of this tutorial. You can find documentation for that in http://myhosting.com/kb/Main_Page.
Java Tomcat hosting experience with myhosting
I have my domain registered at godaddy.com and what I am going to do is create a subdomain and point that to the IP address of my VPS server. You can choose to point your main domain to the VPS IP address. My objective is to create a subdomain that takes any requests and sends them to the Java Tomcat server, for that reason I created a subdomain named try.mysamplecode.com and point to the IP address of my VPS.
 

Step 4: Login to Parallel Plesk Panel

Next you go back to your hosting panel and click on the link below VPS management named Parallel Plesk Panel. From their you can Login to the parallel plesk panel. You can note down these URLs and use them directly from your browser but I find it convenient to just click on the links from the control panel as they just login automatically. After you see the home page for the parallel plesk panel look for the Websites and Domains tab and select that. Click on Add New Domain
Java Tomcat hosting experience with myhosting
Only thing to add here is your domain name (try.mysamplecode.com) and the document root (testSite). Click OK to save. This will create a folder in /var/www/vhosts named try.mysamplecode.com (by your hostname). Inside the /var/www/vhosts/try.mysamplecode.com you will find the conf folder that has the include file for the Apache config virtual host that we just created. If you use the file manager in the VPS management screen you will be able to see all these folders. I use the FileZilla Ftp client and signon as root. You can do whatever is convenient for you. Inside the htttp.include file your will find that plesk automatically created your virtual hosts configuration for your Apache server. Here is a snippet ...

<VirtualHost 216.224.180.198:80>
    ServerName "try.mysamplecode.com:80"
        ServerAlias  "www.try.mysamplecode.com"
                        ServerAlias  "ipv4.try.mysamplecode.com"
                UseCanonicalName Off
.......
.......
.......

Step 5: Signon to Terminal using SSH

You can use the SSH in the VPS management screen or download PuTTY. The one in VPS management is web based needs plugin based on browser and kind of slow. I like PuTTY, anyway its up to you. Login to the SSH using root password.
Java Tomcat hosting experience with myhosting

Step 6: Find Java install directory and version

Most probably java will be already installed in your VPS, if not then you have to install based on your OS. Enter the following commands on the Terminal

java -version
if java is installed should give you something like this
java version "1.6.0_22"
OpenJDK Runtime Environment (IcedTea6 1.10.6) (rhel-1.25.1.10.6.el5_8-x86_64)
OpenJDK 64-Bit Server VM (build 20.0-b11, mixed mode)

which java
provides you with the Java home path, please note this down, my result was 
/usr/bin/java

Step 7: Install Tomcat 6

The default provided by the VPS was Tomcat 5.5, here is the link for Tomcat download

https://tomcat.apache.org/download-60.cgi

Please download the tar.gz version to your computer and then using either FTP or use VPS File manager put that in the /usr/local directory.

My Tomcat installation file name was apache-tomcat-6.0.35.tar.gz
On the terminal type the following commands

cd /usr/local 
tar xvzf apache-tomcat-6.0.35.tar.gz

This will install Tomcat on your VPS. You can go into the conf/tomcat-users.xml and setup some user profiles for admin and manager roles. 

Step 8: Setup JAVA_HOME environment variable

Tomcat needs JAVA_HOME to start.
Edit file /usr/profile and add the JAVA_HOME after HISTSIZE as shown below

HOSTNAME=`/bin/hostname`
HISTSIZE=1000  
JAVA_HOME=/usr/bin/java

Signoff from the Terminal and Sign back again. Type the command echo $JAVA_HOME to check if the value has been set.

Step 9: Start and Stop the Tomcat and check it out

Enter the following commands on your Terminal
cd /usr/local/apache-tomcat-6.0.35/bin
./startup.sh

You will see something like this on the Terminal when the Tomcat starts. In addition to that you can also check the Tomcat logs in /usr/local/apache-tomcat-6.0.35/logs
Using CATALINA_BASE:   /usr/local/apache-tomcat-6.0.35
Using CATALINA_HOME:   /usr/local/apache-tomcat-6.0.35
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-6.0.35/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/apache-tomcat-6.0.35/bin/bootstrap.jar
 

To check the Tomcat running enter the IP address of your VPS server and port 8080
http://yourVPSIPAddress:8080

Java Tomcat hosting experience with myhosting
If this gives an error first check the Tomcat logs if the server is running. If it looks good, most probably it is the Firewall on your VPS. So add a rule allowing traffic on port 8080 from your IP address. Your can find your IP address using http://www.whatismyip.com/

To Stop the Tomcat Server type
./shutdown.sh 
 

Step 10: Create a virtual host on Tomcat

Well at this point my Tomcat is running so I can change my Apache Web server config to route all traffic to my Tomcat Server. But I intend to use my Tomcat for multiple sites in future so I decided to setup a virtual Host on the Tomcat.
Its very easy to setup virtual host on Tomcat, just edit the server.xml file located in /usr/local/$CATALINA_HOME/conf folder and add the following lines after the </Host> tag for the localhost

        <Host name="try.mysamplecode.com" appBase="/var/www/vhosts/try.mysamplecode.com/webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
            <Valve className="org.apache.catalina.valves.AccessLogValve"
                 directory="logs"  prefix="try_log." suffix=".txt"
                 pattern="common"/>
        </Host>


My server.xml file location was /usr/local/apache-tomcat-6.0.35/conf

Make sure that you change the try.mysamplecode.com with your own domain name.
Also create a folder named webapps in your virtual host directory /var/www/vhosts/DOMAIN_NAME

In my case the I create the webapps in /var/www/vhosts/try.mysamplecode.com, So going forward for this specific domain you can drop your WAR files in this newly created webapps directory and it will automatically unpack and deploy. Please restart your Tomcat server at this time.

Step 11: Redirect all Tracffic from Apache virtual host to Tomcat Host

Now go to the Apache config for your domain name created in Step# 4. It's located in /var/www/vhosts/DOMAIN_NAME/conf folder. Add the lines shown in bold ...

CustomLog /var/www/vhosts/try.mysamplecode.com/statistics/logs/access_log plesklog
ErrorLog  "/var/www/vhosts/try.mysamplecode.com/statistics/logs/error_log"
......

ProxyPass / http://try.mysamplecode.com:8080/
ProxyPassReverse / http://try.mysamplecode.com:8080/ 


Replace try.mysamplecode.com with your domain name ! 
Restart your Apache server from the Server management using parallel plesk panel.

Step 12: Deploy our ROOT application

Create a Dynamic Web Project in eclipse named ROOT. Add index.jsp to /WebContent folder. Copy and paste this code in your index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Apache Tomcat linux VPS hosting setup</title>
</head>
<body>
<h1>
    WOW, My Apache Tomcat linux VPS hosting setup works !
</h1>
</body>
</html>

Now export the Project as WAR file named ROOT.war and drop it the domain /webapps folder /var/www/vhosts/DOMAIN_NAME/webapps

All DONE !


You can try typing your URL in the browser and you will see something link this ...


Java Tomcat hosting experience with myhosting

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.