Apache Tomcat服务部署网站的项目实践
作者:微软技术分享
Tomcat是Apache软件基金会的Jakarta项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成.由于有了Sun的参与和支持,最新的Servlet和JSP规范总是能在Tomcat中得到体现,因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web应用服务器.
Tomcat环境搭建
1.由于Tomcat使用Java开发,所以应该下载并安装JDK.
[root@localhost ~]# wget http://www.oracle.com/technetwork/java/javase/downloads/index.html [root@localhost ~]# tar -xzvf jdk-*.tar.gz -C /usr/local/ [root@localhost ~]# mv /usr/local/jdk-*/ /usr/local/jdk
2.修改环境配置文件写入以下内容,保证下次开机配置好Java环境.
[root@localhost ~]# vim /etc/profile #------------------------------------------------------------------ #Java-JDK-Path 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 ~]# source /etc/profile
3.下载并解压安装Tomcat.
[root@localhost ~]# wget http://tomcat.apache.org/tomcat9.0.7.tar.gz [root@localhost ~]# tar -xzvf apache-tomcat-9.0.7.tar.gz -C /usr/local/ [root@localhost ~]# mv /usr/local/apache-tomcat-9.0.7/ /usr/local/tomcat
4.修改环境配置文件写入以下内容.
[root@localhost ~]# vim /etc/profile #------------------------------------------------------------------ #Apache-Tomcat-Path export TOMCAT_HOME=/usr/local/tomcat export PATH=$PATH:$TOMCAT_HOME/bin #------------------------------------------------------------------ [root@localhost ~]# source /etc/profile
5.赋予可执行权限,启动Tomcat,并设置成开机自启动.
[root@localhost ~]# chmod -R 755 /usr/local/tomcat/bin/* [root@localhost ~]# /usr/local/tomcat/bin/catalina.sh start [root@localhost ~]# echo "/usr/local/tomcat/bin/catalina.sh start" >> /etc/profile
部署静态页面
1.创建一个默认目录,用来保存起始页面.
[root@localhost ~]# mkdir -p /usr/local/tomcat/webindex [root@localhost ~]# echo "hello world" > /usr/local/tomcat/webindex/index.html
2.修改tomcat主配置文件,具体只需要修改两处位置即可.
[root@localhost ~]# vim /usr/local/tomcat/conf/server.xml Define a non-SSL/TLS HTTP/1.1 Connector on port 8080 --> <Connector port="80" protocol="HTTP/1.1" #将此处端口改为80 connectionTimeout="20000" redirectPort="8443" /> <!-- A "Connector" using the shared thread pool--> <!-- ...... <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> <Context path="" docBase="/usr/local/tomcat/webindex" reloadable="false" /> #添加本行,指定一下配置文件位置 </Host> </Engine>
3.重新启动Tomcat,稍等片刻即可完成部署.
[root@localhost ~]# /usr/local/tomcat/bin/catalina.sh stop [root@localhost ~]# /usr/local/tomcat/bin/catalina.sh start
Java-war包部署
1.将War包拷入到 /usr/local/tomcat/webapps
目录下,并删除 ROOT 缓存目录.
[root@localhost ~]# cp -a *.war /usr/local/tomcat/webapps/ [root@localhost ~]# rm -fr /usr/local/tomcat/webapps/ROOT/*
2.编辑Tomcat主配置文件,在相应的区域中加入以下标★语句.
[root@localhost ~]# vim /usr/local/tomcat/conf/server.xml </Realm> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> ★ <Context path="" docBase="war包名称.war" debug="0" privileged="true"/> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
3.重新启动Tomcat,稍等片刻即可完成部署.
[root@localhost ~]# /usr/local/tomcat/bin/catalina.sh stop [root@localhost ~]# /usr/local/tomcat/bin/catalina.sh start
Tomcat虚拟主机
基于端口虚拟主机
基于端口的虚拟主机,可以让用户通过端口号,来访问服务器上的资源,在配置虚拟网站时,基于端口的配置方式最为复杂,以下实验将实现在一台服务器上配置多个端口,搭建多个网站,每个网站使用一个端口.
1.首先在tomcat目录创建两个目录分别存放两个虚拟主机页面.
[root@localhost ~]# mkdir -p /usr/local/tomcat/virtual [root@localhost ~]# mkdir -p /usr/local/tomcat/virtual/vhost1 [root@localhost ~]# mkdir -p /usr/local/tomcat/virtual/vhost2 [root@localhost ~]# echo "vhost1" > /usr/local/tomcat/virtual/vhost1/index.html [root@localhost ~]# echo "vhost2" > /usr/local/tomcat/virtual/vhost2/index.html
2.修改主配置文件,添加在server.xml,中设置两个service组件.
[root@localhost ~]# vim /usr/local/tomcat/conf/server.xml <Service name="Catalina"> <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" 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="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> <Context path="" docBase="/usr/local/tomcat/virtual/vhost1" reloadable="false" /> </Host> </Engine> </Service> <Service name="Catalina1"> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <Engine name="Catalina1" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> <Context path="" docBase="/usr/local/tomcat/virtual/vhost2" reloadable="false" /> </Host> </Engine> </Service>
3.重新启动Tomcat,稍等片刻即可完成部署.
[root@localhost ~]# /usr/local/tomcat/bin/catalina.sh stop [root@localhost ~]# /usr/local/tomcat/bin/catalina.sh start
基于域名虚拟主机
当服务器无法为每一个网站分配一个独立的IP的时候,可以尝试让Apache自动识别用户请求的域名,从而根据不同的域名请求来传输不同的内容,这里我们为了验证实验要手动搭建一个DNS解析,以下实验将实现在一台服务器上多个域名,搭建多个网站,每个网站使用一个域名.
1.配置域名与Ip的映射管理,这里你可以配置DNS解析,也可以直接写host文件测试.
[root@localhost ~]# vim /etc/hosts 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 ~]# mkdir -p /usr/local/tomcat/virtual [root@localhost ~]# mkdir -p /usr/local/tomcat/virtual/vhost1 [root@localhost ~]# mkdir -p /usr/local/tomcat/virtual/vhost2 [root@localhost ~]# echo "vhost1" > /usr/local/tomcat/virtual/vhost1/index.html [root@localhost ~]# echo "vhost2" > /usr/local/tomcat/virtual/vhost2/index.html
3.修改server.xml主配置文件,修改以下内容即可.
[root@localhost ~]# vim /usr/local/tomcat/conf/server.xml <Service name="Catalina"> <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" 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="www.wang.com" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> <Context path="" docBase="/usr/local/tomcat/virtual/vhost1" reloadable="false" /> </Host> <Host name="www.rui.com" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> <Context path="" docBase="/usr/local/tomcat/virtual/vhost2" reloadable="false" /> </Host> </Engine> </Service>
4.重新启动Tomcat,稍等片刻即可完成部署.
[root@localhost ~]# /usr/local/tomcat/bin/catalina.sh stop [root@localhost ~]# /usr/local/tomcat/bin/catalina.sh start
到此这篇关于Apache Tomcat服务部署网站的项目实践的文章就介绍到这了,更多相关Apache Tomcat部署网站内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!