http(1)
開啟httpd服務,注意selinux 和防火墻設置。出現錯誤查看系統日志和http錯誤日志。
進程間通信:IPC socket:套接字 IP:PORT
Client <--> Server Server: listen Client: IP:PORT <--> IP:PORT IANA: 0-1023:永久地分配給固定的應用使用,80/http, 21/ftp, 25/smtp, 110/pop3, 143/imap4;僅root有權限使用,特權端口; 1024-41951:注冊端口,但不嚴格,3306/mysql, 11211/memcached, 41952+:客戶端程序使用的隨機端口,動態端口,或稱為私有端口; /proc/sys/net/ipv4/ip_local_port_range BSD Socket API: SOCK_STREAM, SOCK_DGRAM, SOCK_RAW 地址格式: IPv4: AF_INET IPv6: AF_INET6 Unix_Sock:AF_UNIX
http協議: 應用層協議:超文本傳輸 http/0.9 http/1.0:cache, MIME MIME: multipurpose internet mail extensions http/1.1:緩存功能,條件式請求; speedy: SPDY http/2.0:
html: <html> <head> <title>MageEdu</title> </head> <body> </body> </html> css, js, images, ... major/minor: images/ images/jpeg, images/png, images/gif, ... text/ text/plain, text/html, text/js, ... application/ ... http協議的實現: 開源實現:httpd(apache), nginx, lighttpd, ... C/S: C: browser, user agent, chrome, ie, firefox, safari, opera, ... elinks, curl, wget, ... S:httpd(apache), nginx, lighttpd, ... 通信模型: 請求/響應 stateless, cookie, ... 一次完整的Http請求處理過程: (1) 建立或處理連接請求; (2) 接收請求; (3) 解析請求,處理請求; (4) 加載用戶請求的資源; (5) 構建響應報文; (6) 發送響應報文; (7) 記錄訪問于日志中; web資源: URL: 統一資源定位符; scheme://host[:port]/URL URL的根通常要映射為文件系統上的某路徑; DocumentRoot /var/www/html/ /index.html --> /var/www/html/index.html /admin/index.html --> /var/www/html/admin/index.html /images/logo.jpg --> /var/www/html/images/logo.jpg Alias /images/ /data/imgs/ /images/logo.jpg --> /data/imgs/logo.jpg /images/a/3.gif --> /data/imgs/a/3.gif 事務:request/response request: <method> <url> <version> HEADERS <body> response: <version> <status> <reason-phrase> HEADERS <body> HEADERS: name: value name: value <method>:GET,HEAD,POST, PUT, DELETE, OPTIONS, TRACE, ... <status>: 1xx: 2xx: 成功響應 3xx: 重定向響應 4xx: 客戶端錯誤 5xx: 服務端錯誤
httpd:
http://httpd.apache.org httpd: a patchy server, apache ASF: Apache Software Foundation http://www.netcraft.com httpd特性: 高度模塊化設計:core modules + standard modules + 3rd party modules DSO: Dynamic Shared Object MPM: multipath process modules prefork:process 每進程響應一個請求; worker: thread 每線程響應一個請求; event: thread 每進程響應多個請求; 豐富功能: CGI:動態網站; 虛擬主機:IP,PORT,ServerName 反向代理:http, fcgi, wsgi, ajp, ... 負載均衡: ... 版本: httpd程序版本: httpd-1.x httpd-2.0 httpd-2.2 httpd-2.4 安裝使用httpd: base epel 程序環境: 主程序文件: /usr/sbin/httpd 模塊文件: /usr/lib64/httpd/modules/*.so 主配置文件: /etc/httpd/conf/httpd.conf /etc/httpd/conf.d/*.conf /etc/httpd/conf.modules.d/*.conf 站點文檔路徑: /var/www/html 日志文件路徑: /var/log/httpd/ access_log:訪問日志 error_log:錯誤日志 Unit File: /usr/lib/systemd/system/httpd.service 自帶腳本: /usr/sbin/apachectl systemctl start httpd.service systemctl enable httpd.service ~]# ss -tnlp | grep ":80\>"
回顧: http協議: C/S:
事務:request/response start line HEADERS <BODY> request: <method> <url> < version> HEADERS <BODY> response: <version> <status> <reason-phrase> HEADERS <body> httpd: httpd-2.2 httpd-2.4 MPM: prefork:每進程響應一個請求; worker:每線程響應一個請求; event:每進程響應多個請求;
httpd(2)
httpd-2.4的基礎配置
配置文件: /etc/httpd/conf/httpd.conf /etc/httpd/conf.d/*.conf /etc/httpd/conf.modules.d/*.conf 配置文件: DIRECTIVE VALUE DIRECTIVE: 不區分字符大小寫;例如ServerRoot; VALUE:除了文件系統路徑,大多數也不區分字符大小寫; 配置修改完成后: (1) 測試語法; (2) 讓服務程序重載配置文件: ~]# service httpd reload ~]# systemctl reload httpd.service 注意:不是所有修改都是重載就能生效,又時候需要重啟服務。 不到萬不得已,不要重啟服務。?。。。∮绕涫蔷€上的服務。
常見配置修改
1、監聽的地址和端口 Listen [ip:]port Listen可重復使用多數;默認監聽本地可用的所有地址;
2、保持連接 persistent connection:tcp連接建立后,資源獲取完成之后不會斷開連接,而是繼續等待請求其它資源; 如何斷開? 數量限制 時間限制
KeepAlive On|Off MaxKeepAliveRequests 100 KeepAliveTimeout 10 兩個閾值,限制次數或時間,那個先到期都斷開。默認開啟保持連接 創建文件/etc/httpd/conf.d/keeplive.conf KeepAlive Off 即是關閉該功能。
瀏覽器默認每域名開啟2線程或4線程。所以站點頁面內容量大,加載會慢,所以有網站會使用多個域名。
3、DSO httpd命令: -t -D DUMPMODULES : show all loaded modules -M : a synonym for -t -D DUMPMODULES顯示已經加載的模塊
LoadModule mod_name modules/mod_file_name 相對路徑: 相對于ServerRoot指令的值來說;/etc/httpd/ [root@centos7 ~]# ls /etc/httpd/ -l total 8 drwxr-xr-x. 2 root root 57 Oct 8 10:11 conf drwxr-xr-x. 2 root root 4096 Sep 11 15:14 conf.d drwxr-xr-x. 2 root root 4096 Oct 8 16:43 conf.modules.d lrwxrwxrwx. 1 root root 19 Sep 11 15:12 logs -> ../../var/log/httpd lrwxrwxrwx. 1 root root 29 Sep 11 15:12 modules -> ../../usr/lib64/httpd/modules lrwxrwxrwx. 1 root root 10 Sep 11 15:12 run -> /run/httpd 模塊之間存在依賴關系。關閉模塊時應注意~! [root@centos7 ~]# ls /etc/httpd/conf.modules.d/ 00-base.conf 00-mpm.conf 00-systemd.conf 00-dav.conf 00-proxy.conf 01-cgi.conf 00-lua.conf 00-ssl.conf 10-fcgid.conf 00-base.conf只需要注釋掉相應的模塊,再systemctl reload httpd 即可取消某模塊加載。 或修改配置文件語法 LoadModule mod_name modules/mod_file_name
2.4使用 Include conf.modules.d/*.conf
模塊都配置在上目錄中。當然也可以直接配置在主配置文件中。
[root@centos7 ~]# ll /etc/httpd/conf.modules.d/ total 28 -rw-r--r--. 1 root root 3739 Nov 19 2015 00-base.conf ###基本模塊 -rw-r--r--. 1 root root 139 Nov 19 2015 00-dav.conf -rw-r--r--. 1 root root 41 Nov 19 2015 00-lua.conf -rw-r--r--. 1 root root 742 Nov 19 2015 00-mpm.conf ###mpm配置模塊 -rw-r--r--. 1 root root 957 Nov 19 2015 00-proxy.conf ##代理 -rw-r--r--. 1 root root 88 Nov 19 2015 00-systemd.conf -rw-r--r--. 1 root root 451 Nov 19 2015 01-cgi.conf ###CGI功能模塊
以后有需要可以自己創建配置文件放到該目錄下。
4、定義站點主頁:
DirectoryIndex filename1 filename2 ... 定義主頁文件,可以定義多個。 <IfModule dir_module> DirectoryIndex index.html </IfModule> /var/www/html/ 目錄下或其子目錄下有index.html文件,如果不只當訪問的那個網頁,直接訪問該主頁。
5、Main Server相關配置
(1) DocumentRoot 站點文檔根路徑; DocumentRoot "/var/www/html"
定義了中心服務器的網頁放置那個路徑下。
(2) ServerName 服務器名稱; #ServerName www.example.com:80
默認是被注釋掉的。服務器會反向解析主機地址到主機名。把反解析(hosts文件或DNS)的結果,與本機主機名(hostname)不一致會報錯。防止報錯,可直接修改該參數。取消注釋,修改為正確的主機名。
站點文檔訪問授權及眾多服務特性的配置:
默認httpd要對路徑做授權,否則都不允許訪問。 放置在一個容器中進行控制。 <> … </>
(1)基于文件系統路徑:
<Directory "/PATH/TO/DIR"> ... </Directory> <DirectoryMatch ~ "正則表達式"> ... </DirectoryMatch> <File ""> 定義單個文件的訪問授權 ... </File> ... 還有很多種訪問控制方式。
例如禁止訪問根
<Directory /> AllowOverride none Require all denied </Directory>
(2)基于URL進行:
<Location "URL"> ... </Location> <LocationMatch ~ "URL_PATTERN"> 使用正則表達式 ... </LocationMatch>
(3) Options 定義服務方式
Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews None All
常用選項
Indexes:索引(除非是下載站,否則都是拒絕的) FollowSymLinks:允許跟蹤符號鏈接 網頁文件是鏈接過來的。不在/var/www/html路徑下。(默認是不允許的。) ExecCGI:允許執行CGI腳本
(4) AllowOverride
httpd的訪問控制配置,允許每目錄單獨進行;在每個目錄下建立一個.htaccess文件; AllowOverride表示是否允許目錄中的.htaccess文件中的配置來覆蓋當前<>...</>配置段中的配置; Options FileInfo AuthConfig Limit All None
一般而言,為了性能使用none,否者文件被訪問時,將從起始路徑開始檢查.htaccess文件。
(5) 基于源地址的訪問控制 允許所有地址訪問:Require all granted 拒絕所有地址訪問:Require all denied
<RequireAll> </RequireAll> 基于IP控制: Require ip ADDRESS 允許 Require not ip ADDRESS 禁止 ADDRESS: ip network: 10.1.0.0/255.255.0.0 10.1.0.0/16 10.1 基于主機名控制: Require host HOSTNAME Require not host HOSTNAME HOSTNAME: FQDN DOMAIN.TLD
示例 除了10.0.0.1的所有主機都允許訪問
<RequireAll> Require not ip 10.0.0.1 Require all granted </RequireAll>
違規的主機訪問會記錄到日志。
6、User/Group 定義httpd服務運行身份
User apache Group apache
7、路徑別名
Alias /URL/ /PATH/TO/SOME_DIR/
在Alias命令段加入即可 但是需要給加入的目錄授權,否者不能訪問。2.4需要給目錄顯示授權。
Alias /furmo/ /websit/bbs/ <Directory "/websit/bbs"> AllowOverride none Require all granted Options None </Directory>
修改配置文件需要檢查配置。
8、httpd-manual
# yum -y install httpd-manual 安裝后重載服務 配置文件:conf.d/manual.conf [root@centos7 ~]# cat /etc/httpd/conf. conf.d/ conf.modules.d/ [root@centos7 ~]# cat /etc/httpd/conf.d/manual.conf # # This configuration file allows the manual to be accessed at # http://localhost/manual/ # AliasMatch ^/manual(?:/(?:de|en|fr|ja|ko|ru))?(/.*)?$ "/usr/share/httpd/manual$1" <Directory "/usr/share/httpd/manual"> Options Indexes AllowOverride None Require all granted </Directory> http://host/manual/
注意:這里使用的是別名匹配機制。也是別名。
9、status page 顯示httpd服務當前工作狀態的頁面。是自動生成的。
通過stauts模塊擴展實現的。 status_module httpd -M 查看是否加載了該模塊
創建一個配置文件,/etc/httpd/conf.d/stauts.conf
<Location /status> SetHandler server-status ###使用SetHandler 調用模塊生成頁面。 Require all granted </Location>
基于url要是使用location授權
10、日志設定 兩類日志
錯誤日志: ErrorLog "/var/log/httpd/error_log" LogLevel warn Possible values include: debug, info, notice, warn, error, crit, alert, emerg. 訪問日志: LogFormat "FORMAT_STRINGS" LOG_FORMAT_NAME CustomLog "/PATH/TO/LOG_FILE" LOG_FORMAT_NAME format_strings: %h:Remote hostname. Will log the IP address if HostnameLookups is set to Off, which is the default. %l:Remote logname (from identd, if supplied). 無有效值時,使用“-”表示; %u: Remote user if the request was authenticated. May be bogus if return status (%s) is 401 (unauthorized). http協議認證時由客戶端輸入的用戶名; %t:Time the request was received, in the format [18/Sep/2011:19:18:28 -0400]. %r:First line of request. %>s:Status. For requests that have been internally redirected, this is the status of the original request. Use %>s for the final status. %b:Size of response in bytes, excluding HTTP headers. %{VARNAME}i:記錄由VARNAME所表示的請求報文首部的值,例如%{Referer}i,則表示記錄請求報文中Referer首部的值;
可以定制記錄的日志格式。
11、虛擬主機
主機標識方式: IP不同 ip資源不足,費用大。需要多個公網ip PORT不同 不同端口造成訪問困難。 ServerName: 這個比較常用 在請求報文的首部,有一項內容標識要訪問的主機
配置格式
使用<VirtualHost>容器進行配置。最需要的配置是server name 和document root兩項 <VirtualHost IP:PORT> ServerName DocumentRoot <Directory ""> ... Require all granted </DIrective> ErrorLog CustomLog </VirtualHost> httpd-2.2中使用基于ServerName的虛擬主機時,需要先取消中心主機,要使用專用配置指令 NameVirtualHost IP:PORT 2.4配置中部需要此配置。也不需要取消中心主機,才能啟動虛擬主機。 使用不同方式配置虛擬主機需要注意 ip方式配置,ip不能重復 端口方式配置,端口不能重復 server name方式,主機名不能相同(FQDN) 不同虛擬機的配置盡量放到不同配置文件中。便于管理配置。 注意:可以混合使用,比如前兩個事端口方式配置,最后一個使用ip方式配置。
示例
<VirtualHost 10.1.0.68:80> ServerName www1.magedu.com DocumentRoot "/vhosts/www1" <Directory "/vhosts/www1"> Options None AllowOverride None Require all granted </Directory> </VirtualHost> <VirtualHost 10.1.0.68:8080> ServerName www2.magedu.com DocumentRoot "/vhosts/www2" <Directory "/vhosts/www2"> Options None AllowOverride None Require all granted </Directory> </VirtualHost> <VirtualHost 10.1.0.69:80> ServerName www3.magedu.com DocumentRoot "/vhosts/www3" <Directory "/vhosts/www3"> Options None AllowOverride None Require all granted </Directory> </VirtualHost>
如果全部使用servername配置虛擬主機
測試時使用域名測試,如果使用本機測試,修改本機的hosts文件,使用links訪問三個域名即可實現三個虛擬主機訪問。
測試結果
[root@centos7 ~]# curl http://www1.magedu.com w1 [root@centos7 ~]# curl http://www2.magedu.com w2 [root@centos7 ~]# curl http://www3.magedu.com w3 [root@centos7 ~]#
虛擬機配置文件
oot@centos7 ~]# vim /etc/httpd/conf.d/virtualhost.conf ▽ <VirtualHost 192.168.136.129:80> ServerName www1.magedu.com DocumentRoot "/websit/www1" <Directory "/websit/www1"> Options None AllowOverride None Require all granted </Directory> </VirtualHost> <VirtualHost 192.168.136.129:80> ServerName www2.magedu.com DocumentRoot "/websit/www2" <Directory "/websit/www2"> Options None AllowOverride None Require all granted </Directory> </VirtualHost> <VirtualHost 192.168.136.129:80> ServerName www3.magedu.com DocumentRoot "/websit/www3" <Directory "/websit/www3"> Options None AllowOverride None Require all granted </Directory> </VirtualHost>
虛擬主機默認訪問第一個虛擬主機(如果沒有指定訪問那個具體的主機) 也可以定義一個默認虛擬主機
<VirtualHost default:80>
12、基于用戶的訪問控制
Require user USERLIST Require group GRPLIST 虛擬用戶: 確定訪問服務器某個服務的用戶身份,用戶為虛擬用戶。 認證方式: basic digest 需要瀏覽器支持 http協議認證過程 : 認證質詢: WWW-Authencate:響應碼為401,拒絕客戶端請求,并說明用戶需要輸入正確的賬號和密碼之后方可訪問; 認證: Authorization:客戶端填入賬號和密碼,再次發送請求報文;認證通過,服務器發送響應內容; <Directory ""> Options None AllowOverride None AuthType Basic AuthName "STRING" AuthUserFile "" Require user USER1 USER2 ... </Directory> 也可以使用Require valid-user 文件中全部用戶 賬號文件生成工具htpasswd htpasswd [options] "/PATH/TO/HT_PASSWD_FILE" username -c:創建此文件; -m:md5加密密碼存放; -s:sha加密 -D: 刪除指定用戶 [root@centos7 ~]# cat /etc/httpd/conf/.www1passwd tom:$apr1$dWA1eyPX$BCWKMy3K2KAdvThSMNk/a/ jerry:$apr1$fUXrpG9t$8Aj7wNwAnSXzj7gaONa0M1 [root@centos7 ~]# tree /websit/www1/ /websit/www1/ ├── admin │ └── index.html └── index.html <VirtualHost *:80> ServerName www1.magedu.com DocumentRoot "/vhosts/www1" <Directory "/vhosts/www1"> Options None AllowOverride None Require all granted </Directory> <Directory "/vhosts/www1/admin"> Options None AllowOverride None AuthType Basic AuthName "Admin Area, Enter your name/pass" AuthUserFile "/etc/httpd/conf/.htpasswd" Require valid-user </Directory> ErrorLog "logs/www1-error_log" CustomLog "logs/www1-access_log" combined </VirtualHost> Require的使用方式: (1) Require valid-user (2) Require user USER1 USER2 ... 指定用戶 基于組進行認證: <Directory "/vhosts/www1/admin"> Options None AllowOverride None AuthType Basic AuthName "Admin Area, Enter your name/pass" ###該內容隨便寫 (會在將來提示輸入用戶名和密碼的窗口上顯示) AuthUserFile "/etc/httpd/conf/.htpasswd" AuthGroupFile "/etc/httpd/conf/.htgroup" Require group GRPNAME1 GRPNAME 2 </Directory> 創建組賬號文件: 每行定義一個組 group_name: user1 user2 ...
任何要進程要讀取數據,就要與之對應的數據載體(方式)有對應的交互接口。 文件系統–系統調用 mysql–要能讀取mysql套接字,支持mysql協議。
獲取任何數據,就要有與之對應的實現,庫,協議,驅動…
練習:(1) 基于主機名實現三個虛擬主機 (2) 每虛擬主機使用獨立的訪問日志和錯誤日志 (3) 第三個虛擬主機的/admin要進行用戶訪問認證 (4) 在第二個虛擬主機上提供/status; (5) 在第三個虛擬主機提供路徑別名/bbs,訪問其它文件系統路徑; (6) 嘗試使用混合類型的虛擬主機: 基于IP,PORT和ServerName 練習2:使用腳本實現基于主機名的虛擬主機按需創建; 每虛擬使用單獨的配置文件; 腳本可接受參數,參數虛擬主機名稱;
是否創建虛擬主機 是 輸入主機名 檢查/etc/httpd/conf.d/目錄下是否有重名的。
指定主機doucumentroot 是否使用認證 是 創建用戶名和密碼文件
自動創建目錄并授權配置認證
是否創建路勁別名 是 提示輸入別名 輸入目標路徑
創建配置文件到/etc/httpd/conf.d/NAME-vhost.conf
創建腳本文件。創建配置文件
原創文章,作者:yyw,如若轉載,請注明出處:http://www.www58058.com/56556