第三块:构建虚拟WEB主机
虚拟Web主机
在同一台服务器中运行多个Web站点,其中每一个站点并不独立占用一台真正的计算机
httpd支持的虚拟主机类型
1、基于域名的虚拟主机
2、基于IP地址的虚拟主机
3、基于端口的虚拟主机
第1步:基于ip的虚拟主机
需求:
1:构建2个虚拟Web站点:
www.benet.com,IP地址为 192.168.1.36
www.accp.com,IP地址为 192.168.1.63
2:在浏览器中访问这两个域名时,分别显示不同的内容
分析:在虚拟机中添加网卡,给定网桥模式,修改添加网卡信息,并且将mack地址修改。
[root@linuxidc conf.d]# vim vhost.conf
<VirtualHost 192.168.1.63:80> #定义虚拟主机,给定IP地址
ServerAdmin linuxidc@linuxidc.com #定义此虚拟主机的IP地址
DocumentRoot /opt/accp/ #定义虚拟主机的归属目录
ServerName www.accp.com #定义域名
ErrorLog logs/accp.com-error_log #错误日志
CustomLog logs/accp.com-access_log common
</VirtualHost>
<Directory "/opt/accp"> #定义虚拟主机的目录权限
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
Require all granted
</Directory>
<VirtualHost 192.168.1.36:80>
ServerAdmin admin@benet.com
DocumentRoot /opt/benet/
ServerName www.benet.com
ErrorLog logs/benet.com-error_log
CustomLog logs/benet.com-access_log common
</VirtualHost>
<Directory "/opt/benet">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
Require all granted
</Directory>
[root@linuxidc conf.d]# mkdir /opt/accp #创建虚拟主机的文件夹目录
[root@linuxidc conf.d]# mkdir /opt/benet
[root@linuxidc conf.d]# echo "<h1>welcome accp<h1>" > /opt/accp/index.html
[root@linuxidc conf.d]# echo "<h1>welcome benet<h1>" > /opt/benet/index.html #给定虚拟主机输入一些信息
[root@linuxidc conf.d]# service httpd restart
[root@linuxidc conf.d]# service iptables stop


第2步:基于端口地址的虚拟主机
[root@linuxidc conf.d]# vim vhost.conf
Listen 192.168.1.63:8080
<VirtualHost 192.168.1.63:8080>
ServerAdmin linuxidc@linuxidc.com
DocumentRoot /opt/accp/
ServerName www.accp.com
ErrorLog logs/accp.com-error_log
CustomLog logs/accp.com-access_log common
</VirtualHost>
<Directory "/opt/accp">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
Require all granted
</Directory>
<VirtualHost 192.168.1.63:80>
ServerAdmin admin@benet.com
DocumentRoot /opt/benet/
ServerName www.benet.com
ErrorLog logs/benet.com-error_log
CustomLog logs/benet.com-access_log common
</VirtualHost>
<Directory "/opt/benet">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
Require all granted
</Directory>
[root@linuxidc conf.d]# service httpd restart


第3步:基于域名的虚拟主机
[root@linuxidc conf.d]# vim vhost.conf
Listen 192.168.1.63:8080
<VirtualHost 192.168.1.63:8080>
ServerAdmin linuxidc@linuxidc.com
DocumentRoot /opt/accp/
ServerName www.accp.com
ErrorLog logs/accp.com-error_log
CustomLog logs/accp.com-access_log common
</VirtualHost>
<Directory "/opt/accp">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
Require all granted
</Directory>
<VirtualHost 192.168.1.63:80>
ServerAdmin admin@benet.com
DocumentRoot /opt/benet/
ServerName www.benet.com
ErrorLog logs/benet.com-error_log
CustomLog logs/benet.com-access_log common
</VirtualHost>
<Directory "/opt/benet">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
Require all granted
</Directory>
注释:hosts文件在C:\Windows\System32\drivers\etc下,hosts文件中添加以下内容,需要将此文件拖出到桌面修改,修改完之后再拖回去。
192.168.1.63 www.benet.com
192.168.1.63 www.accp.com



本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-03/141687.htm
