天天看點

linux apache2.4 配置虛拟主機

1. 端口監聽

apache的端口監聽設定,是指設定apache這個軟體針對目前伺服器的哪些端口提供服務。通常浏覽器提供的都是web請求,但有些請求不在apache的服務範圍之内(端口不符)

1.1 設定監聽多端口

直接在httpd.conf中添加監聽的端口即可。

#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 80
#添加要監聽的端口
Listen 8080           

2. 主機配置

一個主機,最核心的就是兩件事:

  1. 主機(站點)的名字:ServerName “主機名” 
    • 主機(站點)的實際檔案夾位置:DocumentRoot “站點的實際完整路徑”

apache的作用其實就是一個”轉換”的角色:将目前電腦中的某個檔案夾,對外以某個域名(站點)的方式展現出來。換句話說:站點的本質就是一個檔案夾。

#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn\'t have a registered DNS name, enter its IP address here.
#
#ServerName localhost:8080
ServerName localhost

#
# Deny access to the entirety of your server\'s filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
<Directory />
    AllowOverride none
    Require all denied
</Directory>

#
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something\'s not working as
# you might expect, make sure that you have specifically enabled it
# below.
#

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#修改預設的配置,設定為E:/www為站點根目錄
DocumentRoot "E:/www"
<Directory "E:/www">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn\'t give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #用于顯示設定“可顯示檔案清單”(當無可顯示網頁的時候)
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    # 允許.htaccess檔案覆寫設定
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    # 控制誰能通路這台伺服器,這是apache2.4中的用法
    # 原來版本為
    # Order deny,allow
    # Allow from all

    Require all granted
</Directory>           

當一個請求通過域名解析進入到目前apache并端口配置成功後,apache就會開始”提供服務”。

  1. 在站點設定中找到ServerName項,看是否比對請求中的主機名 
    • 如果找到,則在對應的目錄(DocumentRoot配置項)中找相應的檔案
    • 如果找到,則傳回該檔案(或調用php語言子產品執行後傳回)
    • 如果第2步沒有找到對應請求中的主機名,那麼将第一個主機當作準備提供服務的主機

是以:隻要ip解析和端口能夠進入目前伺服器并比對apache端口設定,則apache一定會提供服務,即使主機名不比對

當修改主機根目錄後通路可能會造成

Forbidden,You don\'t have permission to access / on this server

.這是由于檔案夾的通路是有權限的,初始的站點所對應的檔案夾,安裝的時候已經設定好權限了。現在改了,需要進行設定。

3. 配置檔案夾通路權限

權限中部分設定見2中的代碼内容。以下内容apache可以設定“預設網頁”以提供給使用者

#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
    #這裡是對全局(所有站點,所有檔案夾)都起作用
    DirectoryIndex index.html index.php index.htm
</IfModule>           

        此時,對于沒有指定要通路明确的網頁請求,會按順序從前往後找這些檔案,找到後就在傳回給使用者浏覽器中顯示出來。但是如果沒有找到,此時,Options中的Indexes發揮作用:顯示該檔案夾中所有檔案和檔案夾 

我們可以将DirectoryIndex設定項單獨放在站點檔案夾區域設定中,則隻對該單獨的站點或檔案夾起作用

<Directory "E:/www">
    #用于顯示設定“可顯示檔案清單”(當無可顯示網頁的時候)
    Options Indexes FollowSymLinks

    # 允許.htaccess檔案覆寫設定
    AllowOverride All

    # 控制誰能通路這台伺服器,這是apache2.4中的用法
    # 原來版本為
    # Order deny,allow
    # Allow from all
    Require all granted

    #設定預設顯示頁面,隻對該檔案夾(及其子檔案夾)有效
    DirectoryIndex phpinfo.php
</Directory>           

4. 主機名稱設定

在應用中,通常用一下兩種方式來通路同一個站點:

  http://www.chris.com  http://chris.com

此時,就相當于”2個站點(主機名)”但通路的是同一個内容

這需要使用主機名稱來實作,在多站點設定中會使用到

