1. 程式人生 > >Linux9.6 httpd默認虛擬主機

Linux9.6 httpd默認虛擬主機

配置 就是 pos usr blog cor 文件中 ast eas

可以理解為,一臺服務器跑了多個網站,既能訪問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上,都會訪問第一個,所以一般我們把默認虛擬主機弄成空的。

[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/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

[root@chy002 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
[root@chy002 htdocs]# curl abc.com
abc.com[root@chy002 htdocs]# curl 111.com
111.com[root@chy002 htdocs]# curl 192.168.212.130
abc.com
#默認abc.com是默認主機

  

Linux9.6 httpd默認虛擬主機