2019.9.21 Tomcat基于端口的虚拟主机
我这里做的是基于端口访问不同的测试页面 结果如下:
1,解压jdk包,跟apache的gcc gcc-c++的底层源码差不多
卸载rpm安装的jdk软件包
方法 1
[root@localhost ~]# rpm -qa | grep -i openjdk
java-1.8.0-openjdk-1.8.0.131-11.b12.el7.x86_64
java-1.7.0-openjdk-headless-1.7.0.141-2.6.10.5.el7.x86_64
java-1.8.0-openjdk-headless-1.8.0.131-11.b12.el7.x86_64
java-1.7.0-openjdk-1.7.0.141-2.6.10.5.el7.x86_64
[root@localhost ~]# rpm -e java-1.8.0-openjdk --nodeps //卸载不了的软件包加 --nodeps解除依赖关系
[root@localhost ~]# rpm -e java-1.7.0-openjdk-headless --nodeps
[root@localhost ~]# rpm -e java-1.8.0-openjdk-headless --nodeps
[root@localhost ~]# rpm -e java-1.7.0-openjdk --nodeps
方法 2
[root@localhost ~]# which java
/usr/bin/which: no java in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
[root@localhost ~]# rm -rf /usr/bin/java
[root@localhost ~]# java -version
2,jdk的安装
[root@nginx ~]# tar xf jdk-8u191-linux-x64.tar.gz
[root@localhost ~]# mv jdk1.8.0_191/ /usr/local/java //移动到路径取名叫java
[root@localhost ~]# java -version //查看显示没有环境变量
bash: java: 未找到命令...
[root@localhost ~]# vim /etc/profile //添加环境变量
在最下面加这个环境变量
export JAVA_HOME=/usr/local/java
export PATH=$PATH:$JAVA_HOME/bin
[root@localhost ~]# echo $PATH //一共有的查看环境变量
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]# source /etc/profile //从新加载刚刚配置的环境变量
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/java/bin //之后就可以看到我们刚刚配置的环境变量了
[root@localhost ~]# java -version //安装好的jdk就出来了
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
3,Tomcat的安装
[root@localhost ~]# tar xf apache-tomcat-8.5.40.tar.gz //解压
[root@localhost ~]# mv apache-tomcat-8.5.40 /usr/local/tomcat8 //移动到对应路径
[root@localhost ~]# ls /usr/local/tomcat8/bin/
bootstrap.jar configtest.bat setclasspath.sh tomcat-native.tar.gz
catalina.bat configtest.sh shutdown.bat tool-wrapper.bat
catalina.sh daemon.sh shutdown.sh tool-wrapper.sh //这里面就是tomcat的所有文件,startup.sh 开启脚本 shutdown.sh 关闭脚本
catalina-tasks.xml digest.bat startup.bat version.bat
commons-daemon.jar digest.sh startup.sh version.sh
commons-daemon-native.tar.gz setclasspath.bat tomcat-juli.jar
[root@localhost ~]# netstat -lnpt | grep :8080 //汤姆猫的端口的 8080
tcp6 0 0 :::8080 :::* LISTEN 4312/java
4,创建测试文件
[root@localhost ~]# mkdir /web/{a,b} //创建测试网页
[root@localhost ~]# vim /web/a/index.jsp //创建web目录和a/下面的index.jsp测试文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test page</title>
</head>
<body>
<% out.println("Welcome to test site,http://www.a.com");%>
</body>
</html>
[root@localhost ~]# vim /web/b/index.jsp //创建web目录和b/下面的index.jsp测试文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test page</title>
</head>
<body>
<% out.println("Welcome to test site,http://www.b.com");%>
</body>
</html>
5,配置主配置文件
root@localhost web]# vim /usr/local/tomcat8/conf/server.xml // 配置主配置文件
<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="Catalina1"> //设置第一个端口配置
<Connector port="8080" protocol="HTTP/1.1" //设定8080端口访问
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="/web/a" path="" reloadable="flase" //设定访问的网页路径位置
>
</Context>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
<Service name="Catalina2"> //第二个端口
<Connector port="8090" protocol="HTTP/1.1" //设定测试端口8090
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8010" 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="/web/b" path="" reloadable="flase" ////设定访问的网页路径位置
>
</Context>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
[root@localhost ~]# /usr/local/tomcat8/bin/shutdown.sh //关闭
[root@localhost ~]# /usr/local/tomcat8/bin/startup.sh //开启
[root@localhost ~]# tail -f /usr/local/tomcat8/logs/catalina.out //查看日志
22-Sep-2019 00:46:32.905 信息 [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
22-Sep-2019 00:46:33.049 信息 [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
22-Sep-2019 00:46:33.090 信息 [main] org.apache.catalina.startup.Catalina.start Server startup in 6325 ms