Linux9.6 httpd默认虚拟主机

Posted Learning Notes

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux9.6 httpd默认虚拟主机相关的知识,希望对你有一定的参考价值。

可以理解为,一台服务器跑了多个网站,既能访问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             localhost

192.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上,都会访问第一个,所以一般我们把默认虚拟主机弄成空的。

[[email protected] 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>



[[email protected] htdocs]# mkdir -p  /data/wwwroot/abc.com
[[email protected] htdocs]# mkdir  /data/wwwroot/111.com
[[email protected] htdocs]# vim /data/wwwroot/abc.com/index.php
[[email protected] htdocs]# vim /data/wwwroot/111.com/index.php
[[email protected] htdocs]# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.212.130 111.com abc.com

[[email protected] htdocs]# ping abc.com
PING 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 2000ms
rtt min/avg/max/mdev = 0.051/0.058/0.070/0.008 ms
[[email protected] htdocs]# curl abc.com
abc.com[[email protected] htdocs]# curl 111.com
111.com[[email protected] htdocs]# curl 192.168.212.130
abc.com
#默认abc.com是默认主机

  

以上是关于Linux9.6 httpd默认虚拟主机的主要内容,如果未能解决你的问题,请参考以下文章

配置httpd结合php及默认虚拟主机

lamp架构-设置httpd的默认主机(服务器多网站运行设置)

httpd配置-默认虚拟主机

httpd之虚拟主机和默认虚拟主机

4.Apache和PHP结合 Apache默认虚拟主机

Apache与PHP的结合配置Apache默认虚拟主机