歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Linux下配置Apache httpd

Linux下配置Apache httpd

日期:2017/3/1 12:20:31   编辑:關於Linux

httpd是Apache超文本傳輸協議(HTTP)服務器的主程序。它被設計為一個獨立運行的後台進程,它會建立一個處理請求的子進程或線程的池對外提供服務。httpd支持基於 虛擬主機,以及基於HOST、IP、PORT實現虛擬主機,反向代理,負載均衡,路徑別名,用戶認證,basic,支持第三方模塊等眾多特性。本文主要描述了Linux下httpd(2.2版本)的相關基本配置。

1、配置文件概述

配置文件,主要分為3個部分:
        # grep "Section" /etc/httpd/conf/httpd.conf
        ### Section 1: Global Environment(全局環境配置)
        ### Section 2: 'Main' server configuration (核心服務器配置)
        ### Section 3: Virtual Hosts(虛擬主機配置,即建多個站點)

        注意:Main Server和Virtual Hosts不同時使用;默認啟用的是Main Server;

    配置文件的語法 
        指令 值
            指令:不區分字符大小寫
            值:區分字符大小寫

    校驗配置文件      
        httpd -t: 檢查配置文件語法
        service httpd configtest        

    當前配置使用的版本
    # /usr/sbin/httpd -v
        Server version: Apache/2.2.15 (Unix)
        Server built:   Aug 13 2013 17:29:28

2、關閉歡迎頁面

/etc/httpd/conf.d/welcome.conf:重命名(不以.conf結尾)或刪除

3、定義ServerRoot

    ServerRoot "/etc/httpd"
        定義httpd頂級目錄,用於存放配置,錯誤,日志文件等
        目錄尾部不要使用斜槓

4、指定監聽的地址和端口

    Listen [IP:]PORT
    注意:Listen可以出現多次

5、定義默認的主頁面

    DirectoryIndex index.html index.html.var  index.php

6、設定默認字符集

        AddDefaultCharset UTF-8
        常用字符集:UTF-8, GBK, GB2312, GB18030

7、配置持久連接

    KeepAlive Off|On            持久鏈接是開啟還是關閉
    MaxKeepAliveRequests 100    一次長連接最大能訪問多少個資源,達到後會斷開
    KeepAliveTimeout 15         一次長連接的超時時長

