Monday, 17 September 2012

Amazon EC2 Basics (Part 2)

The next step would be to associate an elastic IP with your EC2 instance and setup an Apache server:

From your Amazon console, click elastic IPs and Allocate a new address.

Associate it to your EC2 instance (if you don't associate with an instance, you'll be charged).

Wherever you registered your domain name (Godaddy, Namecheap, etc), you're going to have to change the Address Record (A record) to point to your IP. e.g., for Namecheap, click "Manage domains", the "All host records", and enter your EC2 IP address for both the fields as shown below:

You should be able to head over to your registered address (e.g., foo.com) on port 8080 now and see the tomcat manager.

You might want to install Apache 2 (if you want, otherwise, you can just let Tomcat work as a web server instead of a servlet container; using Apache 2 as the server with Tomcat as the container would allow you to fiddle around with modules). In this example, we're going to be using mod_proxy to proxy requests to Tomcat. Apache listens on port 80 and Tomcat on port 8080.

Installing and starting apache is simple:

ssh into your instance

type sudo yum -y install httpd

type sudo /sbin/chkconfig httpd on

type sudo /sbin/service httpd start

/var/www/html/ is the root web directory; httpd.conf is located at /etc/httpd/conf; mod_proxy is located at /etc/httpd/modules

note that after changing httpd.conf, you should restart apache using sudo /sbin/service httpd restart

Before doing anything else, you need to modify your Amazon security group to open up port 80:

on your Amazon console, click Security Groups

check the box of your security group, click "Inbound", create a custom TCP rule for port 80 (port range would be 80) and add this rule

Also, you might want to modify port 8080 to only accept requests from your instance for added security:

if you were following Part 1 of this series, you should remove the old rule for port 8080, where the "Source" was 0.0.0.0/0

Next, modify your httpd.conf file for any context path that you wish to forward to Tomcat. In this example, I'm forwarding / to http://foo.com:8080/myapp. Note that by default, mod_proxy is already configured properly in this file (otherwise, follow this link, modifying as appropriate):