2015年5月9日星期六

[方法整理] Deploy war file on EC2 instance

Deploy war file on EC2 instance

1. Use the following statement to connect to the EC2 instance created:
ssh -i task13-key-pair.pem ec2-user@ec2-52-24-226-141.us-west-2.compute.amazonaws.com

2. Install Tomcat7 on that EC2 instance:(reference: http://in4ray.blogspot.com/2012/04/install-tomcat-7-on-amazon-linux.html)
(1) Initiate SSH session (as 'ec2-user') and connect to the Amazon Linux instance by it public DNS name.
(2) Install Tomcat 7 together with standard Tomcat samples, documentation, and management web apps:
sudo yum install tomcat7-webapps tomcat7-docs-webapp tomcat7-admin-webapps
(3) Start/stop/restart Tomcat 7 as a service.
start:
sudo service tomcat7 start
stop:
sudo service tomcat7 stop 
restart:
sudo service tomcat7 restart 
(4) Add Tomacat 7 service to the autostart.
sudo chkconfig tomcat7 on
  

3. Deploy war file
(1) Add 8080 port to the security group:(reference: http://www.27programs.com/?p=251)
(2) Upload war file
scp -i task13-key-pair.pem task13V1.war ec2-user@ec2-52-24-226-141.us-west-2.compute.amazonaws.com:~/
(3) On your instance console, go to the directory /var/lib/tomcat7/webapps and copy the war file
[ec2-user@ip-172-31-42-83 ~]$ cp task13V1.war /var/lib/tomcat7/webapps/task13V1.war
(4) Restart tomcat by issuing:
sudo service tomcat7 restart  
(5) Access the application from browser

[方法整理]Create a simple war file

Create a simple war file

(source: http://oak.cs.ucla.edu/cs144/projects/tomcat/)

Now that we understand the basic structure of a WAR file, we will be creating a very simple WAR file, called simple.war, that just contains a static "hello, world" type HTML page and the basic web.xml file. Here is a step-by-step instruction on how you can create a .war file:

Create a temporary folder to place all files that should go into the .war file. Make it as your current directory by "cd" into the directory.
Create a file named hello.html in your temporary directory with the following content.
<html>
    <head><title>Hello World</title></head>
    <body><h1>Hello World</h1></body>
</html>

Create a WEB-INF folder and create the deployment descriptor file web.xml in it with the following content.
<web-app id="simple" version="2.4">
    <welcome-file-list>
        <welcome-file>hello.html</welcome-file>
    </welcome-file-list>
</web-app>

This descriptor file will make the hello.html page as the "default page" that your application returns.Since you are not including any servlet classes in the WAR file yet, you can skip steps 4 and 5 below and move directly to step 6.
Build your servlet classes. Create a WEB-INF/classes folder and copy your servlet classes into it.
Create a WEB-INF/lib folder and place all JAR library files that your servlet code depends on. After these steps, the layout of your temporary folder may look like the following, for example:
simple_temp
 |
 +- hello.html
 |
 +- WEB-INF
     +- web.xml
     |
     +- lib
     |   +- core-library.jar
     |
     +- classes
         +- edu
             +- ucla
                 +- hello.class

At the root of your temporary folder, use the jar command to create your WAR file. For example:
jar cfM simple.war *

Follow the above instructions to build your "hello, world" page and web.xml as simple.war file and deploy it by placing the war file into the $CATALINA_BASE/webapps directory of your Tomcat server. Once deployed, point your browser to http://localhost:1448/simple. You should see the "Hello World" HTML page you specified as the welcome-file.