If you have Amazon EC2 linux, then here it is complete tutorial for deploying Java Web Application. First connect to your Amazon EC2 Linux Instance. To run Java web application, you need below things to be installed
  1. JDK
  2. Tomcat
  3. Database (Here I am using MySQL) 

By default you will be connected as ec2-user. Now login as administrator to have complete rights

sudo su

JDK 

Amazon EC2 Linux comes with JDK installation. You don't need to install it. Just check its version
java -version

Tomcat 7

Use below commands for Tomcat 7 management
// Install tomcat 7 with webapps docs-webapp admn-webapps
sudo yum install tomcat7-webapps tomcat7-docs-webapp tomcat7-admin-webapps

// Start tomcat7 server
sudo service tomcat7 start

// Stop tomcat7 server
sudo service tomcat7 stop 

// Restart tomcat7 server
sudo service tomcat7 restart  

// To know running status of tomcat7
sudo service tomcat7 status

// To reload 
sudo service tomcat7 reload

// Add Tomacat 7 service to the autostart
sudo chkconfig tomcat7 on

Database (MySQL)

Find below queries for mysql server management
// To install mysql server
sudo yum install mysql-server

// To start mysql server
sudo service mysqld start

// To stop mysql server
sudo service mysqld stop

// To start server on startup
sudo chkconfig mysqld on

// connect to mysql as root
mysql -u root

// to change root password
mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit


// To create database
Create database**********
create schema demos
connect demos
quit

Webapps 

We should deploy WAR file tomcat webapps folder, so now we have to find out it
// find where "webapps" folder located
find / -type d -name 'webapps'

// this is general path for tomcat7 webapps folder
/var/lib/tomcat7/webapps

// change permission for webapps
chmod 777 webapps

// find server.xml
find / -type f -name 'server.xml'

// this is general path for tomcat7 server.xml
/etc/tomcat7/server.xml

// If you want to change server.xml
cd /etc/tomcat7
vi server.xml
//press insert button
//do changes you want
//escape key then :wq

Run Web application on Port 80

Tomcat 7 default port is 8080. HTTP default port is 80, So we have to redirect all traffic from port 80 to 8080. Find below for commands
// It will redirect HTTP request data from port 8080 to 80
sudo /sbin/iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
sudo /sbin/service iptables save

To delete war files or app folders

If you want to remove apps or war files from webapps folder, then use below commands
// for Deleting folders
rm -r mydir
rm -rf mydir

Deploy WAR File

Now we came to final phase of deploying WAR file. Here we have to copy WAR file from local machine to Amazon EC2 Instance. 

Required software

  1. Putty
  2. PuttyGen
  3. FileZilla
While creating amazon EC2, you will get private key file (.pem) to access EC2 instance. Using Putty and this private key file (.pem), you can connect your Amazon EC2 instance. For more information read this article. FileZilla uses .ppk file. So we have to convert .pem file to .ppk file. We can user PuttyGen for converting .pem file to .ppk file

Convert .pem file to .ppk file

  1. Download your .pem from AWS
  2. Open PuTTYgen, select Type of key to generate as: SSH-2 RSA
  3. Click "Load" on the right side about 3/4 down
  4. Set the file type to *.*
  5. Browse to, and Open your .pem file
  6. PuTTY will auto-detect everything it needs, and you just need to click "Save private key" and you can save your ppk key for use with PuTTY

Setup FileZilla

Install FileZilla and Open It. 
Now Click on File > Site Manager 
Add a new site with the following parameters:
Host: Your public dns name of ec2 instance, or the public ip address of the server
Protocol: SFTP
Logon Type: Normal
User: From the docs: "For Amazon Linux, the default user name is ec2-user. For RHEL5, the user name is often root but might be ec2-user. For Ubuntu, the user name is ubuntu. For SUSE Linux, the user name is root. Otherwise, check with your AMI provider."
Now click on "Connect"

Now copy WAR file to webapps folder

Restart tomcat server. Now your web application on the net

17 comments:

  1. Hey how to connect to mysql database in my war...??

    ReplyDelete
    Replies
    1. It is simple. Install MySQL in local PC and EC2 instance with same credentials. Develop Java Web Application with local database. Export it into WAR file. Deploy that WAR file to EC2. Now EC2 MySQL database will be local database to your deployed WAR file.

      Delete
  2. Hi..I have install tomcat server successfully on aws ec2-server.
    But not able to deployed my war file due permission denied on webapps

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. sudo chmod 777 /var
      sudo chmod 777 /var/lib
      sudo chmod 777 /var/lib/tomcat7
      sudo chmod 777 /var/lib/tomcat7/webapps
      run the above commands after verifing your tomcat version

      or you can only change the permissions of webappps only by:
      sudo chmod 777 /var/lib/tomcat7/webapps

      Delete
    3. but if we are changing the permission with 777, it will be open to the world which is security threat for our application. But how to overcome this ?

      Delete
  3. This was very useful, very clear explaination

    ReplyDelete
  4. how to deploy my database .sql file to my AMI i have followed all the above steps

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. when i deploy the application it fails saying:
    FAIL - Application at context path /appname could not be started

    ReplyDelete
  7. I have deployed the war file in tomcat and restarted the tomcat.
    How can we see whether the application is running or not?

    ReplyDelete
  8. I have deployed the war and restarted the tomcat
    Tomcat is working fine but when i access my application i am getting 404 error
    Can you help me in this

    ReplyDelete
  9. This is generally a result of the multi-level distribution systems or multiple entities with individual approaches and goals. visit site

    ReplyDelete

Blogroll

Popular Posts