This is a beginner’s guide to setting up an AWS EC2 instance with Ubuntu/Tomcat7/MySQL.
- 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.
- 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.
- 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.
- 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
- Installations: After sshing into the instance, update Ubuntu package manager and installed packages
sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install mysql-server
sudo apt-get install tomcat7 tomcat7-webapps
- 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"
Then you need to follow a few more steps mentioned here.
Sometimes it makes more sense to serve static content like images to your website from a folder on the disk on your server rather than packaging it into a war file. To serve static content from tomcat7, you don’t need to write any java code or create a war file. You can simply edit the $TOMCAT_HOME/conf/server.xml and add <Context path=”/images” docBase=”/home/ubuntu/images” /> tag to the <Host> tag present in the file. path refers to the url path relative to the host/context and docBase refers to the location on disk. So if you have a url like /images/1.jpg, having the file /home/ubuntu/images/1.jpg will render the image.
If you have a war file and are creating a custom servlet, then you will also need to map the url pattern /images/* to the default servlet in web.xml file.
Additionally, the folder on the disk should have the right permissions to allow read access by tomcat.
What is Cron?
Cron is a service that runs periodic tasks known as cron jobs. Cron has various implementations which handle the execution of cron jobs. Cron jobs are stored in crontab file and can be edited using the crontab command.
//list all cron jobs
static-202:~ Sayeed$ crontab -l
//edit cron jobs. crontab file opens in the default text editor for editing.
static-202:~ Sayeed$ crontab -e
//set vim as the editor
static-202:~ Sayeed$ export visual=vi
//delete the crontab file of the current user. this will delete all the cron jobs.
static-202:~ Sayeed$ crontab -r
To delete a particular cron job, open the crontab file using crontab -e command -> delete the cron job -> save the file.
minute(0-59) hour(0-23) day-of-month(1-31) month(1-12) day-of-week(0-6, 0 being sunday) <command>
I wanted to add a cron job on my linux server to take the dump of MYSQL database at 00:00 hrs everyday. Here is how it looks:
0 0 * * * mysqldump -uxyz -pxyz et_prod > /home/ubuntu/et_prod.sql
//take database dump at 10a.m. every sunday.
* 10 * * 0 mysqldump -uxyz -pxyz et_prod > /home/ubuntu/et_prod.sql
//take database dump every 2 hours.
* */2 * * * mysqldump -uxyz -pxyz et_prod > /home/ubuntu/et_prod.sql