• 分类

  • 归档

Into the Cloud – Migrating My Apache+JBoss+MySQL Applications to Amazon’s EC2, Part II

(Continued from Part I) I started out with the Ubuntu 8.0.4 AMI, created an instance off it. Then I did three more things using Elasticfox (see manual for instructions):

  1. Got an Elastic IP and associated with my instance. An Elastic IP is a static IP for your EC2 account that can be associated and deassociated with any instance in your account, at any time.
  2. Configured the security group of the instance to only accept connections at port 80 from any machine, and port 22 (for SSH) from my own machine
  3. I created an EBS and attached it to my instance. Be sure to create the EBS in the same availability zone as the instance, otherwise it cannot be attached.

These two steps allowed the instance to be accessible from the internet but securely. The third step created a storage for application data to reside. While the last step wasn’t absolutely necessary, because the applications would run fine with data on the same instance, it would be nice to have data in a separate storage area that was easily backed up and able to connect to multiple instances.

The next steps were to install MySQL, JDK, JBoss, and Apache. I did in that order because the first three worked OOB while Apache required some configuration to work with JBoss.

MySQL

I logged on to my instance using Putty, and followed step 4 and 5 of this blog verbatim without problems.

JDK

Follow step 2 of this blog – no problems. I used a newer update of JDK 6.

JBoss

JBoss download page is here. I downloaded the exact version (*.zip) I was using before, using wget. Copied it to /usr/local and unzip. Of course the server would say unzip wasn’t installed. So install unzip using

sudo apt-get install unzip

I created a soft link /usr/local/jboss to the installed jboss directory. Since it was a good practice to run JBoss not as root, I created a new user jboss, and changed the ownership of the JBoss installation:

sudo groupadd jboss
sudo useradd -g jboss jboss
chown -R jboss:jboss [JBOSS INSTALLATION DIRECTORY]

Now add Java to jboss user’s PATH by adding these lines to /home/jboss/.profile file (when installing JDK I already created the soft link /usr/local/java pointing to the JDK home directory):

#add java home in path
export JAVA_HOME=/usr/local/java
PATH=$JAVA_HOME/bin:$PATH

To ensure JBoss to start on reboot, I installed found this simple script off the net and put it in file /etc/init.d/jboss

#! /bin/sh

start(){
 echo "Starting jboss.."
 su -l jboss -c '/usr/local/jboss/bin/run.sh > /dev/null 2> /dev/null &'
}

stop(){
 echo "Stopping jboss.."
 su -l jboss -c '/usr/local/jboss/bin/shutdown.sh -S &'
}

restart(){
 stop
# give stuff some time to stop before we restart
 sleep 60
# protect against any services that can't stop before we restart (warning this k
ills all Java instances running as 'jboss' user)
 su -l jboss -c 'killall java'
# if the 'su -l ...' command fails try:
 #   sudo -u jboss killall java
 start
}

case "$1" in
 start)
 start
 ;;
 stop)
 stop
 ;;
 restart)
 restart
 ;;
 *)
 echo "Usage: jboss {start|stop|restart}"
 exit 1
esac

exit 0

and installed the startup script:

update-rc.d jboss defaults

Now it was time to startup JBoss:

/etc/init.d/jboss start

Voila! The JBoss was up and running. I could verify that by

wget http://localhost:8080/

It downloaded the default JBoss index.html page into the current directory. Of course I could also verify it using a browser if I opened up port 8080 in the security group.

Apache

Installing Apache was easy

sudo apt-get install apache

I was previously using Apache 2.0.x and mod_jk to connect to JBoss. The new Apache installation was 2.2.x and came with mod_proxy_ajp, so it took me fair amount of time to find all the details on the web. But once it wasn’t that bad, it wasn’t that bad at all.

I could first verify Apache was working by pointing my browser to the IP address of my instance. “It Works!” That is reassuing.

To integrate with JBoss I enabled mod_proxy_ajp

sudo a2enmod proxy_ajp

Then I created a new configuration file for my instance:

sudo vi /etc/apache2/sites-available/mysite

In this file I added these lines:

ProxyPass / ajp://localhost:8009/
ProxyPassReverse / ajp://localhost:8009/

After restarting Apache I found a 403 error in my browser. It turned out the configuration for mod_proxy_ajp, /etc/apache2/mods-enabled/proxy.conf, the default is Deny From All

<Proxy *>
 AddDefaultCharset off
 Order deny,allow
 Deny from all
</Proxy>

It needed to be changed to allow connections to passed through. I simply changed it to “Allow from all” because I was using EC2’s security group to control the overall accessibility from the outside world.

After this change and restarting Apache, the browswer showed JBoss’s default web page. It worked!

One Last Problem

Now I got the basic configuration to work, I still faced one more problem. The Apache+JBoss configuration only worked if I restarted Apache after every JBoss restart. If I restarted JBoss without an Apache restart, I got “Service Temporarily Unavailable” error. That was not how it used to work on my hosted server.

Into the Cloud – Migrating My Apache+JBoss+MySQL Applications to Amazon’s EC2, Part I

Hoodbook has a few applications running on a dedicated server that is hosted for a couple of hundred dollars a month.  The server has nothing fancy, just a plain Apache+JBoss+MySQL configuration, with RAID and an offsite backup facility.  Applications were written using JBoss Seam + JSF applications. I have thought about migrating the apps to Amazon EC2 for a while. Now I finally got time to do it.

