Tomcat 基于域名的虚拟主机配置

使用基于域名的虚拟主机配置是比较流行的方式,可以在同一个IP上配置多个域名并且都通过80端口访问。

一、假设服务器有个IP地址为192.168.2.170
[root@localhost ~]# ifconfig ens33:5 192.168.2.170/24 up
[root@localhost ~]# ifconfig
ens33:5: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.170  netmask 255.255.255.0  broadcast 192.168.2.255
        ether 00:0c:29:16:90:ae  txqueuelen 1000  (Ethernet)
二、192.168.2.170对应的域名如下,配置主机的host文件便于测试
[root@localhost ~]# vim /etc/hosts
[root@localhost ~]# cat /etc/hosts|grep 192.168.2.170
192.168.2.170 www.site1.com
192.168.2.170 www.site2.com
192.168.2.170 www.site3.com
三、建立虚拟主机存放网页的根目录,并创建首页文件index.html
[root@localhost tomcat1]# mkdir site1App/ site2App/ site3App/
[root@localhost tomcat1]# mkdir site1App/ROOT/ site2App/ROOT/ site3App/ROOT/

[root@localhost tomcat1]# echo "site1" > site1App/ROOT/index.html
[root@localhost tomcat1]# echo "site2" > site2App/ROOT/index.html
[root@localhost tomcat1]# echo "site3" > site3App/ROOT/index.html

四、修改 conf/server.xml 在文件末尾加入以下配置

<Host appBase="site1App" autoDeploy="true" name="www.site1.com" unpackWARs="true">Host>
<Host appBase="site2App" autoDeploy="true" name="www.site2.com" unpackWARs="true">Host>
<Host appBase="site3App" autoDeploy="true" name="www.site3.com" unpackWARs="true">Host>

五、配置完以后可以启动 Tomcat 服务并进行测试

[root@localhost tomcat1]# service tomcat start
Starting tomcat (via systemctl):                           [  确定  ]
[root@localhost 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@localhost tomcat1]# curl http://www.site1.com/
site1
[root@localhost tomcat1]# curl http://www.site2.com/
site2
[root@localhost tomcat1]# curl http://www.site3.com/
site3

你可能感兴趣的:(Tomcat)