tomcat部署虚拟主机
需求一:创建两台虚拟主机
关键部分
在tomcat的server.xml配置文件,创建两个域名的虚拟机
123 <Host name=”www.a.com” appBase=”a”
124 unpackWARs=”true” autoDeploy=”true”>
125 </Host>
126 <Host name=”www.b.com” appBase=”b”
127 unpackWARs=”true” autoDeploy=”true”>
128 </Host>
具体的配置过程:
步骤一:修改配置文件
[root@lvsweb1 ~]# vim /usr/local/tomcat/conf/server.xml
123 <Host name=”www.a.com” appBase=”a”
124 unpackWARs=”true” autoDeploy=”true”>
125 </Host>
126 <Host name=”www.b.com” appBase=”b”
127 unpackWARs=”true” autoDeploy=”true”>
128 </Host>
步骤二:创建相应的文件
[root@lvsweb1 ~]# mkdir -p /usr/local/tomcat/{a,b}/ROOT
[root@lvsweb1 ~]# echo “AAA” > /usr/local/tomcat/a/ROOT/index.html
[root@lvsweb1 ~]# echo “BBB” > /usr/local/tomcat/b/ROOT/index.html
步骤三:重新开启服务
[root@lvsweb1 ~]#/usr/local/tomcat/bin/shutdown.sh
[root@lvsweb1 ~]#/usr/local/tomcat/bin/startup.sh
[root@lvsweb1 ~]# vim /etc/hosts
… …
192.168.6.12 www.a.com www.b.com
[root@lvsweb1 ~]# firefox http://www.a.com:8080/
[root@lvsweb1 ~]# firefox http://www.b.com:8080/
需求二:将www.b.com网站的首页目录为base
关键部分
修改配置文件server.xml,如果不改的化,默认是访问的是ROOT,现在要修改成访问base
[root@web1 ~]# vim /usr/local/tomcat/conf/server.xml
<Host name=”www.a.com” appBase=”a” unpackWARS=”true” autoDeploy=”true”>
</Host>
<Host name=”www.b.com” appBase=”b” unpackWARS=”true” autoDeploy=”true”>
<Context path=”” docBase=”base” reloadable=”true”/>
</Host>
[root@web1 ~]# mkdir /usr/local/tomcat/b/base
[root@web1 ~]# echo “BASE” > /usr/local/tomcat/b/base/index.html
[root@web1 ~]# /usr/local/tomcat/bin/shutdown.sh
[root@web1 ~]# /usr/local/tomcat/bin/startup.sh
[root@client ~]# firefox http://www.b.com:8080/ //结果为base目录下的页面内容
需求三:当用户访问http://www.a.com/test打开/var/www/html目录下的页面
关键部分
<Host name=”www.a.com” appBase=”a” unpackWARS=”true” autoDeploy=”true”>
<Context path=”/test” docBase=”/var/www/html/” />
</Host>
<Host name=”www.b.com” appBase=”b” unpackWARS=”true” autoDeploy=”true”>
<Context path=”” docBase=”base” />
</Host>
配置过程
[root@web1 ~]# vim /usr/local/tomcat/conf/server.xml
<Host name=”www.a.com” appBase=”a” unpackWARS=”true” autoDeploy=”true”>
<Context path=”/test” docBase=”/var/www/html/” />
</Host>
<Host name=”www.b.com” appBase=”b” unpackWARS=”true” autoDeploy=”true”>
<Context path=”” docBase=”base” />
</Host>
[root@web1 ~]# echo “Test” > /var/www/html/index.html
[root@web1 ~]# /usr/local/tomcat/bin/shutdown.sh
[root@web1 ~]# /usr/local/tomcat/bin/startup.sh
[root@client ~]# firefox http://www.a.com:8080/test
//返回/var/www/html/index.html的内容
//注意,访问的端口为8080
需求四:配置Tomcat支持SSL加密网站
关键部分
[root@web1 ~]# keytool -genkeypair -alias tomcat -keyalg RSA -keystore /usr/local/tomcat/keystore //提示输入密码为:123456
//-genkeypair 生成密钥对
//-alias tomcat 密钥别名
//-keyalg RSA 定义密钥算法为RSA算法
//-keystore 定义密钥文件存储在:/usr/local/tomcat/keystore
[root@web1 ~]# vim /usr/local/tomcat/conf/server.xml
… …
<Connector port=”8443” protocol=”org.apache.coyote.http11.Http11NioProtocol”
maxThreads=”150” SSLEnabled=”true” scheme=”https” secure=”true”
keystoreFile=”/usr/local/tomcat/keystore” keystorePass=”123456” clientAuth=”false” sslProtocol=”TLS” />
//备注,默认这段Connector被注释掉了,打开注释,添加密钥信息即可
[root@web1 ~]# /usr/local/tomcat/bin/shutdown.sh
[root@web1 ~]# /usr/local/tomcat/bin/startup.sh
[root@client ~]# vim /etc/hosts
… …
192.168.2.100 www.a.com www.b.com
[root@client ~]# firefox https://www.a.com:8443/
[root@client ~]# firefox https://www.b.com:8443/
[root@client ~]# firefox https://192.168.2.100:8443/
需求五:配置Tomcat日志
[root@web1 ~]# vim /usr/local/tomcat/conf/server.xml
.. ..
<Host name=”www.a.com” appBase=”a” unpackWARS=”true” autoDeploy=”true”>
<Context path=”/test” docBase=”/var/www/html/” />
<Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs”
prefix=” a_access” suffix=”.txt”
pattern=”%h %l %u %t "%r" %s %b” />
</Host>
<Host name=”www.b.com” appBase=”b” unpackWARS=”true” autoDeploy=”true”>
<Context path=”” docBase=”base” />
<Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs”
prefix=” b_access” suffix=”.txt”
pattern=”%h %l %u %t "%r" %s %b” />
</Host>
[root@web1 ~]# /usr/local/tomcat/bin/shutdown.sh
[root@web1 ~]# /usr/local/tomcat/bin/startup.sh
[root@lvsweb1 ~]# ls /usr/local/tomcat/logs/
a_access.2018-08-29.txt catalina.2018-08-29.log catalina.out host-manager.2018-08-29.log localhost.2018-08-29.log localhost_access_log.2018-08-29.txt manager.2018-08-29.log
需求六:配置Tomcat集群
1) 在192.168.4.5主机上配置Nginx调度器(具体安装步骤参考前面的章节)
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
http{
upstream toms {
server 192.168.2.100:8080;
server 192.168.2.200:8080;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://toms;
}
}
}
2) 在192.168.2.100和192.168.2.200主机上配置Tomcat调度器
以下以Web1为例:
[root@web1 ~]# yum -y install java-1.8.0-openjdk //安装JDK
[root@web1 ~]# yum -y install java-1.8.0-openjdk-headless //安装JDK
[root@web1 ~]# tar -xzf apache-tomcat-8.0.30.tar.gz
[root@web1 ~]# mv apache-tomcat-8.0.30 /usr/local/tomcat
3)启动服务
[root@web1 ~]# /usr/local/tomcat/bin/startup.sh