Tomcat virtual host configuration based on domain name

Keywords: Tomcat curl Java Apache

Using domain name based virtual host configuration is a popular way. You can configure multiple domain names on the same IP and access them through port 80.

1, Suppose the server has an IP address of 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)
2, The domain name corresponding to 192.168.2.170 is as follows. Configure the host file of the host for testing
[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
3, Set up the root directory where the virtual host stores the web page, and create the homepage file 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

4, Modify conf/server.xml to add the following configuration at the end of the file

<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>

5, After configuration, you can start the Tomcat service and test it

[root@localhost tomcat1]# service tomcat start
Starting tomcat (via systemctl):                           [  Determine  ]
[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 Three 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...

8month 30 13:14:06 bogon systemd[1]: Starting SYSV: tomcat server...
8month 30 13:14:06 bogon tomcat[2456]: Using CATALINA_BASE:   /usr/local/tomcat_cluster/tomcat1
8month 30 13:14:06 bogon tomcat[2456]: Using CATALINA_HOME:   /usr/local/tomcat_cluster/tomcat1
8month 30 13:14:06 bogon tomcat[2456]: Using CATALINA_TMPDIR: /usr/local/tomcat_cluster/tomcat1/temp
8month 30 13:14:06 bogon tomcat[2456]: Using JRE_HOME:        /usr/local/src/jdk1.8.0_131
8month 30 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
8month 30 13:14:06 bogon tomcat[2456]: Tomcat started.
8month 30 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

Posted by karlovac on Tue, 05 May 2020 01:55:34 -0700