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:
  1. From your Amazon console, click elastic IPs and Allocate a new address.
  2. Associate it to your EC2 instance (if you don't associate with an instance, you'll be charged).
  3. 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:


  4. You should be able to head over to your registered address (e.g., foo.com) on port 8080 now and see the tomcat manager.
  5. 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.
  6. 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
  7. 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
  8. Also, you might want to modify port 8080 to only accept requests from your instance for added security:
    • on your Amazon console, click Security Groups
    • check the box of your security group, click "Inbound", create a custom TCP rule for port 8080 (port range would be 8080) and under "Source", add your elastic IP e.g., some.ip.foo.bar/32
    • 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
  9. 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):
    • ProxyPass         /  http://foo.com:8080/myapp
      ProxyPassReverse  /  http://foo.com:8080/myapp
  10. Also modify the Tomcat server.xml file:
    • vim /etc/tomcat7/server.xml
    • Make sure that you comment out existing lines which deal with port 8080 and instead add the following line:
    • <Connector port="8080" proxyName="www.foo.com" proxyPort="80"/>
  11. Restart your Tomcat and Apache, and you should be all set. Just type foo.com and you should be able to view your website.

No comments:

Post a Comment