Apache


Apache介紹

Apache HTTP Server(簡稱Apache)是Apache軟件基金會的一個開放源碼的網頁服務器,可以在大多數計算機操作系統中運行,由於其多平台和安全性被廣泛使用,是最流行的Web服務器端軟件之一。它快速、可靠並且可通過簡單的API擴展,將Perl/Python等解釋器編譯到服務器中。

靜態資源與動態資源

靜態資源:靜態內容,客戶端從服務器獲得的資源的表現形式與原文件相同,格式為.html,.css,.jgp等

動態資源:通常是程序文件,需要在服務器執行之后,將執行的結果返回給客戶端, .php .jsp等

長連接與短連接

長鏈接:連接建立,每個資源獲取完成后不會斷開連接,而是繼續等待其他請求的完成

       優點:只需要建立一次連接

        缺點:如果請求資源的時間少於長連接的閾值,那么多余的時間會被浪費

                   如果請求資源的時間大於長連接的閾值,那么有些資源將訪問不到

短鏈接:客戶端和服務器每進行一次HTTP操作,就建立一次連接,任務結束就中斷連接。

     優點:當請求資源時,請求完畢立即斷開

     缺點:當請求資源較多時,那么需要不斷的建立連接,斷開連接,占用資源

優化方案:給長連接設置閾值,設置時間較短的長鏈接

http/1.0默認是使用短鏈接

http/1.1默認是使用長連接

HTTP狀態碼

HTTP狀態碼(英語:HTTP Status Code)是用以表示網頁服務器超文本傳輸協議響應狀態的3位數字代碼。它由 RFC 2616 規范定義的,並得到 RFC 2518、RFC 2817、RFC 2295、RFC 2774 與 RFC 4918 等規范擴展。所有狀態碼的第一個數字代表了響應的五種狀態之一。所示的消息短語是典型的,但是可以提供任何可讀取的替代方案。 除非另有說明,狀態碼是HTTP / 1.1標准(RFC 7231)的一部分。

常見的狀態碼

200:請求已成功,請求所希望的響應頭或數據體將隨此響應返回。
301:請求的URL指向的資源已經被刪除,但在響應報文中通過Location指明了資源現在所處的新位置
302:與301相似,但在響應報文中通過Location指明資源現在所處臨時新位置
304:如果客戶端發送了一個帶條件的 GET 請求且該請求已被允許,而文檔的內容(自上次訪問以來或者根據請求的條件)並沒有改變,則服務器應當返回這個狀態碼
401:需要輸入賬號密碼訪問資源
403:請求被禁止
404:服務器無法找到客戶端請求的資源
500:服務器內部錯誤
502:代理服務器從后端服務器收到一條偽響應

  

安裝apache服務

yum install httpd -y

apache配置文件路徑

/etc/httpd/conf/httpd.conf

apache訪問頁面文件路徑

/var/www/html/index.html

apache日志

apache日志一般分為兩類,1.錯誤日志,2.訪問日志

錯誤日志:一般存放apache所生成的錯誤信息

錯誤日志默認存放路徑

/etc/httpd/logs/error_log

訪問日志:記錄着訪問本網站的客戶端信息,例如ip等

訪問日志默認存放路徑

/etc/httpd/logs/access_log

訪問日志類型

combined和common:復合型和普通型

訪問日志格式

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common

含義:

%h:遠端主機
%I:遠端登錄名
%u:遠程用戶名
%t:時間
%r:請求第一行
%>s:狀態
%b:傳送字節
%{Referer}i\:請求來源
\"%{User-Agent}i\:客戶端瀏覽器提供的瀏覽器識別信息

MPM模塊 

Multipath Process Module:多路處理模塊,Apache 一共有3種穩定的 MPM 模式(多進程處理模塊),它們分別是 prefork、worker、event。2.4版本的httpd默認是prefork工作模式。
而由於event不支持https,因此,企業里面很少使用event

  • prefork模式 (apache默認工作模式)

    工作特點:

      使用多個進程,每個進程只有一個線程,每個進程在某個確定的時間只能維持一個鏈接,優點是穩定,但內存開銷較高

  • worker模式

    工作特點:

      使用多個進程,每個進程包含多個線程,每個線程在某個確定的時間只能維持一個鏈接,內存占用比較小,適合大並發,高流量的web服務器worker缺點是一個線程崩潰,整個進程就會連同其任何線程一起掛掉

  • event模式

    不支持https

修改MPM模塊來修改apache工作模式

查看apache當前工作模式 httpd -V

[root@localhost ~]# httpd -V
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using ::1. Set the 'ServerName' directive globally to suppress this message
Server version: Apache/2.4.6 (CentOS)            #apache當前版本
Server built:   Nov 16 2020 16:18:20             
Server's Module Magic Number: 20120211:24
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     prefork                          #當前工作模式
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/httpd"
 -D SUEXEC_BIN="/usr/sbin/suexec"
 -D DEFAULT_PIDLOG="/run/httpd/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="conf/mime.types"
 -D SERVER_CONFIG_FILE="conf/httpd.conf"
[root@localhost ~]# 

 

修改MPM模塊工作模式   vim  /etc/httpd/conf.modules.d/00-mpm.conf

[root@localhost ~]# vim /etc/httpd/conf.modules.d/00-mpm.conf 
# Select the MPM module which should be used by uncommenting exactly
# one of the following LoadModule lines:

# prefork MPM: Implements a non-threaded, pre-forking web server
# See: http://httpd.apache.org/docs/2.4/mod/prefork.html
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so                 #當前配置文件只有這行沒有注釋,說明這行是生效的 

