Tomcat 基于端口的虚拟主机配置

如一台服务器只有一个IP或需要通过不同的端口访问不同的虚拟主机,可以使用基于端口的虚拟主机配置。

一、假设服务器有个IP地址为192.168.1.106
[root@bogon tomcat1]# ifconfig
ens33: flags=4163  mtu 1500
        inet 192.168.1.106  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::2a8d:be6:a4a8:ea0  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:24:26:9c  txqueuelen 1000  (Ethernet)
        RX packets 37522  bytes 2896962 (2.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 221  bytes 34199 (33.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
二、需要配置的虚拟主机分别为9081、9082和9083,配置主机的host文件便于测试。
[root@localhost conf]# vim /etc/hosts
[root@bogon tomcat1]# cat /etc/hosts|grep 192.168.1.106
192.168.1.106 www.testtomcat.com
三、建立虚拟主机存放网页的根目录,并创建首页文件index.html
[root@bogon tomcat1]# mkdir webapps1  webapps2  webapps3
[root@bogon tomcat1]# ls
bin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  webapps1  webapps2  webapps3  work

[root@bogon tomcat1]# mkdir webapps1/test1 webapps2/test2 webapps3/test3

[root@bogon tomcat1]# echo "test9081" > webapps1/test1/index.html
[root@bogon tomcat1]# echo "test9082" > webapps2/test2/index.html
[root@bogon tomcat1]# echo "test9083" > webapps3/test3/index.html
四、修改 conf/server.xml 在文件末尾加入以下配置
[root@bogon tomcat1]# vim conf/server.xml

<Service name="myService1" >
        <Connector port="9081"/>
        <Engine name="myEngine1" defaultHost="test1" >
                <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" />
                <Host name="test1" appBase="webapps1" />
        Engine>
Service>


<Service name="myService2" >
        <Connector port="9082"/>
        <Engine name="myEngine2" defaultHost="test2" >
                <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" />
                <Host name="test2" appBase="webapps2" />
        Engine>
Service>

<Service name="myService3" >
        <Connector port="9083"/>
        <Engine name="myEngine3" defaultHost="test3" >
                <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" />
                <Host name="test3" appBase="webapps3" />
        Engine>
Service>
五、配置完以后可以启动 Tomcat 服务并进行测试
[root@bogon tomcat1]# service tomcat start
Starting tomcat (via systemctl):                           [  确定  ]
[root@bogon tomcat1]# systemctl status tomcat
● tomcat.service - SYSV: tomcat server
   Loaded: loaded (/etc/rc.d/init.d/tomcat; bad; vendor preset: disabled)
   Active: active (running) since 三 2017-08-30 13:14:06 CST; 15s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 2415 ExecStop=/etc/rc.d/init.d/tomcat stop (code=exited, status=0/SUCCESS)
  Process: 2456 ExecStart=/etc/rc.d/init.d/tomcat start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/tomcat.service
           ├─1397 /usr/local/src/jdk1.8.0_131/bin/java -Djava.util.logging.config.file=/usr/local/tomcat_cluster/tomcat1/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.Class...
           └─2461 /usr/local/src/jdk1.8.0_131/bin/java -Djava.util.logging.config.file=/usr/local/tomcat_cluster/tomcat1/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.Class...

830 13:14:06 bogon systemd[1]: Starting SYSV: tomcat server...
830 13:14:06 bogon tomcat[2456]: Using CATALINA_BASE:   /usr/local/tomcat_cluster/tomcat1
830 13:14:06 bogon tomcat[2456]: Using CATALINA_HOME:   /usr/local/tomcat_cluster/tomcat1
830 13:14:06 bogon tomcat[2456]: Using CATALINA_TMPDIR: /usr/local/tomcat_cluster/tomcat1/temp
830 13:14:06 bogon tomcat[2456]: Using JRE_HOME:        /usr/local/src/jdk1.8.0_131
830 13:14:06 bogon tomcat[2456]: Using CLASSPATH:       /usr/local/tomcat_cluster/tomcat1/bin/bootstrap.jar:/usr/local/tomcat_cluster/tomcat1/bin/tomcat-juli.jar
830 13:14:06 bogon tomcat[2456]: Tomcat started.
830 13:14:06 bogon systemd[1]: Started SYSV: tomcat server.
[root@bogon tomcat1]# curl http://www.testtomcat.com:9081/test1/
test9081
[root@bogon tomcat1]# curl http://www.testtomcat.com:9082/test2/
test9082
[root@bogon tomcat1]# curl http://www.testtomcat.com:9083/test3/
test9083
附:配置过程中的问题
1、在编辑配置文件 conf/server.xml 时发生的问题
29-Aug-2017 22:36:58.245 警告 [main] org.apache.catalina.startup.Catalina.load Catalina.start using conf/server.xml: 元素类型 "Service" 必须后跟属性规范 ">""/>"29-Aug-2017 22:36:58.245 严重 [main] org.apache.catalina.startup.Catalina.start Cannot start server. Server instance is not configured.
29-Aug-2017 22:53:48.084 严重 [main] org.apache.tomcat.util.digester.Digester.fatalError Parse Fatal Error at line 168 column 9: 元素类型 "Service" 必须后跟属性规范 ">""/>"
29-Aug-2017 23:51:42.237 警告 [main] org.apache.catalina.startup.Catalina.load Catalina.start using conf/server.xml: 元素类型 "Connector" 必须后跟属性规范 ">""/>"29-Aug-2017 23:51:42.243 严重 [main] org.apache.tomcat.util.digester.Digester.fatalError Parse Fatal Error at line 169 column 27: 元素类型 "Connector" 必须后跟属性规范 ">""/>"。
 org.xml.sax.SAXParseException; systemId: file:/usr/local/tomcat_cluster/tomcat1/conf/server.xml; lineNumber: 169; columnNumber: 27; 元素类型 "Connector" 必须后跟属性规范 ">" 或 "/>"。
29-Aug-2017 23:54:15.068 警告 [main] org.apache.catalina.startup.Catalina.load Catalina.start using conf/server.xml: 元素类型 "Engine" 必须后跟属性规范 ">""/>"29-Aug-2017 23:54:15.068 严重 [main] org.apache.catalina.startup.Catalina.start Cannot start server. Server instance is not configured.

发生这些问题的原因是:有些地方的空格或Tab不是在英文状态下添加的。

解决方法:将所有的空格或Tab全部都删掉,在英文状态下重敲一遍,要确保实在英文状态下。

你可能感兴趣的:(Tomcat)