Phil explains how to write a scalable SNMP Trap and Inform message receiver in Java using SNMP4J. He also explains what SNMP is and surrounding ideas such as TCP and UDP.
Installing Tomcat 7 on CentOS 5.5 and AWS
First, I’d like to wish you a happy April Fools day! I’ve never really been into the pranks that most people do on this day, and the idea I had fell through due to the lack of time (go figure). The good news is that there is always next year!
For a project I had to do in a Distributed Computing class, I ended up having to configure a Tomcat 7 server running on CentOS 5.5 (a Redhat Linux derivative). I always document my actions when I charter into unknown waters, so I thought I’d post my documentation for the public to see.
Before I begin, this project required the use of Amazon Web Services for deployment. Being previously unfamiliar with AWS, I thought I’d outline some pros and cons to the service compared to traditional VPS hosting.
I like AWS because I can quickly create servers and delete servers on the fly without having to pay up front or rely on a support member who can only work so fast. AWS also has the ability to expand dynamically for the load to a server. For example, if you are hosting a site on AWS, and they detect your site is under a high load that the server can’t handle, more servers will be deployed on the spot to handle the increased traffic through an automatic load balancing mechanism. Also, unlike most VPS providers, AWS provides a physical firewall that is in front of your AWS server, so you don’t necessarily have to worry about configuring a software firewall which might not always work perfectly. Finally, the AWS servers are very fast, I have never seen any kind of a speed issue with them. On other providers, this isn’t always the case, especially if the server is virtualized using OpenVZ (where the provider can oversell the server resources easily).
The biggest disadvantage of using AWS in the long term (for hosting) is the pricing scheme. AWS uses a simple pricing scheme that charges you for every hour your server is running and is active. This adds up to roughly $5-$100/month depending on what you get. Then on top of that, AWS also charges for bandwidth by the GB. Although this pricing scheme is fine for very little traffic or a lot of traffic, for the average amount of traffic (about 1-2TB / month), this pricing scheme isn’t the best compared to other providers. For example, the virtual server that hosts my website costs me $20 per month, it has so much disk space and bandwidth (2TB included). If I were to ask AWS for the same thing, I’d pay $10 for the server, and then roughly $100 for the bandwidth every month (if I were to use it all).
Now that I’ve talked about AWS, it’s time to give some information about how to get Tomcat 7 running on CentOS 5.5. If you’re making Tomcat 7 run on AWS, I started with the EC2 (Elastic Compute) image: ami-caf107a3 – “CentOS-55-Base-32bit-10GB-EBS-pvgrub–v1-0″. I made this server run on a micro instance which is very cheap (in the free usage tier for the first 12 months), the micro instance includes 10GB of disk space and about 512MB of ram. While I was in the AWS control panel, I modified the firewall to allow port 22 for SSH access, and port 80 for web access (since I will be running Tomcat on port 80 which is the standard HTTP port).
I know that Tomcat means Java, and when I think of Java, I think of a virtual machine that has to run the Java byte code (equivalent of a compiled program, but for Java). The virtual machine is why Java takes a lot of memory to power. Of course, 512MB of ram isn’t going to get you far for any Java application and that is why I decided that I should make some swap space for Tomcat to run on top of a JVM (Java Virtual Machine).
Note that everything I do below will require root access to complete. I’m also not sure if SSHing into the machine is possible to follow these steps without an elastic IP, so I had one added to my AWS account and bounded to this server.
If you’re on AWS like I was, there is no swap space pre-defined, therefore, it is wise to create a swap file and make it into swap space. For those of you who don’t know what swap space is, it’s basically virtual ram that is used when your main physical ram (in my case 512MB) is depleted and applications need more memory to run. The steps to making the swap space are as follows:
dd if=/dev/zero of=/swapfile bs=1024 count=524288
edit /etc/fstab and append the following line to it:
/swapfile swap swap defaults 0 0
After the swap space is made, I ran a fairly straight forward update command to ensure that the CentOS installation was up to date and functional.
yum update –y
For Apache Tomcat 7 to function, the Sun Java Development Kit must be installed. OpenJDK which is available through the CentOS repository using yum will not work!
Using the command wget or some alternate means, download the JDK 6 for Java SE (Standard Edition) from Oracle’s website. I downloaded the Linux .rpm.bin file to my home directory. Their main website to access the Java JDK is at: http://www.oracle.com/technetwork/java/javase/downloads/index.html. Assuming you downloaded the JDK 6 Update 24 to a file named jdk6u24.rpm.bin in your home directory as root (~ or /root), change to that directory and give the file executable permissions, then execute the file.
chmod 700 jdk6u24.rpm.bin
Shortly after you execute the file; it will install several packages for Java. Once it is complete, hit
-bash-3.2# java -version
java version “1.6.0_24″
Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Java HotSpot(TM) Client VM (build 19.1-b02, mixed mode, sharing)
With Java installed, tomcat 7 is now the primary focus. Tomcat 7 can be downloaded from the following site: http://tomcat.apache.org/download-70.cgi. At this point, tomcat 7 is up to version 7.0.11 and I downloaded the core release of it. Here is the process I used to install Tomcat, although the actual mirror site for Tomcat may not work in the future.
get tomcat 7 core from: http://tomcat.apache.org/download-70.cgi
tar xzf apache-tomcat-7.0.11.tar.gz
mv apache-tomcat-7.0.11 /usr/share
Tomcat is now installed, but it is not yet a service. To accomplish this, we need to create a new file tomcat at the following location “/etc/init.d/tomcat”. An example command that will modify this file using vi is: “vi /etc/init.d/tomcat”. In this file, the following needs to be pasted:
#!/bin/bash # description: Tomcat Start Stop Restart # processname: tomcat # chkconfig: 234 20 80 JAVA_HOME=/usr/java/jdk1.6.0_24 export JAVA_HOME PATH=$JAVA_HOME/bin:$PATH export PATH CATALINA_HOME=/usr/share/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
In addition, this file needs to be set to the permission value of 755. This can be accomplished using:
chmod 755 /etc/init.d/tomcat
We now need to modify Tomcat to run on port 80. For my installation, I replaced the contents of the file: /usr/share/apache-tomcat-7.0.11/conf/server.xml with the following contents:
<?xml version='1.0' encoding='utf-8'?> <Server port="8005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <Listener className="org.apache.catalina.core.JasperListener" /> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <Service name="Catalina"> <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" resolveHosts="false"/> </Host> </Engine> </Service> </Server>
I saved the file and then executed the following commands:
service tomcat start
service tomcat stop
In the file catalina.out, I checked for any errors that might be present. For my installation, the file had no errors in it. Assuming that there are no errors, the following commands will start tomcat as a service and then make tomcat automatically start on a system startup or reboot.
service tomcat start
chkconfig tomcat on
To manage tomcat via the web interface, navigate to your server’s IP address in a browser using http://YOUR_SERVER_IP. I also found it helpful to know that if I name a certain directory “ROOT” or drop in a war file named root.war, this would become the default page for Tomcat when the base url is loaded “http://YOUR_SERVER_IP/”.
That is all I have. As always, thanks for reading!