# worker MPM: Multi-Processing Module implementing a hybrid
# multi-threaded multi-process web server
# See: http://httpd.apache.org/docs/2.4/mod/worker.html
#
#LoadModule mpm_worker_module modules/mod_mpm_worker.so                  #如果切換為worker 模式,將這行注釋打開,把prefork那行注釋掉

# event MPM: A variant of the worker MPM with the goal of consuming
# threads only for connections with active processing
# See: http://httpd.apache.org/docs/2.4/mod/event.html
#
#LoadModule mpm_event_module modules/mod_mpm_event.so

 將配置文件修改后再httpd -V 查看工作模式

[root@localhost ~]# httpd -V
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using ::1. Set the 'ServerName' directive globally to suppress this message
Server version: Apache/2.4.6 (CentOS)
Server built:   Nov 16 2020 16:18:20
Server's Module Magic Number: 20120211:24
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     worker                                #模式已更改為worker
  threaded:     yes (fixed thread count)
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/httpd"
 -D SUEXEC_BIN="/usr/sbin/suexec"
 -D DEFAULT_PIDLOG="/run/httpd/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="conf/mime.types"
 -D SERVER_CONFIG_FILE="conf/httpd.conf"

apache虛擬主機

當一台服務器上只運行一個apache時,當用戶訪問量不大時,會造成資源浪費,為了節省資源可以創建虛擬主機來運行多個apache的web服務,運行多個虛擬主機有三種實現方式

1、基於ip :為每個虛擬主機至少准備一個IP

2、基於端口號:為每個主機至少准備一個端口號

3、基於主機名:為每個主機至少准備一個主機名

創建虛擬主機可以混合使用上述三種任意方式

注意:一般創建虛擬主機時,不能和中心主機混用,使用虛擬主機時禁用中心主機。

禁用中心主機:注釋DocumentRoot

每個虛擬主機都有專門的配置: vim /etc/httpd/conf/httpd.conf 

<VirtualHost "IP:PORT">
    ServerName:
    DocumentRoot
</VirtualHost>

 舉例:vim /etc/httpd/conf/httpd.conf ,在文件最后追加

<VirtualHost 192.168.41.136:80>                    #基於IP創建虛擬主機
    ServerName www.web1.com                        #虛擬主機名稱         
    DocumentRoot "/var/www/web1"                   #網站主站點目錄
    ErrorLog logs/web1-error_log                   #錯誤日志路徑
    CustomLog logs/web1-access_log combiend        #訪問日志路徑
    <Directory "/var/www/html/web1">               #針對哪個目錄做訪問控制       
        Options None
      AllowOverride None
   <RequireAll>                                  #訪問控制權限設置
Require all granted #允許所有人訪問 Require not ip 192.168.1.0/24 #不允許訪問的IP段    </RequireAll>   </Directory> </VirtualHost>

Listen 8080
<VirtualHost *:8080>                               #基於端口號創建虛擬主機
    ServerName www.web2.com                        #虛擬主機名稱         
    DocumentRoot "/var/www/web2"                   #網站主站點目錄
    ErrorLog logs/web2-error_log                   #錯誤日志路徑
    CustomLog logs/web2-access_log combiend        #訪問日志路徑
    <Directory "/var/www/html/web2">               #針對哪個目錄做訪問控制       
        Options None
      AllowOverride None
   <RequireAll>                                  #訪問控制權限設置
Require all granted #允許所有人訪問 Require not ip 192.168.1.0/24 #不允許訪問的IP段    </RequireAll>   </Directory> </VirtualHost>

apache訪問控制

apache有四種訪問控制模式

1、基於站點訪問控制

可基於兩種類型的路徑指明對哪些資源進行訪問控制
        文件系統控制:
            <Directory ""> </Directory>
            <File ""> </File>
            <FileMatch ""> </FileMatch>
        URL路徑:
            <location ""> </location>
            ...

  

2、基於用戶訪問控制

基於用戶訪問控制
        <Directory "/var/test">
            AllowOverride None
            Options None
            AuthType Basic
            AuthName "this is admin page"
            AuthUserFile "/etc/httpd/conf.d/.htpasswd"
            Require user admin1 admin2 admin3
        </Directory>
            創建訪問授權賬號
                -c:自動創建htpasswd文件,因此,僅應該在添加第一個用戶時使用
                -m:md5加密
                -D:刪除用戶
                htpasswd -c -m /etc/httpd/conf.d/.htpasswd admin1
                htpasswd -m /etc/httpd/conf.d/.htpasswd admin1

  

3、基於來源地址訪問控制

Directory中"基於來源地址"實現訪問控制
        (1)Options
            Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews None All
            Indexes: 如果沒有默認主頁面也找不到自定義頁面會顯示索引頁面
        (2)基於來源地址的訪問控制機制
            Require all granted
            Require all denied
            來源地址:
          Require ip IPADDR
          Require not ip IPADDR
                IPADDR:
                    192.168
                    192.168.0.0
                    192.168.0.0/24
                    192.168.0.0/255.255.255.0
示例:
    <Directory "/var/www/admin">
      AllowOverride None
      Options None
      #Require all granted

      Require not ip 192.168.254.0/24
    </Directory>

  

4、基於組訪問控制

 

基於組訪問控制
        <Directory "/var/test">
            AllowOverride None
            Options None
            AuthType Basic
            AuthName "this is admin page"
            AuthUserFile "/etc/httpd/conf.d/.htpasswd"
            AuthGroupFile "/etc/httpd/conf.d/.htgroup"
            Require group webadmins
        </Directory>

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM