httpd是Apache超文本傳輸協議(HTTP)服務器的主程序。它被設計為一個獨立運行的後台進程,它會建立一個處理請求的子進程或線程的池對外提供服務。httpd支持基於 虛擬主機,以及基於HOST、IP、PORT實現虛擬主機,反向代理,負載均衡,路徑別名,用戶認證,basic,支持第三方模塊等眾多特性。本文主要描述了Linux下httpd的安裝以及相關基本用法。
演示環境及版本
# cat /etc/issue
CentOS release 6.5 (Final)
Kernel \r on an \m
# uname -r
2.6.32-431.el6.x86_64
查看httpd是否已安裝
# rpm -qa httpd
使用yum列出相關httpd安裝文件,此處為本地yum源
# yum list |grep httpd
httpd.x86_64 2.2.15-29.el6.centos local_server
httpd-devel.i686 2.2.15-29.el6.centos local_server
httpd-devel.x86_64 2.2.15-29.el6.centos local_server
httpd-manual.noarch 2.2.15-29.el6.centos local_server
httpd-tools.x86_64 2.2.15-29.el6.centos local_server
安裝及驗證httpd
# yum -y install httpd
# rpm -qa |grep httpd
httpd-tools-2.2.15-29.el6.centos.x86_64
httpd-2.2.15-29.el6.centos.x86_64
使用rpm方式尋找配置文件
# rpm -qc httpd
常用的配置文件:
/etc/httpd/conf.d/*.conf ###輔助配置文件
/etc/httpd/conf/httpd.conf ###主配置文件
/etc/sysconfig/httpd ###httpd工作模式配置文件
使用rpm方式查看安裝位置及生成的二進制文件
# rpm -ql httpd
主程序:
/usr/sbin/httpd MPM模式默認
/usr/sbin/httpd.event
/usr/sbin/httpd.worker
使用rpm方式查看包的幫助文件
# rpm -qd httpd
啟動腳本:/etc/rc.d/init.d/httpd
日志文件目錄:
/var/log/httpd
access_log:訪問日志
error_log: 錯誤日志
站點文檔目錄:(站點根目錄)
/var/www/html
httpd的工作目錄:/var/www
二、httpd的工作模式
1、MPM: Multipath Processing Module(多路處理模塊)
prefork: 多進程模型,每個進程響應一個請求;穩定性好,但並發能力有限;預先生成多個空閒進程;
由於prefork使用select()系統調用,所以最大並發不能超過1024;
worker:多進程模型,每個進程可生成多個線程,每個線程響應一個請求;預先生成多個空閒線程;
event:一個進程直接響應n個請求;可同時啟動多個進程;
httpd-2.2: 測試使用; ### Author : Leshami
httpd-2.4: 可生產使用; ### Blog : http://blog.csdn.net/leshami
2、幾種工作方式的切換
prefork模式下
# service httpd start
Starting httpd: [ OK ]
# ps -ef|grep httpd |grep -v grep ###一個主進程,生成了8個空閒進程
root 6413 1 0 09:40 ? 00:00:00 /usr/sbin/httpd
apache 6416 6413 0 09:40 ? 00:00:00 /usr/sbin/httpd
apache 6417 6413 0 09:40 ? 00:00:00 /usr/sbin/httpd
apache 6418 6413 0 09:40 ? 00:00:00 /usr/sbin/httpd
apache 6419 6413 0 09:40 ? 00:00:00 /usr/sbin/httpd
apache 6420 6413 0 09:40 ? 00:00:00 /usr/sbin/httpd
apache 6421 6413 0 09:40 ? 00:00:00 /usr/sbin/httpd
apache 6422 6413 0 09:40 ? 00:00:00 /usr/sbin/httpd
apache 6423 6413 0 09:40 ? 00:00:00 /usr/sbin/httpd
# ss -tulpn |grep httpd
tcp LISTEN 0 128 :::80 :::* users:(("httpd",6413,4),("httpd",6416,4),("httpd",6417,4),("httpd",6418,4),
("httpd",6419,4),("httpd",6420,4),("httpd",6421,4),("httpd",6422,4),("httpd",6423,4)
# netstat -nltp|grep 80
tcp 0 0 :::80 :::* LISTEN 6413/httpd
worker工作方式
# cat /etc/sysconfig/httpd|grep -v ^#
HTTPD=/usr/sbin/httpd.worker
# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@orasrv1 ~]# ps -ef|grep httpd|grep -v grep
root 2261 1 0 17:47 ? 00:00:00 /usr/sbin/httpd.worker
apache 2264 2261 0 17:47 ? 00:00:00 /usr/sbin/httpd.worker
apache 2265 2261 0 17:47 ? 00:00:00 /usr/sbin/httpd.worker
apache 2266 2261 0 17:47 ? 00:00:00 /usr/sbin/httpd.worker
event工作方式
注,盡管2.2版本可以設置工作方式為httpd.event,生產環境不建議使用
# cat /etc/sysconfig/httpd|grep -v ^#
HTTPD=/usr/sbin/httpd.event
[root@orasrv1 ~]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@orasrv1 ~]# ps -ef|grep httpd|grep -v grep
root 2402 1 0 17:49 ? 00:00:00 /usr/sbin/httpd.event
apache 2405 2402 0 17:49 ? 00:00:00 /usr/sbin/httpd.event
apache 2406 2402 0 17:49 ? 00:00:00 /usr/sbin/httpd.event
apache 2407 2402 0 17:49 ? 00:00:00 /usr/sbin/httpd.event
三、httpd模塊化特性
高度模塊化
core + modules,
DSO: Dynamic Shared Object
模塊目錄:
/etc/httpd/modules: 符號鏈接文件
/usr/lib64/httpd/modules
模塊的查看
httpd -M ###查看當前httpd進程的所有模塊
httpd.event -M ###查看worker工作模式下的所有模塊
httpd.worker -M ###event工作模式下的所有模塊
httpd.worker -l ###event工作模式下的核心模塊
模塊的查看示例
# httpd -M
Loaded Modules:
core_module (static)
mpm_prefork_module (static)
http_module (static)
so_module (static)
auth_basic_module (shared)
..............
# httpd.event -l
Compiled in modules:
core.c
event.c
http_core.c
mod_so.c
模塊的動態裝載與卸載
# cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak
# cat /etc/httpd/conf/httpd.conf |grep authn_alias_module
LoadModule authn_alias_module modules/mod_authn_alias.so
# vi /etc/httpd/conf/httpd.conf ###注釋mod_authn_alias.so模塊
# cat /etc/httpd/conf/httpd.conf |grep authn_alias_module
#LoadModule authn_alias_module modules/mod_authn_alias.so
# service httpd restart
# httpd -M ###使用該方式前後進行對比即可知道模塊是否裝載或卸載
四、驗證httpd服務
# echo "
orasrv1.xlk.com
" >/var/www/html/index.html # curl http://192.168.21.10
orasrv1.xlk.com