1、建立虚拟主机存放网页的根目录,并创建首页文件index.html
[root@bogon tomcat]# mkdir webapps1 webapps2 webapps3
[root@bogon tomcat]#pwd
/usr/local/apache-tomcat-8.5.6
[root@bogon tomcat]# ls
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps webapps1 webapps2 webapps3 work
[root@bogon tomcat]# mkdir webapps1/test1 webapps2/test2 webapps3/test3
[root@bogon tomcat]# echo "test9081" > webapps1/test1/index.html
[root@bogon tomcat]# echo "test9082" > webapps2/test2/index.html
[root@bogon tomcat]# echo "test9083" > webapps3/test3/index.html
2、修改 conf/server.xml 在文件加入以下配置,注意必须填在<Server></Server>标签内
<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>
3、配置完以后可以启动 Tomcat 服务并进行测试
[root@bogn tomcat]# ./bin/startup.sh
Using CATALINA_BASE: /usr/local/apache-tomcat-8.5.65
Using CATALINA_HOME: /usr/local/apache-tomcat-8.5.65
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-8.5.65/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/apache-tomcat-8.5.65/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.65/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@bogon tomcat1]# curl http://127.0.0.1:9081/test1/
test9081
[root@bogon tomcat1]# curl http://127.0.0.1:9082/test2/
test9082
[root@bogon tomcat1]# curl http://127.0.0.1:9083/test3/
test9083