shallow丿ovephp
網站可以直接訪問,實際上是DocumentRoot "/usr/local/apache2.4/htdocs"指定了一個文件web
[root@localhost ~]# vi /usr/local/apache2.4/conf/httpd.conf \DocumentRoot 211 # below. 212 # 213 214 # 215 # DocumentRoot: The directory out of which you will serve your 216 # documents. By default, all requests are taken from this directory, but 217 # symbolic links and aliases may be used to point to other locations. 218 # 219 DocumentRoot "/usr/local/apache2.4/htdocs" 220 <Directory "/usr/local/apache2.4/htdocs"> 221 # 222 # Possible values for the Options directive are "None", "All", 223 # or any combination of: 224 # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews 225 # 226 # Note that "MultiViews" must be named *explicitly* --- "Options All"
而域名爲apache
[root@localhost ~]# vi /usr/local/apache2.4/conf/httpd.conf /ServerName 188 # 189 # ServerName gives the name and port that the server uses to identify itself. 190 # This can often be determined automatically, but we recommend you specify 191 # it explicitly to prevent problems during startup. 192 # 193 # If your host doesn't have a registered DNS name, enter its IP address here. 194 # 195 ServerName www.example.com:80 196 197 # 198 # Deny access to the entirety of your server's filesystem. You must 199 # explicitly permit access to web content directories in other 200 # <Directory> blocks below. 201 # 202 <Directory /> 203 AllowOverride none
Windows下的hosts瀏覽器
C:\Users\Administrator>cd C:\Windows\System32\drivers\etc C:\Windows\System32\drivers\etc>start hosts # 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 127.0.0.1 localhost
在hosts中添加一行192.168.9.134 www.abc.com www.123.com服務器
C:\Users\Administrator>ping www.abc.com 正在 Ping www.abc.com [192.168.9.134] 具備 32 字節的數據: 來自 192.168.9.134 的回覆: 字節=32 時間<1ms TTL=64 來自 192.168.9.134 的回覆: 字節=32 時間<1ms TTL=64 來自 192.168.9.134 的回覆: 字節=32 時間<1ms TTL=64 來自 192.168.9.134 的回覆: 字節=32 時間<1ms TTL=64 192.168.9.134 的 Ping 統計信息: 數據包: 已發送 = 4,已接收 = 4,丟失 = 0 (0% 丟失), 往返行程的估計時間(以毫秒爲單位): 最短 = 0ms,最長 = 0ms,平均 = 0ms C:\Users\Administrator>ping www.123.com 正在 Ping www.abc.com [192.168.9.134] 具備 32 字節的數據: 來自 192.168.9.134 的回覆: 字節=32 時間<1ms TTL=64 來自 192.168.9.134 的回覆: 字節=32 時間<1ms TTL=64 來自 192.168.9.134 的回覆: 字節=32 時間<1ms TTL=64 來自 192.168.9.134 的回覆: 字節=32 時間<1ms TTL=64 192.168.9.134 的 Ping 統計信息: 數據包: 已發送 = 4,已接收 = 4,丟失 = 0 (0% 丟失), 往返行程的估計時間(以毫秒爲單位): 最短 = 0ms,最長 = 0ms,平均 = 0ms
瀏覽器訪問www.abc.com或www.123.com出現It work!則表示成功app
apache默認虛擬主機,當訪問ServerName和DocumentRoot 默認虛擬主機curl
[root@localhost ~]# vi /usr/local/apache2.4/conf/httpd.conf /extra 470 #Include conf/extra/httpd-languages.conf 471 472 # User home directories 473 #Include conf/extra/httpd-userdir.conf 474 475 # Real-time info on requests and configuration 476 #Include conf/extra/httpd-info.conf 477 478 # Virtual hosts 479 #Include conf/extra/httpd-vhosts.conf 480 481 # Local access to the Apache HTTP Server Manual 482 #Include conf/extra/httpd-manual.conf 483 484 # Distributed authoring and versioning (WebDAV) 485 #Include conf/extra/httpd-dav.conf
將479行的註釋去掉爲Include conf/extra/httpd-vhosts.confide
打開二級配置文件,虛擬主機配置文件網站
[root@localhost ~]# vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 23 <VirtualHost *:80> 24 ServerAdmin webmaster@dummy-host.example.com 25 DocumentRoot "/usr/local/apache2.4/docs/dummy-host.example.com" 26 ServerName dummy-host.example.com 27 ServerAlias www.dummy-host.example.com 28 ErrorLog "logs/dummy-host.example.com-error_log" 29 CustomLog "logs/dummy-host.example.com-access_log" common 30 </VirtualHost> 31 32 <VirtualHost *:80> 33 ServerAdmin webmaster@dummy-host2.example.com 34 DocumentRoot "/usr/local/apache2.4/docs/dummy-host2.example.com" 35 ServerName dummy-host2.example.com 36 ErrorLog "logs/dummy-host2.example.com-error_log" 37 CustomLog "logs/dummy-host2.example.com-access_log" common 38 </VirtualHost>
一個主機表明一個網站 ServerAdmin有無均可以 DocumentRoot表明網站根目錄 ServerName表明域名 ServerAlias表明別名 ErrorLog錯誤日誌 CustomLog訪問日誌this
修改成如下,若修改後以前的www.example.com就會訪問不了
23 <VirtualHost *:80> 24 DocumentRoot "/data/wwwroot/abc.com" 25 ServerName abc.com 26 ServerAlias www.abc.com www.123.com 27 ErrorLog "logs/abc.com-error_log" 28 CustomLog "logs/abc.com-access_log" common 29 </VirtualHost> 30 31 <VirtualHost *:80> 32 DocumentRoot "/data/wwwroot/111.com" 33 ServerName 111.com 34 ServerAlias www.example.com 35 ErrorLog "logs/111.com-error_log" 36 CustomLog "logs/111.com-access_log" common 37 </VirtualHost>
但能夠添加一個別名爲www.example.com,但他此時對應的目錄爲/data/wwwroot/111.com
在站點目錄下建立
[root@localhost ~]# mkdir /data/wwwroot/ [root@localhost ~]# mkdir /data/wwwroot/abc.com [root@localhost ~]# mkdir /data/wwwroot/111.com [root@localhost ~]# vi /data/wwwroot/abc.com/index.php <?php echo "hello!abc.com"; ?> [root@localhost ~]# vi /data/wwwroot/111.com/index.php <?php echo "hello!111.com"; ?> [root@localhost ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost ~]# ping www.abc.com PING abc.com (199.181.132.250) 56(84) bytes of data. ^C64 bytes from 199.181.132.250: icmp_seq=1 ttl=128 time=248 ms --- abc.com ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 248.780/248.780/248.780/0.000 ms
若沒有綁定hosts,則會訪問到外網,此時能夠使用curl -x
[root@localhost ~]# curl -x 192.168.9.134:80 abc.com hello!abc.com [root@localhost ~]# curl -x 192.168.9.134:80 www.abc.com hello!abc.com [root@localhost ~]# curl -x 192.168.9.134:80 www.abcde.com hello!abc.com
不管任何域名都指向abc.com,而abc.com是虛擬主機配置的文件中的虛擬主機,並且仍是默認虛擬主機
[root@localhost ~]# curl -x 192.168.9.134:80 www.example.com hello!111.com curl -x 192.168.9.134:80 www.111.com hello!abc.com [root@localhost ~]# curl -x 192.168.9.134:80 111.com hello!111.com
www.example.com指向111.com,而www.111.com沒有指定別名www.111.com,則會指向www.abc.com
虛擬主機配置文件生效,則能夠定義多個虛擬主機
[root@localhost ~]# vi /usr/local/apache2.4/conf/httpd.conf 471 472 # User home directories 473 #Include conf/extra/httpd-userdir.conf 474 475 # Real-time info on requests and configuration 476 #Include conf/extra/httpd-info.conf 477 478 # Virtual hosts 479 Include conf/extra/httpd-vhosts.conf 480 481 # Local access to the Apache HTTP Server Manual 482 #Include conf/extra/httpd-manual.conf 483 484 # Distributed authoring and versioning (WebDAV) 485 #Include conf/extra/httpd-dav.conf 486
而最爲特殊的是默認虛擬主機,任何域名解析到這個主機上則會自動解析虛擬主機