8、模塊動態裝卸載

    LoadModule foo_module modules/mod_foo.so

    相對於ServerRoot參數所指定的路徑;
        ServerRoot /etc/httpd

    可以參考:   [Linux下安裝Apache httpd](http://blog.csdn.net/leshami/article/details/49906229) 

9、工作模式參數配置

            (prefork工作模式)
    StartServers       8            (啟動8個空閒進程)
    MinSpareServers    5            (最少空閒進程為5)
    MaxSpareServers   20            (最大空閒進程為20)
    ServerLimit      256            (最多客戶端數)
    MaxClients       256            (最多客戶端數)
    MaxRequestsPerChild  4000       (每個子進程最大處理多少個請求)
    

                 (work工作模式)
    StartServers         4          (啟動進程數)
    MaxClients         300           
    MinSpareThreads     25
    MaxSpareThreads     75
    ThreadsPerChild     25          (每個進程最多啟動多少個線程)
    MaxRequestsPerChild  0
    

    注意:修改了裝載的模塊後,reload即可生效;

10、指定Main Server的docroot,用於設定URL的根路徑,即與服務器上文件路徑的映射關系

DocumentRoot "/var/www/html"

    例如:/u01/web     此處DocumentRoot為/u01/web
        文件系統路徑:/u01/web/bbs/upload/a.rar
        則URL路徑為:http://Server_IP/bbs/upload/a.rar

    # mkdir -p /u01/web
    # echo "This is a new site location" > /u01/web/index.html
    # vi /etc/httpd/conf/httpd.conf    ###修改為/u01/web
    # service httpd reload
    # curl http://192.168.21.10
    This is a new site location

11、站點路徑訪問控制

基於本地文件系統路徑
        
            Options Indexes FollowSymLinks
            AllowOverride None
            ....
        

    基於URL
        

        

12、Directory容器中的訪問控制定義

(a) Options  (頁面如何展示給用戶看)
        Indexes: 當訪問的路徑下無默認的主頁面時,將所有資源以列表形式呈現給用戶;危險,慎用;
        FollowSysLinks:跟隨符號鏈接指向的原文件(即能否訪問鏈接文件);

        上述選項,如果要去掉或者說關閉某項功能,則使用符號“-”,如下示例:
        Options -Indexes FollowSymLinks

        示例,關閉Indexes功能
            # grep "\-Indexes" /etc/httpd/conf/httpd.conf
            Options -Indexes FollowSymLinks
            # service httpd reload
            Reloading httpd: 
            [root@orasrv1 ~]# curl http://192.168.21.10
            
            

###收到403,沒有訪問許可

Forbidden

You don't have permission to access / on this server.

Apache/2.2.15 (CentOS) Server at 192.168.21.10 Port 80 (b) 基於IP的訪問控制 Order allow,deny ###定義順序 Allow from all ###允許所有主機訪問 from後面能接受的地址格式:IP, Network Address 網絡地址格式: 172.16 172.16.0.0 172.16.0.0/16 172.16.0.0/255.255.0.0 示例: Order allow,deny Deny from 172.16.100.77 Allow from 172.16

13、內置的status頁面

         ###是一個內置頁,用於展示服務器性能
            SetHandler server-status  ###(處理器:是一個小程序)
            Order deny,allow
            Deny from all
            Allow from 192.168
     

    ExtendedStatus On 可以通過配置該指令,獲取更多的統計信息

    示例:
        # curl http://192.168.21.10/server-status
        
          ...........
        
7957 in state: _ , 7958 in state: W , 7959 in state: _ 7960 in state: _ , 7961 in state: _ , 7962 in state: _ 7963 in state: _ , 7964 in state: _ , 

............

14、虛擬主機

一個物理服務器(虛擬機)服務於多個站點:每個站點通過一個虛擬主機來實現;
    httpd支持三種類型的虛擬主機:
        基於IP
        基於Port
        基於Host

    注意:禁用Main Server; 注釋DocumentRoot指令即可;即虛擬主機與Main Server不兼容

    定義虛擬主機:
        
            ServerName
            DocumentRoot 
            ServerAlias
            

            
            ErrorLog  
            CustomLog 
        

    示例1:基於IP,假定如下配置,當前主機上已配置2個IP
        
            ServerName websrv.ycdata.net
            DocumentRoot "/Vhosts/websrv.ycdata.net"
        
        
            ServerName bbs.ycdata.net
            DocumentRoot "/Vhosts/bbs.ycdata.net"
          

        注,虛擬主機監聽的端口必須和Listen監聽的一樣
        # mkdir -p /Vhosts/websrv.ycdata.net
        # mkdir -p /Vhosts/bbs.ycdata.net
        # echo "

you are visiting websrv.ycdata.net

">/Vhosts/websrv.ycdata.net/index.html # echo "

you are visiting bbs.ycdata.net

">/Vhosts/bbs.ycdata.net/index.html # httpd -t # service httpd reload # curl http://192.168.21.10/

you are visiting websrv.ycdata.net

# curl http://192.168.144.128/

you are visiting bbs.ycdata.net

示例2:基於Port,假定如下配置 Listen 8080 Listen 8081 ServerName websrv.ycdata.net DocumentRoot "/Vhosts/websrv.ycdata.net" ServerName bbs.ycdata.net DocumentRoot "/Vhosts/bbs.ycdata.net" # httpd -t # service httpd reload # curl http://192.168.21.10:8080

you are visiting websrv.ycdata.net

# curl http://192.168.21.10:8081

you are visiting bbs.ycdata.net

示例3:基於Host NameVirtualHost 192.168.21.10:80 ServerName websrv.ycdata.net DocumentRoot "/Vhosts/websrv.ycdata.net" ServerName bbs.ycdata.net DocumentRoot "/Vhosts/bbs.ycdata.net" 修改windows客戶端hosts如下 C:\Users\1636>type C:\Windows\System32\drivers\etc\hosts 192.168.21.10 websrv.ycdata.net 192.168.21.10 bbs.ycdata.net # httpd -t # service httpd reload # 基於Windows端測試,截圖略

15、配置日志功能

指令集位置,級別定義
        ErrorLog logs/error_log:定義錯誤日志文件路徑;會被虛擬機主機繼承;也可以基於虛擬之際定義日志
        LogLevel warn
            支持這些級別:debug, info, notice, warn, error, crit, alert, emerg.

    定義日志格式
        LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

        %h  Remote host
        %l  Remote logname (from identd, if supplied)
        %u  Remote user (from auth; may be bogus if return status (%s) is 401)
        %t  Time the request was received (standard english format)
        %r  First line of request
            method  url  version
        %s  Status. For requests that got internally redirected, this is the status
            of the *original* request --- %>s for the last.
        %b  Size of response in bytes, excluding HTTP headers. In CLF format, 
            i.e. a '-' rather than a 0 when no bytes are sent.
        %{Foobar}i  The contents of Foobar: header line(s) in the request sent to the server.
            %{referer}i: 跳轉至當前頁面之前來源的上一次所在的頁面;
            %{User-Agent}i:用戶代理;

        詳情請參考:http://httpd.apache.org/docs/2.2/mod/mod_log_config.html#formats

    定義訪問日志,如下示例尾部使用了combined,則會使用我們自定義的日志格式
        CustomLog logs/access_log  combined
        指令        日志文件       日志格式名稱

16、與配置相關的一些命令

httpd程序自帶的工具程序:
        httpd: apache的服務程序
            -t: 配置文件語法測試
            -M: 列出所有已經裝載的模塊
            -l: 列出所有的靜態模塊
            -S:列出所有的虛擬主機

        如,列出當前定義的虛擬主機
            # httpd -S
            VirtualHost configuration:
            192.168.21.10:80       is a NameVirtualHost
                     default server websrv.ycdata.net (/etc/httpd/conf/httpd.conf:1022)
                     port 80 namevhost websrv.ycdata.net (/etc/httpd/conf/httpd.conf:1022)
                     port 80 namevhost bbs.ycdata.net (/etc/httpd/conf/httpd.conf:1026)
            Syntax OK

        apachectl: shell腳本,httpd服務控制

        apxs: httpd得以擴展使用第三方模塊的工具接口;

        rotatelogs: 不關閉httpd而切換其使用到的日志文件
            access_log, access_log.1, access_log.2

Copyright © Linux教程網 All Rights Reserved