ServerAlias 别名1 别名2 别名3

當然,如果在本機上通路,記得配置對應的hosts檔案,否則仍然無效

5. 目錄别名設定

目錄别名也叫虛拟目錄,對于實際存在的目錄,可以直接按正常的檔案夾通路層級關系來通路,但是對于不存在的目錄,我們可以通過配置項,做到對外看起來存在一樣:

比如:http://www.study.com/soft/ 站點中不存在該目錄,正常通路會出現

NOT FOUND

,但是可以通過設定讓該位址通路

<IfModule alias_module>
    #Alias /soft "真實路徑"
    Alias /soft "E:/www/study/study/"
</IfModule>           

設定之後會出現

Forbidden

,解決方法和2中一樣。

6. 檔案夾通路控制的檔案控制方式

通常,我們在config配置檔案中,使用Directory配置項,目的是用來控制檔案夾的通路權限。 

但我們也可以使用一個獨立的檔案來控制某檔案夾的通路權限。 

該檔案名必須是: .htaccess

注意:

  1. 隻有字尾和點号(無檔案名部分) 
    • 該檔案必須放在要被控制通路權限的檔案夾中(不同的檔案夾可以放不同的該檔案)
    • 其“上級檔案夾”(通常是Directory設定中的檔案夾)必須使用如下代碼允許.htaccess發揮作用: 

      AllowOverride All

    • .htaccess檔案中出現代碼,幾乎可以跟Directory設定中出現的代碼一樣。
    • 如果.htaccess檔案有效,則其設定會覆寫其上級設定。
    • 此.htaccess檔案修改後可以立即發揮作用,無需重新開機apache

7. 多站點設定

7.1 在http.conf中打開多站點配置子產品

# Virtual hosts
#Include conf/extra/httpd-vhosts.conf           

将其前面的分号去掉改為:

# Virtual hosts
Include conf/extra/httpd-vhosts.conf
如果在httpd.conf中找不到改行,自己添加也行。           

7.2 在httpd-vhosts.conf檔案中修改預設站點根目錄

這是對于apache2.4來說,如果你原先httpd.conf中的根目錄修改了,那麼這裡也要改,因為打開多站點功能後該設定會覆寫httpd.conf檔案中的部分設定。否則可能會出現Forbidden。

#下一行為預設設定,端口預設為80,必須為httpd.conf中你監聽的端口
<VirtualHost _default_:80>
#下一行為預設設定,由于我的預設站點根目錄修改了,是以在這裡也要将它改掉,否則會出現Forbidden
#DocumentRoot "${SRVROOT}/htdocs"
DocumentRoot "E:/www"
#ServerName www.example.com:80
</VirtualHost>           

7.3 配置站點

你可以按照它給的例子

#<VirtualHost *:80>
#    ServerAdmin [email protected]
#    DocumentRoot "${SRVROOT}/docs/dummy-host.example.com"
#    ServerName dummy-host.example.com
#    ServerAlias www.dummy-host.example.com
#    ErrorLog "logs/dummy-host.example.com-error.log"
#    CustomLog "logs/dummy-host.example.com-access.log" common
#</VirtualHost>           

以下是我的設定:

#下一行"*:80"在httpd.conf的配置檔案中必須監聽該端口
<VirtualHost *:80>
    #設定主機名
    ServerName www.study.com
    #設定主機名稱,即用該别名也可以通路(前提是域名解析正确)
    ServerAlias study.com
    #設定該站點根目錄
    DocumentRoot "E:/www/study"
    #設定檔案夾通路控制,其路徑要和上一行的DocumentRoot一樣,
    <Directory "E:/www/study">
        #用于顯示設定“可顯示檔案清單”(當無可顯示網頁的時候)
        Options Indexes
        #啟用檔案夾通路控制的檔案.htaccess設定
        AllowOverride All
        #請求控制
        Require all granted
        #預設打開的頁面設定
        DirectoryIndex index.php index.html
    </Directory>
</VirtualHost>

#···依照上面的方法,可以設定多個站點