1.簡介
安裝:yum install -y httpd
主程序:/usr/sbin/httpd
httpd2.4支持MPM的動態切換
配置文件:/etc/httpd/conf /etc/httpd/conf.d/*.conf
模塊相關的配置文件:/etc/httpd/conf.modules.d/*.conf
模塊文件路徑:/usr/lib64/httpd/modules
systemd unit file: /usr/lib/systemd/system/httpd.service
訪問日誌,錯誤日誌,默認目錄:/var/log/httpd (access_log error_log)
網頁文件目錄:/var/www
網頁文件默認目錄:/var/www/html
例如:
https://mp.csdn.net/index.html
scheme://server[:port][/PATH/TO/SOME_SOURCE]
httpd默認 DocumentRoot: /var/www/html
服務控制:
systemctl start httpd.service
systemctl stop httpd.service
systemctl restart httpd.service
2. httpd特性
CGI:Common Gateway Interface
虛擬主機: IP PORT FQDN
反向代理
負載均衡
路徑別名
豐富的用戶認證機制
支持第三方模塊
...
3.常用配置項
/etc/httpd/conf/httpd.conf
格式: 指令 值
其中,指令不區分大小寫,值則有可能區分大小寫
通常以新增配置文件的方式修改httpd配置項,解耦,靈活
0.配置文件中的配置塊:
<Directory />
AllowOverride none
Require all denied
</Directory>
1.監聽端口
Listen [IP-address:]portnumber [protocol]
---地址可省略,端口不能省略
例如:Listen 80
2.長連接
KeepAlive On|Off ------ Enables HTTP persistent connections
KeepAliveTimeout num[ms] ------ Amount of time the server will wait for subsequent requests on a persistent connection
MaxKeepAliveRequests number ------ Number of requests allowed on a persistent connection
3.MPM
httpd -M ----列出所有模塊
在以下文件中配置:/etc/httpd/conf.modules.d/00-mpm.conf
mpm_prefork.conf文件內容如下:
<IfModule mpm_prefork_module>
StartServers 10 # 啓動時進程數
MinSpareServers 5 # 最小空閒進程數
MaxSpareServers 10 # 最大空閒進程數
MaxRequestWorkers 100 # 最大併發進程數
MaxConnectionsPerChild 10000 # 最大連接數限制
</IfModule>
PV(page view): 一次完整的頁面訪問,包括該頁面的所有資源
UV(user view): 獨立的用戶瀏覽量
IP: IP訪問量
4.DSO (Dynamically Shared Objects)
所有使用 httpd -M 顯示爲 (shared) 的模塊都可以使用 LoadModule 裝載
5.定義Main Server的文檔頁面路徑
ServerName [scheme://]domain-name|ip-address[:port]
ServerAlias
DocumentRoot directory-path
---- 指定 URL PATH 與 File System PATH 的映射關係
6.站點訪問控制
可基於兩種機制對資源進行訪問控制
文件系統路徑:
<Directory "">
</Directory>
<File "">
</File>
<FileMatch "PATTERN">
</FileMatch>
URL路徑:
<Location "">
</Location>
<LocationMatch "PATTERN">
</LocationMatch>
基於源地址的訪問控制:
Require all granted
Require all deneid
基於IP控制:
Require ip 10 172.20 192.168.2
Require not ip 10 172.20 192.168.2
基於host控制:
Require host HOST_NAME
Require not host HOST_NAME
要放置在<RequireAll> 或 <RequireAny> 配置塊中
Options:
Indexes
If a URL which maps to a directory is requested, and there is no
DirectoryIndex
(e.g.,index.html
) in that directory, thenmod_autoindex
will return a formatted listing of the directory.如果該目錄沒有index.html,則可以列出該目錄下所有資源文件
FollowSymLinks
The server will follow symbolic links in this directory. This is the default setting.
Even though the server follows the symlink it does not change the pathname used to match against
<Directory>
sections.The
FollowSymLinks
andSymLinksIfOwnerMatch
Options
work only in<Directory>
sections or.htaccess
files.Omitting this option should not be considered a security restriction, since symlink testing is subject to race conditions that make it circumventable.
如果使用該選項,則可以訪問連接文件 連接至 其他目錄或文件
7.定義站點主頁面
DirectoryIndex index.html
8.定義路徑別名
Alias /webpath /full/filesystem/path
即 將 /webpath 這個URL重定向至 文件系統的/full/filesystem/path
注意:別名的路徑 /full/filesystem/path 也需要顯式授權 (Require all granted)
9.設定默認字符集
AddDefaultCharset UTF-8
10.日誌設定
ErrorLog "logs/error_log"
CustomLog "logs/access_log" combined
----相對於ServerRoot的路徑 ( ServerRoot "/etc/httpd"),而該目錄的logs是個軟鏈接:
[root@localhost ~]# ll /etc/httpd/logs
lrwxrwxrwx. 1 root root 19 Dec 21 21:59 /etc/httpd/logs -> ../../var/log/httpd
所以http的log都放在/var/log/httpd下
log的格式由 LogFormat 定義
Common Log Format (CLF)
"%h %l %u %t \"%r\" %>s %b"
Common Log Format with Virtual Host
"%v %h %l %u %t \"%r\" %>s %b"
NCSA extended/combined log format
"%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\""
各種字符的定義:http://httpd.apache.org/docs/2.4/mod/mod_log_config.html
11.基於用戶的訪問控制
認證質詢:
www-authenticate:響應碼401,拒絕客戶端請求,要求賬號密碼
認證:
Authorization:客戶端輸入賬號密碼後再次發送請求報文,認證通過後,服務器發送響應資源
認證有兩種:
basic:明文
digest:消息摘要認證
配置示例:
<Directory "/data/www">
Options None
AllowOverride None
AuthType basic
AuthName "Hello Please Auth"
AuthUserFile "/etc/httpd/conf.d/myAuth.users"
Require user hss tom
#Require valid-user
#AuthGroupFile "/path/to/groupfile"
#Require group groupname
</Directory>
認證文件可使用htpasswd
htpasswd [options] /path/to/authfile username
-c 第一次創建文件時使用,如果文件已存在,則會覆蓋
-m md5格式加密
-s sha格式加密
-D 刪除指定用戶
-b 批量添加用戶 htpasswd -b [options] /path/to/authfile username password
12.虛擬主機
基於IP: ---- 爲每個主機提供至少一個IP
基於PORT: ---- 爲每個主機提供至少一個PORT
基於FQDN: ---- 爲每個主機提供至少一個FQDN,根據請求報文中的host字段路由到不同的虛擬主機
配置示例:
<VirtualHost 192.168.75.10:80> #地址可以寫成*:80
ServerName www.a.com
DocumentRoot "/data/www/a_com"
<Directory "/data/www/a_com">
Options None
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.75.10:80>
ServerName www.b.com
DocumentRoot "/data/www/b_com"
<Directory "/data/www/b_com">
Options None
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
13.status頁面
配置示例:
<Location /url/path/to/status>
SetHandler server-status
Require all granted #注意,此處可做認證
</Location>
URL:
基本語法:
<scheme>://[<user>[:<password>]@]<host>:<port>/<path>[;<params>][?<query>][#<frag>]
params: 參數
http://www.baidu.com/bbs/hello;gender=f
query:
http://www.baidu.com/bbs/item.php?username=tom&title=abc
frag:
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html-single/Installation_Guide/index.html#ch-Boot-x86
14.curl命令
curl [options] [URL...]
curl的常用選項:
-A/--user-agent <string> 設置用戶代理髮送給服務器
-e/--referer <URL> 來源網址
--compressed 要求返回是壓縮的格式
-I/--head 只顯示響應報文首部信息
--basic 使用HTTP基本認證
--tcp-nodelay 使用TCP_NODELAY選項
--cacert <file> CA證書 (SSL)
-H/--header <line>自定義首部信息傳遞給服務器
--limit-rate <rate> 設置傳輸速度
-u/--user <user[:password]>設置服務器的用戶和密碼
-0/--http1.0 使用HTTP 1.0
15.user/group
指定以哪個用戶的身份運行httpd服務進程;
User apache
Group apache
16、使用mod_deflate模塊壓縮頁面優化傳輸速度
該操作節約帶寬,但是消耗更多CPU
SetOutputFilter DEFLATE
# mod_deflate configuration
# Restrict compression to these MIME types
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/css