可以理解为,一台服务器跑了多个网站,既能访问baidu 又能访问qq,就用了一个httpd服务,一个网站多个域名,每个网站都是对应一个虚拟主机。
httpd配置文件里DocumentRoot "/usr/local/apache2.4/htdocs"为什么可以在htdocs下访问到1.php,就是只因为这个DocumentRoot这个参数定义了该文件夹
域名为 ServerName 定义的名字
在windows平台修改hosts,在该文件定义域名IP,可以把IP指向某域名,在win的cmd上测试成功
C:\Windows\System32\drivers\etc# Copyright (c) 1993-2009 Microsoft Corp.## This is a sample HOSTS file used by Microsoft TCP/IP for Windows.## This file contains the mappings of IP addresses to host names. Each# entry should be kept on an individual line. The IP address should# be placed in the first column followed by the corresponding host name.# The IP address and the host name should be separated by at least one# space.## Additionally, comments (such as these) may be inserted on individual# lines or following the machine name denoted by a ‘#‘ symbol.## For example:## ?????102.54.94.97 ????rhino.acme.com ?????????# source server# ??????38.25.63.10 ????x.acme.com ?????????????# x client host# localhost name resolution is handled within DNS itself.#127.0.0.1 ??????localhost#::1 ????????????localhost192.168.212.130 ?www.chyuanliu.com ??www.chyuanliu1.com
在httpd配置文件中
ServerName www.example.com:80仅定义了一个域名,为apache的默认主机,也就是说任何一个域名,只要指向这个IP,都会访问到这个站点,这个站点就是默认主机。这就带来了问题,如果机子上跑多个域名,不希望所有域名都指向到这个站点,所有需要配置一个虚拟主机在httpd配置文件中# Virtual hosts#Include conf/extra/httpd-vhosts.conf
打开该注释之后,之前的httpd配置文件中的ServerName和DocumentRoot都会失效
vhosts配置文件中,每一个VirtualHost都是一个虚拟主机,都是一个站点,第一个是默认虚拟主机,即访问IP可以访问到,无论任何域名解析到这个IP上,都会访问第一个,所以一般我们把默认虚拟主机弄成空的。
[root@chy002 htdocs]# vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf<VirtualHost *:80> ???DocumentRoot "/data/wwwroot/abc.com" ??#网站根目录 ???ServerName abc.com ????????????????????????????????#网站域名 ???ServerAlias www.abc.com www.123.com ????#网站别名 ???ErrorLog "logs/abc.com-error_log" ???CustomLog "logs/abc.com-access_log" common</VirtualHost><VirtualHost *:80> ???DocumentRoot "/data/wwwroot/111.com" ???ServerName 111.com ???ServerAlias www.example.com ???ErrorLog "logs/111.com-error_log" ???CustomLog "logs/111.com-access_log" common</VirtualHost>[root@chy002 htdocs]# mkdir -p ?/data/wwwroot/abc.com[root@chy002 htdocs]# mkdir ?/data/wwwroot/111.com[root@chy002 htdocs]# vim /data/wwwroot/abc.com/index.php[root@chy002 htdocs]# vim /data/wwwroot/111.com/index.php[root@chy002 htdocs]# vi /etc/hosts127.0.0.1 ??localhost localhost.localdomain localhost4 localhost4.localdomain4::1 ????????localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.212.130 111.com abc.com[root@chy002 htdocs]# ping abc.comPING 111.com (192.168.212.130) 56(84) bytes of data.64 bytes from 111.com (192.168.212.130): icmp_seq=1 ttl=64 time=0.070 ms^C--- 111.com ping statistics ---3 packets transmitted, 3 received, 0% packet loss, time 2000msrtt min/avg/max/mdev = 0.051/0.058/0.070/0.008 ms[root@chy002 htdocs]# curl abc.comabc.com[root@chy002 htdocs]# curl 111.com111.com[root@chy002 htdocs]# curl 192.168.212.130abc.com#默认abc.com是默认主机
Linux9.6 httpd默认虚拟主机
原文地址:https://www.cnblogs.com/chyuanliu/p/8502538.html