虚拟主机(Virtual Host)
概念:同一台机器上搭建属于不同域名或者基于不同IP的多个网站服务技术,可以为运行在同一台物理机器上的各个网站指配不同的IP和端口,也可以让多个网站拥有不同的域名。
根据需求创建虚拟主机
1.需求
同一台主机上运行两套应用程序,通过端口区分(基于端口的虚拟主机)(一套程序是通过50001端口,另外一套是通过50002端口),基于之前搭建的环境上。
2.实施内容
1)因为虚拟主机是基于端口的,所以修改配置文件/etc/httpd/conf/httpd.conf
Listen 80
⇒
Listen 50001
Listen 50002
2)定义虚拟主机的配置文件
第一个虚拟主机的配置文件
/etc/httpd/conf.d/virtual.d/virtual1.conf
ServerName 192.168.100.29
DocumentRoot "/var/www/html_virtual1"
Options FollowSymLinks
AllowOverride None
Require all granted
CustomLog logs/access_virtual1_log combined env=!nolog
ErrorLog logs/error_virtual1.log
ErrorDocument 400 /error/40X.json
ErrorDocument 401 /error/40X.json
ErrorDocument 403 /error/40X.json
ErrorDocument 404 /error/40X.json
ErrorDocument 405 /error/40X.json
ErrorDocument 408 /error/40X.json
ErrorDocument 410 /error/40X.json
ErrorDocument 411 /error/40X.json
ErrorDocument 412 /error/40X.json
ErrorDocument 413 /error/40X.json
ErrorDocument 414 /error/40X.json
ErrorDocument 415 /error/40X.json
ErrorDocument 500 /error/50X.json
ErrorDocument 501 /error/50X.json
ErrorDocument 502 /error/50X.json
ErrorDocument 503 /error/50X.json
ErrorDocument 506 /error/50X.json
Alias /error/ "/var/www/error_virtual1/"
IncludeOptional conf.d/mod_jk_virual1.conf
第二个虚拟主机的配置文件
/etc/httpd/conf.d/virtual.d/virtual2.conf
ServerName 192.168.100.29
DocumentRoot "/var/www/html_virtual2"
Options FollowSymLinks
AllowOverride None
Require all granted
CustomLog logs/access_virtual2_log combined env=!nolog
ErrorLog logs/error_virtual2.log
ErrorDocument 400 /error/40X.json
ErrorDocument 401 /error/40X.json
ErrorDocument 403 /error/40X.json
ErrorDocument 404 /error/40X.json
ErrorDocument 405 /error/40X.json
ErrorDocument 408 /error/40X.json
ErrorDocument 410 /error/40X.json
ErrorDocument 411 /error/40X.json
ErrorDocument 412 /error/40X.json
ErrorDocument 413 /error/40X.json
ErrorDocument 414 /error/40X.json
ErrorDocument 415 /error/40X.json
ErrorDocument 500 /error/50X.json
ErrorDocument 501 /error/50X.json
ErrorDocument 502 /error/50X.json
ErrorDocument 503 /error/50X.json
ErrorDocument 506 /error/50X.json
Alias /error/ "/var/www/error_virtual2/"
IncludeOptional conf.d/mod_jk_virual2.conf
3)为了让apache启动时,加载这两个配置文件,在/etc/httpd/conf/httpd.conf中追加下面的配置
IncludeOptional conf.d/virtual.d/*.conf
4)分别创建两条虚拟主机的文档目录,并在里面放上需要访问的html资源
mkdir /var/www/html_virtual1
virutal1test.html
mkdir /var/www/html_virtual2
virutal2test.html
5)从上面各自虚拟主机的配置文件来看,都定义了各自的mod_jk文件,所以创建mod_jk文件,worker还是使用以前定义好的tomat1.
/etc/httpd/conf.d/mod_jk_virtual1.conf
JkMount /virtual1/* tomcat1
/etc/httpd/conf.d/mod_jk_virtual2.conf
JkMount /virtual2/* tomcat1
6)在tomcat里放上两个war包,一个名叫virutal1,virtual2
7)重启apache,重启tomcat
systemctl restart httpd
/opt/tomcat/bin/shutdown.sh
/opt/tomcat/bin/startup.sh
8)测试apache的静态资源是否能正常访问
http://192.168.100.29:50001/virtual1test.html
结果:正常显示
http://192.168.100.29:50002/virtual2test.html
结果:正常显示
9)确认log
/var/log/httpd/access_virtual1_log
10.0.210.135 - - [03/Aug/2017:15:34:59 +0900] "GET /virtual1test.html HTTP/1.1" 200 53 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)"
/var/log/httpd/access_virtual2_log
10.0.210.135 - - [03/Aug/2017:15:45:54 +0900] "GET /virtual2test.html HTTP/1.1" 200 53 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)"
10)通过apache&mod_jk&tomcat访问tomcat的资源
http://192.168.100.29:50001/virtual1/index.jsp
结果:正常显示
http://192.168.100.29.50002/virtual2/index.jsp
结果:正常显示
11)确认log
/var/log/httpd/access_virtual1_log
10.0.210.135 - - [03/Aug/2017:15:52:15 +0900] "GET /virtual1/index.jsp HTTP/1.1" 200 290 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)"
/var/log/httpd/access_virtual2_log
10.0.210.135 - - [03/Aug/2017:15:52:55 +0900] "GET /virtual2/index.jsp HTTP/1.1" 200 286 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)"
总结:总体来说,配的很顺利,但是对于一些概念,还需要学习,比如对于ServerName的概念,我就不是特别理解。看到httpd.conf里的解释是这样的解释的
#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
If your host doesn't have a registered DNS name, enter its IP address here.