Tomcat服务器 设置 不同端口、域名、网站

Tomcat服务器

不同端口 不同域名 不同网站
  • 服务器:阿里的
  • 系统:Ubuntu 16.04
  • Tomcat版本: Apache Tomcat/8.5.42
  • 软件:WinSCP、petty.exe

原来根据哔哩哔哩教程:设置了一个网站,用www.a.com访问。现在想增加www.b.com访问另一个网站。
参考网上的教程找到tomcat安装的路径,修改tomcat/conf/server.xml,一顿操作,发现没有用。
在这里插入图片描述

于是自己研究了一下,弄好了。

serve.xml(旧)

删了注释的

    <?xml version="1.0" encoding="UTF-8"?>
    <Server port="8005" shutdown="SHUTDOWN">
      <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
      <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
      <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
      <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
      <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
      <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="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 &quot;%r&quot; %s %b" />
          </Host>
          
        </Engine>
      </Service>
    </Server>

serve.xml(新)

    <?xml version="1.0" encoding="UTF-8"?>
    <Server port="8005" shutdown="SHUTDOWN">
      <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
      <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
      <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
      <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
      <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
      <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="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.a.com"  appBase="webapps"
                unpackWARs="true" autoDeploy="true">
                 <!-- <Context docBase="app1" path="/app1"  reloadable="true" /> -->
            <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs/1"
                   prefix="localhost_access_log" suffix=".txt"
                   pattern="%h %l %u %t &quot;%r&quot; %s %b" />
    
          </Host>
    
          <Host name="www.b.com"  appBase="webapps2"
                unpackWARs="true" autoDeploy="true">
                <!-- <Context docBase="app2" path=""  reloadable="true" /> -->
                <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs/2"
                   prefix="localhost_access_log" suffix=".txt"
                   pattern="%h %l %u %t &quot;%r&quot; %s %b" />
          </Host>
          
          <!-- 修改的地方 -->

        </Engine>
      </Service>
    </Server>

细心的你可以发现:

只有Host里面的东西改了一下。新增或修改的内容如下:

    <Host name="www.a.com"  appBase="webapps"
                unpackWARs="true" autoDeploy="true">
                 <!-- <Context docBase="app1" path="/app1"  reloadable="true" /> -->
            <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs/1"
                   prefix="localhost_access_log" suffix=".txt"
                   pattern="%h %l %u %t &quot;%r&quot; %s %b" />
    
    </Host>
    
    <Host name="www.b.com"  appBase="webapps2"
                unpackWARs="true" autoDeploy="true">
                <!-- <Context docBase="app2" path=""  reloadable="true" /> -->
                <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs/2"
                   prefix="localhost_access_log" suffix=".txt"
                   pattern="%h %l %u %t &quot;%r&quot; %s %b" />
    
    </Host>

里面多了一个webapps2,我们要在tomcat目录下把原来的webapps复制一下,改名成为webapps2,就行。

在这里插入图片描述

然后要用命令重启tomcat:看这个教程

关闭tomcat,测试一下,是无法访问自己网站的。
在这里插入图片描述
之后就能通过不同网址访问不同网站了。记得阿里云控制台要把你要用的端口开放。

其他问题

但是有一个小bug。访问网址如果没有 www,就会访问不到。

尝试1:

于是我做做出如下修改:(去掉www)


   <Host name="a.com"  appBase="webapps"
               unpackWARs="true" autoDeploy="true">
                <!-- <Context docBase="app1" path="/app1"  reloadable="true" /> -->
           <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs/1"
                  prefix="localhost_access_log" suffix=".txt"
                  pattern="%h %l %u %t &quot;%r&quot; %s %b" />
   
         </Host>
   
           <Host name="b.com"  appBase="webapps2"
               unpackWARs="true" autoDeploy="true">
               <!-- <Context docBase="app2" path=""  reloadable="true" /> -->
               <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs/2"
                  prefix="localhost_access_log" suffix=".txt"
                  pattern="%h %l %u %t &quot;%r&quot; %s %b" />
   
         </Host>
         

然后要用命令重启tomcat,但是发现,网址加上www话就无法访问了。真是两者不可得兼。

尝试2:

最后对xml不熟悉的我,用了如下麻烦的方法:


   <Host name="a.com"  appBase="webapps"
             unpackWARs="true" autoDeploy="true">
              <!-- <Context docBase="app1" path="/app1"  reloadable="true" /> -->
         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs/1"
                prefix="localhost_access_log" suffix=".txt"
                pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  </Host>
   
 <Host name="www.a.com"  appBase="webapps"
             unpackWARs="true" autoDeploy="true">
              <!-- <Context docBase="app1" path="/app1"  reloadable="true" /> -->
         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs/1"
                prefix="localhost_access_log" suffix=".txt"
                pattern="%h %l %u %t &quot;%r&quot; %s %b" />
 </Host>
 
 
 <Host name="b.com"  appBase="webapps2"
             unpackWARs="true" autoDeploy="true">
             <!-- <Context docBase="app2" path=""  reloadable="true" /> -->
             <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs/2"
                prefix="localhost_access_log" suffix=".txt"
                pattern="%h %l %u %t &quot;%r&quot; %s %b" />
 
 </Host>
 <Host name="www.b.com"  appBase="webapps2"
             unpackWARs="true" autoDeploy="true">
             <!-- <Context docBase="app2" path=""  reloadable="true" /> -->
             <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs/2"
                prefix="localhost_access_log" suffix=".txt"
                pattern="%h %l %u %t &quot;%r&quot; %s %b" />
 
 </Host>

简单粗暴,这样子两个方式都能访问了。
如果大家有简单的方法,可以留言,教教小编,多谢。

发布了15 篇原创文章 · 获赞 4 · 访问量 1247

猜你喜欢

转载自blog.csdn.net/qq_37152533/article/details/98642764