A beginners journey - The Search for a good hosting planThis 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 destinationAgain 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 planAs 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
Step 3: Hosting control panelAfter 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.
- 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.
Step 4: Login to Parallel Plesk PanelNext 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
Step 5: Signon to Terminal using SSHYou 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.
Step 6: Find Java install directory and versionMost 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
if java is installed should give you something like this
java version "1.6.0_22"
OpenJDK Runtime Environment (IcedTea6 1.10.6) (rhel-188.8.131.52.6.el5_8-x86_64)
OpenJDK 64-Bit Server VM (build 20.0-b11, mixed mode)
provides you with the Java home path, please note this down, my result was
Step 7: Install Tomcat 6The default provided by the VPS was Tomcat 5.5, here is the link for Tomcat download
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
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 variableTomcat needs JAVA_HOME to start.
Edit file /usr/profile and add the JAVA_HOME after HISTSIZE as shown below
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 outEnter the following commands on your Terminal
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
To Stop the Tomcat Server type
Step 10: Create a virtual host on TomcatWell 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"
directory="logs" prefix="try_log." suffix=".txt"
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 HostNow 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
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 applicationCreate 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 ...