• Apache Tomcat服务部署网站


    Tomcat是Apache软件基金会 (Apache Software Foundation)的Jakarta项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成.由于有了Sun的参与和支持,最新的Servlet和JSP规范总是能在Tomcat中得到体现,因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web应用服务器.


    ♥ 文章声明 ♥
    该系列文章部分文字描述,整理于以下文献,化繁为简.
    《鸟哥的Linux私房菜 (基础学习篇 第三版)》 - 作者:鸟哥
    《Linux就该这么学》 - 作者:刘遄
    《linux运维之道》- 作者:丁明一


    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
    
    67          Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    68     -->
    69     <Connector port="80" protocol="HTTP/1.1"          #将此处端口改为80
    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 &quot;%r&quot; %s %b" />
    163           <Context path="" docBase="/usr/local/tomcat/webindex" reloadable="false" />   #添加本行,指定一下配置文件位置
    164
    165       </Host>
    166     </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
    
    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 ~]# /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
    
     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 &quot;%r&quot; %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 &quot;%r&quot; %s %b" />
     82           <Context path="" docBase="/usr/local/tomcat/virtual/vhost2" reloadable="false" />
     83       </Host>
     84     </Engine>
     85   </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
    
     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 &quot;%r&quot; %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 &quot;%r&quot; %s %b" />
     68           <Context path="" docBase="/usr/local/tomcat/virtual/vhost2" reloadable="false" />
     69       </Host>
     70
     71     </Engine>
     72   </Service>
    

    4.重新启动Tomcat,稍等片刻即可完成部署.

    [root@localhost ~]# /usr/local/tomcat/bin/catalina.sh stop
    [root@localhost ~]# /usr/local/tomcat/bin/catalina.sh start
    

    版权声明: 本博客,文章与代码均为学习时整理的笔记,博客中除去明确标注有参考文献的文章,其他文章【均为原创】作品,转载请务必【添加出处】,您添加出处是我创作的动力!

    警告:如果您恶意转载本人文章,则您的整站文章,将会变为我的原创作品,请相互尊重!
  • 相关阅读:
    DedeCMS用channelartlist调用顶级栏目及列表
    利用SQL语句替换织梦DedeCms数据库内容
    PHP 获取当前目录下的所有文件
    APP 商城功能
    left join , inner join 区别
    微信支付现金红包接口(转)
    微信红包发送规则
    PHP中的排序函数sort、asort、rsort、krsort、ksort区别分析(转)
    调用微信红包接口返回(转)
    一起发红包 微信平台红包接口调用教程(转)
  • 原文地址:https://www.cnblogs.com/LyShark/p/11827477.html
Copyright © 2020-2023  润新知