Tag Archives: AWS

Running an AWS EC2 instance with Ubuntu/Tomcat/MySQL

This is a beginner’s guide to setting up an AWS EC2 instance with Ubuntu/Tomcat7/MySQL.

  1. Create AWS account: Creant an AWS account with your personal and credit card details and login to AWS console. If you are an existing Amazon cusotmer, you can login with that account. AWS provides a free tier which is worth exploring. I have deployed two websites on free tier instances that are functioning smoothly.

    Warning: It is said on forums that free tier instances can mysteriously disappear. So, a periodic back-up of your data and a guide on your set-up process should be maintained.

  2. Configure Security Group: Next you need to set up a security group to which the instance will belong. A security group is where you enable the ports you want your instance to listen on. A default security group already exists which you can modify. Click on Network & Security options in the left pane and edit the default security group to add http, https and ssh ports to the Inbound tab. Without this, your instance won’t respond to any incoming http, https and ssh requests. Other data transfer protocols need to be added here if required.

  3. Create Instance: To launch an AWS instance, select Instances from the left pane and click on launch instance. Give your instance a descriptive name. Create a new public/private key pair and download the key which is a .pem file needed to connect to your server. Store this file in a secure location. Select the configuration for your server and make sure you select the default security group you just edited in the previous step. Security group needs to be configured before launching the instance because you won’t be able to change it afterwards. Once your instance launches, you can see its details by clicking on Instances option from the left pane in the console.

    Warning: Do not restart an instance from the AWS console after it has been launched. Doing so changes the public IP of the instance and you will need to update the DNS record with your domain registrar for your site to function. To overcome this, you can create an elastic IP and assign it to your instance. You will incur charges if you create an elastic IP and leave it unassigned.

  4. SSH Into The Instance: Launch Command Prompt and cd to the directory where your .pem file is stored.
    ssh -i keyname.pem ubuntu@publicIP

    You can get the public IP of your instance from AWS Console > Instances. You may need to modify permissions of the .pem file if required

    chmod 600 keyname.pem

  5. Installations: After sshing into the instance, update Ubuntu package manager and installed packages
    sudo apt-get update
    sudo apt-get dist-upgrade
    

    install MySQL

    sudo apt-get install mysql-server

    install tomcat

    sudo apt-get install tomcat7 tomcat7-webapps
  6. Running Tomcat on Port 80: By default tomcat listens for http requests on port 8080 but by default browser http requests are sent to port 80. In order to fulfill them, you need to configure tomcat to run on port 80. First you need to modify the HTTP connector in conf/server.xml
    <Connector port="80" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

    Then you need to follow a few more steps mentioned here.