Tomcat是Apache软件基金会的Jakarta项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成.由于有了Sun的参与和支持,最新的Servlet和JSP规范总是能在Tomcat中得到体现,因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web应用服务器.
Tomcat环境搭建
1.由于Tomcat使用Java开发,所以应该下载并安装JDK.
[root@localhost ~] [root@localhost ~] [root@localhost ~]
|
2.修改环境配置文件写入以下内容,保证下次开机配置好Java环境.
[root@localhost ~]
export JAVA_HOME=/usr/local/jdk export JAVA_BIN=/usr/local/jdk/bin export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export JAVA_HOME JAVA_BIN PATH CLASSPATH
[root@localhost ~]
|
3.下载并解压安装Tomcat.
[root@localhost ~] [root@localhost ~] [root@localhost ~]
|
4.修改环境配置文件写入以下内容.
[root@localhost ~]
export TOMCAT_HOME=/usr/local/tomcat export PATH=$PATH:$TOMCAT_HOME/bin
[root@localhost ~]
|
5.赋予可执行权限,启动Tomcat,并设置成开机自启动.
[root@localhost ~] [root@localhost ~] [root@localhost ~]
|
部署静态页面
1.创建一个默认目录,用来保存起始页面.
[root@localhost ~] [root@localhost ~]
|
2.修改tomcat主配置文件,具体只需要修改两处位置即可.
[root@localhost ~]
67 Define a non-SSL/TLS HTTP/1.1 Connector on port 8080 68 --> 69 <Connector port="80" protocol="HTTP/1.1" 70 connectionTimeout="20000" 71 redirectPort="8443" /> 72 <!-- A "Connector" using the shared thread pool--> 73 <!-- ...... 157 <!-- Access log processes all example. 158 Documentation at: /docs/config/valve.html 159 Note: The pattern used is equivalent to using pattern="common" --> 160 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" 161 prefix="localhost_access_log" suffix=".txt" 162 pattern="%h %l %u %t "%r" %s %b" /> 163 <Context path="" docBase="/usr/local/tomcat/webindex" reloadable="false" /> 164 165 </Host> 166 </Engine>
|
3.重新启动Tomcat,稍等片刻即可完成部署.
[root@localhost ~] [root@localhost ~]
|
Java-war包部署
1.将War包拷入到 /usr/local/tomcat/webapps
目录下,并删除 ROOT 缓存目录.
[root@localhost ~] [root@localhost ~]
|
2.编辑Tomcat主配置文件,在相应的区域中加入以下标★语句.
[root@localhost ~]# vim /usr/local/tomcat/conf/server.xml
146 </Realm> 147 148 <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> ★ <Context path="" docBase="war包名称.war" debug="0" privileged="true"/> 150 151 <!-- SingleSignOn valve, share authentication between web applications 152 Documentation at: /docs/config/valve.html --> 153 <!-- 154 <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
|
3.重新启动Tomcat,稍等片刻即可完成部署.
[root@localhost ~] [root@localhost ~]
|
Tomcat虚拟主机
基于端口虚拟主机
基于端口的虚拟主机,可以让用户通过端口号,来访问服务器上的资源,在配置虚拟网站时,基于端口的配置方式最为复杂,以下实验将实现在一台服务器上配置多个端口,搭建多个网站,每个网站使用一个端口.
1.首先在tomcat目录创建两个目录分别存放两个虚拟主机页面.
[root@localhost ~] [root@localhost ~] [root@localhost ~]
[root@localhost ~] [root@localhost ~]
|
2.修改主配置文件,添加在server.xml,中设置两个service组件.
[root@localhost ~]
53 <Service name="Catalina"> 54 <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 55 <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> 56 57 <Engine name="Catalina" defaultHost="localhost"> 58 <Realm className="org.apache.catalina.realm.LockOutRealm"> 59 <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> 60 </Realm> 61 62 <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> 63 64 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> 65 <Context path="" docBase="/usr/local/tomcat/virtual/vhost1" reloadable="false" /> 66 </Host> 67 </Engine> 68 </Service> 69 70 <Service name="Catalina1"> 71 <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 72 <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> 73 74 <Engine name="Catalina1" defaultHost="localhost"> 75 <Realm className="org.apache.catalina.realm.LockOutRealm"> 76 <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> 77 </Realm> 78 79 <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> 80 81 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> 82 <Context path="" docBase="/usr/local/tomcat/virtual/vhost2" reloadable="false" /> 83 </Host> 84 </Engine> 85 </Service>
|
3.重新启动Tomcat,稍等片刻即可完成部署.
[root@localhost ~] [root@localhost ~]
|
基于域名虚拟主机
当服务器无法为每一个网站分配一个独立的IP的时候,可以尝试让Apache自动识别用户请求的域名,从而根据不同的域名请求来传输不同的内容,这里我们为了验证实验要手动搭建一个DNS解析,以下实验将实现在一台服务器上多个域名,搭建多个网站,每个网站使用一个域名.
1.配置域名与Ip的映射管理,这里你可以配置DNS解析,也可以直接写host文件测试.
[root@localhost ~] 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 www.wang.com www 127.0.0.1 www.rui.com www
|
2.在tomcat目录创建两个目录分别存放两个虚拟主机页面.
[root@localhost ~] [root@localhost ~] [root@localhost ~]
[root@localhost ~] [root@localhost ~]
|
3.修改server.xml主配置文件,修改以下内容即可.
[root@localhost ~]
53 <Service name="Catalina"> 54 <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 55 <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> 56 57 <Engine name="Catalina" defaultHost="localhost"> 58 <Realm className="org.apache.catalina.realm.LockOutRealm"> 59 <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> 60 </Realm> 61 62 <Host name="www.wang.com" appBase="webapps" unpackWARs="true" autoDeploy="true"> 63 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> 64 <Context path="" docBase="/usr/local/tomcat/virtual/vhost1" reloadable="false" /> 65 </Host> 66 <Host name="www.rui.com" appBase="webapps" unpackWARs="true" autoDeploy="true"> 67 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> 68 <Context path="" docBase="/usr/local/tomcat/virtual/vhost2" reloadable="false" /> 69 </Host> 70 71 </Engine> 72 </Service>
|
4.重新启动Tomcat,稍等片刻即可完成部署.
[root@localhost ~] [root@localhost ~]
|