D20-JSP之部署tomcat
第一节 部署tomcat
JSP(Java Server Pages)是由Sun Microsystems公司倡导、许多公司参与一起建立的一种动态网页技术标准。JSP技术有点类似ASP技术,它是在传统的网页HTML文件(*.htm,*.html)中插入Java程序段(Scriptlet)和JSP标记(tag),从而形成JSP文件(*.jsp)。 用JSP开发的Web应用是跨平台的,既能在Linux下运行,也能在其他操作系统上运行。
Servlet 是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面。 它担当客户请求(Web浏览器或其他HTTP客户程序)与服务器响应(HTTP服务器上的数据库或应用程序)的中间层。 Servlet是位于Web 服务器内部的服务器端的Java应用程序,与传统的从命令行启动的Java应用程序不同,Servlet由Web服务器进行加载,该Web服务器必须包含支持Servlet的Java虚拟机。servlet的工作模
客户端发送请求至服务器
服务器启动并调用Servlet,Servlet根据客户端请求生成响应内容并将其传给服务器
服务器将响应返回客户端
Tomcat 是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。由于有了Sun 的参与和支持,最新的Servlet 和JSP 规范总是能在Tomcat 中得到体现,Tomcat 5 支持最新的Servlet 2.4 和JSP 2.0 规范。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。目前最新版本是7.0。Tomcat 是一个轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。
1 准备java 环境
1.1 下载合适的jdk
//64位Linux 系统使用的 RPM包 ,http://www.oracle.com/technetwork/java/javase/downloads/index.html
1.2 安装jdk
- rpm -ivh jdk-7-linux-x64.rpm
1.3 配置java环境变量
- //在profile文件中加入如下信息:
- vim /etc/profile
#add for tomcat
export JAVA_HOME=/usr/java/jdk1.7.0
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/
export PATH=$PATH:$JAVA_HOME/bin
source /etc/profile
1.4 验证java
- java -version
java version "1.7.0"
Java(TM) SE Runtime Environment (build 1.7.0-b147)
Java HotSpot(TM) 64-Bit Server VM (build 21.0-b17, mixed mode)
- ls /usr/java/
default jdk1.7.0 latest
2 tomcat
2.1 下载
- wget -c http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.29/bin/apache-tomcat-7.0.29.tar.gz
- wget -c http://www.apache.org/dist/tomcat/tomcat-7/v7.0.29/bin/apache-tomcat-7.0.29.tar.gz.md5
2.1.2 md5效验
- md5sum -c apache-tomcat-7.0.29.tar.gz.md5
apache-tomcat-7.0.29.tar.gz: OK
2.2 安装(二进制包)
- tar zxf apache-tomcat-7.0.29.tar.gz
- mv apache-tomcat-7.0.29 /usr/local/tomcat
2.3 tomcat 虚拟主机
//共三个虚拟机
localhost
java
center
2.3.1 建立虚拟机目录
- mkdir -p /var/www/html/{localhost,java,center}
- mkdir -p /var/log/httpd/{localhost,java,center}
- chown -R www.www /var/www/html
2.3.2 准备jsp 测试文件
- cat > index.jsp << EOF
- <%@page language="java" %>
- <html>
- <head>
- <title>Tomcat</title>
- </head>
- <body>
- <h2><font color="red">Tomcat</font></h2>
- <% out.println("Hello,World!"); %>
- </body>
- </html>
- EOF
- cp index.jsp /var/www/html/center
- cp index.jsp /var/www/html/java
- echo "ok" > /var/www/html/localhost/index.html
//index.html 是为haproxy check 准备,可参考下面haproxy 代理 tomcat
注意:默认的localhost 在 /usr/local/tomcat/webapps 目录
2.4 tomcat 完整配置文件
- sed -re '/<!--/,/-->/d' -e '/^$/d' /usr/local/tomcat/conf/server.xml
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.core.JasperListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="/var/www/html/localhost"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/var/www/html/localhost" reloadable="true" crossContext="true"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/var/log/httpd/localhost"
prefix="localhost_access_log." suffix=".txt" resolveHosts="false"
pattern='%h %l %T %t "%r" %s %b "%{Referer}i" "%{User-Agent}i" %{X-Forwarded-For}i' />
</Host>
<Host name="center.test.com" appBase="/var/www/html/center"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/var/www/html/center" reloadable="true" crossContext="true"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/var/log/httpd/center"
prefix="center_access_log." suffix=".txt" resolveHosts="false"
pattern='%h %l %T %t "%r" %s %b "%{Referer}i" "%{User-Agent}i" %{X-Forwarded-For}i' />
</Host>
<Host name="java.test.com" appBase="/var/www/html/java"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/var/www/html/java" reloadable="true" crossContext="true"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/var/log/httpd/java"
prefix="java_access_log." suffix=".txt" resolveHosts="false"
pattern='%h %l %T %t "%r" %s %b "%{Referer}i" "%{User-Agent}i" %{X-Forwarded-For}i' />
</Host>
</Engine>
</Service>
</Server>
3 管理tomcat
3.1 测试server.xml 文件
- /usr/local/tomcat/bin/catalina.sh configtest
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/jdk1.7.0
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Aug 30, 2012 10:11:15 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib#
Aug 30, 2012 10:11:16 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Aug 30, 2012 10:11:16 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Aug 30, 2012 10:11:16 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 981 ms
3.2 启动tomcat
- /usr/local/tomcat/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/jdk1.7.0
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
3.3 tomcat 监听8080端口
- lsof -i :8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 21977 root 40u IPv6 473403 0t0 TCP *:webcache (LISTEN)
4 haproxy 代理 tomcat
拓扑:
Haproxy -> Tomcat
4.1 haproxy 代码
- acl jsp hdr_reg(host) -i ^(center.test.com|java|jsp)
- use_backend tomcat.test.com if jsp
- backend tomcat.test.com
- mode http
- option forwardfor
- balance roundrobin
- cookie SERVERID
- option httpchk HEAD /index.html
- server tomcat01 10.0.100.77:8080 cookie tomcat01 check inter 2000 rise 3 fall 3 weight 3
参考
tomcat
http://baike.baidu.com/view/10166.htm
servlet
baike.baidu.com/view/25169.htm
话说tomcat的前世今生
http://haicang.blog.51cto.com/2590303/963648
tomcat server.xml 中文版介绍
http://jvortex.blog.163.com/blog/static/16961890020111253509772/
tomcat部署web服务(一)
http://wjw7702.blog.51cto.com/5210820/960577
apache tomcat整合后虚拟主机配置
http://lsscto.blog.51cto.com/779396/899306
tomcat 日志配置(Tomcat访问日志浅析)
http://blog.sina.com.cn/s/blog_7acf99ef01013rjt.html
用Haproxy做前端代理时让Apache日志记录客户端IP的修改方法
http://www.ha97.com/4825.html
结束
更多请:
linux 相关 37275208
vmware 虚拟化相关 166682360
本文转自 dongnan 51CTO博客,原文链接:http://blog.51cto.com/dngood/1058668