The main drives for the migration are two-folds: (1) Lower the cost. These applications do not have tons of traffic. The server is three years old, and severely under-utilized. We are paying way over what we get. (2) Step into the future. The future is cloud computing. It is scalable, on-demand, with low overhead. You probably cannot find a more scalable, more reliable hosting solution than Amazon’s EC2. If it is good enough for Twitter, it is good enough for any generic web application.

I have looked into EC2 on and off before but now I believe the time can’t be more ripe for a migration, because of large numbers of how-to documentation online and the availability of Amazon EBS (Elastic Block Storage). Amazon EBS has lower latency than Amazon S3, and is a natural storage extension of EC2 (for a tutorial on their differences, check out this) – best suited for MySQL data. EBS does not have reduncy, so some kind of reduncy has to be built by users. But a quick and dirty solution is to have snapshot of EBS stored to S3.

First, I followed the pre-requisite steps in this blog. I signed up for Amazon EC2 web service, and installed Firefox EC2 plugin Elasticfox. Elasticfox makes managing EC2 instances super easy. I configured Elasticfox with my EC2 account credentials, following Tutorial #1 in Elasticfox’s manual. The only hiccup I had is at Step 4 – Connecting to the Instance. I had trouble connecting to my EC2 instance from putty using the keypair I generated. Whenever I tried to connect, the instance prompted me for root password. It turned out I needed to run Puttygen to convert the generated keypair file (*.pem) into a Putty-recognized key file (*.ppk). Then everything was cool.

In the process I needed to select an Amazon Machine Image (AMI) to launch my EC2 instance. Natually I searched for Apache+JBoss+MySQL images, and I found one: ami-4ea34727. Upon installing it, however, I found it was built on an older version if Ubuntu, 7.10 Gutsy, which was no longer supported. Installing Postfix on the instance, for example, generated errors of unfound urls. That makes the image not usable for my applications. I then investigated upgrading 7.10 to 8.0.4 Hardy which will be supported until 2013, but upgrading from Gutsy to Hardy seemed to have its own quirks.

Finally, I decided to start with an AMI of Ubuntu 8.0.4 (for all Ubuntu images, look here), and build everything from there. It should be an interesting learning experience.

Listen to the Same Speech Twice in A Day

I don’t believe I have ever done this before: I went to the same speech twice in a day. Dr. Kaifu Lee (李开复), the former heaDr. Kaifu Lee (李开复)d of Google China, visited the Silicon Valley last Friday. He gave the keynote at the A(sian)A(merican)M(ulti-Tech)A(ssociation) Annual Conference in the morning, and then gave the same speech (albeit in a less formal setting and more Q&A) at Stanford in the afternoon. Eric and I were at the AAMA conference at Computer History Museum in Mountain View. Later that day, we felt bored with the meeting. So we decided to drive to Stanford and listen to Kaifu Lee’s talk again. Both speeches were excellent, insightful and well worth the time we spent.

As the former head of Google China, Kaifu knows something about the internet in China that most of the people in the world don’t. I was lucky to hear him sharing his insights. The four area he identified, as the next big (internet) things in China:

  • E-Commerce. 50x potential if China were to reach the same E-Commerce penetration level as U.S. now (in terms of percentage of internet user and per user spending). As E-Commerce takes off, so will online advertising (which is only 1/10 of U.S. currently)
  • Mobile internet. 600M mobile users, and 150M mobile internet users. The potential is huge given that smart phones are getting smarter, more wide-spread and many Chinese mobile internet users don’t even own a computer (think of those migrant workers)
  • Cloud computing, Software as a Service. Because of the piracy, there is no traditional software industry in China. But Software as a Service in the Cloud will definitely be the way to go. An early proof is China’s online gaming market, which is at the forefront of the world in terms of business success. Online gaming will continue to grow, but so will other services in the cloud.
  • P2P is leading the world in technical maturity because of the absence of legal issues

These are the four main prospects Dr. Lee highlighted, and his new incubator company Innovation Works will focus on. I have to wholehearted agree. Mobile internet and online advertising, are something Hoodbook has been trying to do in China for the last year. Many proven business models in U.S. can be readily transported to China, with more focus on mobile experience and localized UI.

Euwyn Poon has some details of the talk at his blog http://euwyn.com

PKUAANC EnE (Entrepreneur and Executive) Club Mixer

Just got back from the first PKUAANC EnE mixer. It was a BBQ event near a park, surrounded by a beautiful and upscale apartment complex in San Jose. I should have taken a camera. I got to meet two dozen people, mostly Peking University Alum. A few of them share similar interests with me on mobile phone apps. A good meet, and definitely worth taking the EOS for a topless ride.

Two Year Anniversary of Hoodbook

Hoodbook, Grails, JBoss SeamHoodbook has been in business for two years. Like a two-year old baby, we are still exploring, and growing, too. We have experienced with some of the newer technology platforms, Seams, Grails, and most recently the IPhone application platform. Eric and I, the two founders of the company, each made a few trips back to China to explore business opportunities. This blog is intended to share some of our experiences, mostly on technology but also other random events.