一个服务器主机可以运行多个网站,每个网站都是一个虚拟主机;
任何一个域名解析到这台机器,都可以访问的虚拟主机就是默认虚拟主机;
在dns还未生效时,通过修改hosts文件,设置ip与域名的映射解析,来配置域名。
一、在Windows中编辑hosts文件
Windows系统中hosts位置“C:\Windows\System32\drivers\etc\hosts”
说明:在此可以自定义匹配本地IP和域名,目的是添加临时访问地址(在DNS未生效的时候使用)。
在hosts中添加一行:
192.168.204.128 ????www.abc.com
在windows上ping一下看看能否访问定义的IP:
在浏览器中:
二、在Linux中设置httpd默认虚拟主机
在物理机访问的域名
www.abc.com并未在虚拟机Apache配置文件中定义,虚拟机中只定义了ServerName www.example.com:80一个域名,该域名即为Apache的默认主机,此时通过任何一个绑定该虚拟机IP的域名进行访问都会跳转到该主机。
因为一台服务器可以跑多个域名,为了方便管理,需要对虚拟主机进行配置:
1、编辑httpd配置文件
[root@zlinux ~]# vim /usr/local/apache2/conf/httpd.conf ??//搜索httpd-vhost,去掉#2、编辑虚拟主机配置文件
[root@zlinux ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf 修改内容:
<VirtualHost *:80> ????????????????????????????????????//每个VirtualHost代表一个主机,一个主机代表一个网站 ???????ServerAdmin webmaster@dummy-host.example.com ????//定义管理员的邮箱 ???DocumentRoot "/data/webroot/abctest" ??????????????????//指定根目录 ???ServerName zlinux.com ???????????????????????????????????????????????????//定义网站名称 ???ServerAlias zlinuxbak.com ???????????????????????????????????????????????//设置网站别名 ???ErrorLog "logs/dummy-host.example.com-error_log" ??????//错误日志 ???CustomLog "logs/dummy-host.example.com-access_log" common</VirtualHost><VirtualHost *:80> ???ServerAdmin webmaster@dummy-host2.example.com ???DocumentRoot "/data/webroot/123test" ???ServerName 123test.com ???ServerAlias www.example.com ???ErrorLog "logs/dummy-host2.example.com-error_log" ???CustomLog "logs/dummy-host2.example.com-access_log" common</VirtualHost>说明:此时即定义了一个网站,定义网站的核心参数即为:DocumentRoot(网站根目录)、ServerName(域名)。
注意: 虚拟主机生效后,原Apache配置文件中的默认主机(www.example.com 192.168.204.128)就会失效。
3、添加虚拟主机相应目录
[root@zlinux ~]# mkdir /data/wwwroot/[root@zlinux ~]# mkdir /data/wwwroot/abctest[root@zlinux ~]# mkdir /data/wwwroot/123test在目录中创建PHP测试文件:
[root@zlinux ~]# vim /data/wwwroot/abctest/index.php<?phpecho "this is a testfile!";?>[root@zlinux ~]# vim /data/wwwroot/123test/index.php<?phpecho "这是一个123test文件!";?>4、测试虚拟主机
[root@zlinux ~]# curl -x 192.168.204.128:80 zlinux.comThis is a testfile![root@zlinux ~]# [root@zlinux ~]# curl -x 192.168.204.128:80 123test.com这是一个123test文件![root@zlinux ~]# [root@zlinux ~]# curl -x 192.168.204.128:80 000test.com ????//在没有指定或错误域名的情况下,访问任意的域名都将访问到默认主机所对应的内容。This is a testfile![root@zlinux ~]# httpd配置-默认虚拟主机
原文地址:http://blog.51cto.com/3069201/2074422