一、HTTP软件
1、http服务器应用
http服务器程序
httpd apache
nginx
lighttpd应用程序服务器
IIS .asp
tomcat .jsp
jetty 开源的servlet容器,基于Java的web容器
Resin CAUCHO公司,支持servlets和jsp的引擎
webshpere(IBM), weblogic(BEA), jboss,oc4j(Oracle)市场占有率统计
www.netcraft.
2、httpd介绍
httpd(主程序名字)
20世纪90年代初,国家超级计算机应用中心NCSA开发
1995年开源社区发布apache(名字简称 a patchy server)
ASF: Apache Software Foundation(特意成立的基金会)
FSF:Free Software Foundation(自由软件基金会)特性:
高度模块化:core + modules (模块开发模式,内核加模块)
DSO: Dynamic Shared Object 动态加/卸载
MPM:multi-processing module多路处理模块
3、MPM工作模式
prefork:多进程I/O模型,每个进程响应一个请求,默认模型
一个主进程:生成和回收n个子进程,创建套接字,不响应请求
多个子进程:工作work进程,每个子进程处理一个请求;系统初始时,预先生成多个空闲进程,等待请求,最大不超过1024个worker:复用的多进程I/O模型,多进程多线程,IIS使用此模型
一个主进程:生成m个子进程,每个子进程负责生个n个线程,每个线程响应一个请求,并发响应请求:m*nevent:事件驱动模型(worker模型的变种)
一个主进程:生成m个子进程,每个进程直接响应n个请求,并发响应请求:m*n,有专门的线程来管理这些keep-alive类型的线程,当有真实请求时,将请求传递给服务线程,执行完毕后,又允许释放。这样增强了高并发场景下的请求处理能力
httpd-2.2: event 测试版,centos6默认
httpd-2.4:event 稳定版,centos7默认[root@Centos6-serverhtml]#ulimit-acorefilesize(blocks,-c)0datasegsize(kbytes,-d)unlimitedschedulingpriority(-e)0filesize(blocks,-f)unlimitedpendingsignals(-i)3808maxlockedmemory(kbytes,-l)64maxmemorysize(kbytes,-m)unlimitedopenfiles(-n)1024pipesize(512bytes,-p)8POSIXmessagequeues(bytes,-q)819200real-timepriority(-r)0stacksize(kbytes,-s)10240cputime(seconds,-t)unlimitedmaxuserprocesses(-u)3808virtualmemory(kbytes,-v)unlimitedfilelocks(-x)unlimited
4、进程角色
监听 --> 工作 --> 队列空闲
5、httpd功能特性
虚拟主机(通常一个网站在一个服务器上,但如果网络访问量不大,可以在一个服务器上运行多个网站,就叫虚拟主机)
具体实现通过修改下列三项进行建设虚拟主机IP、Port、FQDN
CGI:Common Gateway Interface,通用网关接口
httpd处理不了的事物可以转发给能处理的机器上去,就是通过通用网关接口完成的
反向代理
根据用户的请求,把用户调度到相应的服务器上
负载均衡
路径别名
丰富的用户认证机制
basic -->不加密认证
digest -->加密认证,但兼容性较差,有部分浏览器不支持支持第三方模块
二、Httpd安装
1、安装方法
版本
CentOS 6: 2.2
CentOS 7: 2.4安装方式:
rpm:centos发行版,稳定,建议使用
编译:定制或特殊需求CentOS 6程序信息:
httpd-2.2
配置文件位置:
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/*.conf
检查配置语法:
httpd –t
service httpd configtest
2、CentOS 6 httpd程序环境
服务脚本(启动脚本):/etc/rc.d/init.d/httpd
脚本配置文件:/etc/sysconfig/httpd服务控制和启动:
chkconfig httpd on|off
service {start|stop|restart|status|configtest|reload} httpd站点网页文档根目录:
/var/www/html模块文件路径:
/etc/httpd/modules
/usr/lib64/httpd/modules主程序文件(MPM模式文件):
/usr/sbin/httpd(默认模式)
/usr/sbin/httpd.worker
/usr/sbin/httpd.event主进程文件:
/etc/httpd/run/httpd.pid日志文件目录:
/var/log/httpd
access_log: 访问日志
error_log:错误日志帮助文档包:
httpd-manual
[root@Centos6-serverhtml]#ll/etc/httpd/total8drwxr-xr-x2rootroot4096Jan2322:56confdrwxr-xr-x2rootroot4096Jan2322:56conf.dlrwxrwxrwx1rootroot19Jan2322:56logs->../../var/log/httpdlrwxrwxrwx1rootroot29Jan2322:56modules->../../usr/lib64/httpd/moduleslrwxrwxrwx1rootroot19Jan2322:56run->../../var/run/httpd[root@Centos6-serverhtml]#httpd-thttpd:apr_sockaddr_info_get()failedforCentos6-serverhttpd:Couldnotreliablydeterminetheserver'sfullyqualifieddomainname,using127.0.0.1forServerNameSyntaxOK[root@Centos6-serverhtml]#cat/etc/httpd/run/httpd.pid26226[root@Centos6-serverhtml]#pstree-pinit(1)─┬─。。。。。├─httpd(26226)─┬─httpd(26245)│├─httpd(26246)│├─httpd(26247)│├─httpd(26248)│├─httpd(26249)│├─httpd(26250)│├─httpd(26251)│└─httpd(26252)。。。。。[root@Centos6-serverhtml]#psaux|grephttpdroot262260.00.31774643856?SsJan230:02/usr/sbin/httpdapache262450.00.31776003112?SJan230:00/usr/sbin/httpdapache262460.00.31776003100?SJan230:00/usr/sbin/httpdapache262470.00.31776003112?SJan230:00/usr/sbin/httpdapache262480.00.31776003080?SJan230:00/usr/sbin/httpdapache262490.00.31776003160?SJan230:00/usr/sbin/httpdapache262500.00.31776003076?SJan230:00/usr/sbin/httpdapache262510.00.31776003076?SJan230:00/usr/sbin/httpdapache262520.00.31776003076?SJan230:00/usr/sbin/httpdroot403870.00.0103332844pts/0S+04:590:00grephttpd[root@Centos6-serverhtml]#yuminstallhttpd-manual[root@Centos6-serverhtml]#servicehttpdrestart#然后就可以登陆http://192.168.1.100/manual/查看帮助文档
三、Httpd 2.2常见配置
1、httpd配置文件的组成
# grep "Section" /etc/httpd/conf/httpd.conf
配置文件的主要内容分成三个部分
### Section 1: Global Environment --->全局环境
### Section 2: 'Main' server configuration --->主服务器设置
### Section 3: Virtual Hosts--->虚拟主机配置格式:directive value --->类似于键值对或者前面是关键字|变量|指令后面是一个值
directive: 不区分字符大小写
value: 为路径时,是否区分大小写,取决于文件系统
[root@Centos6-serverhtml]#grep"Section"/etc/httpd/conf/httpd.conf###Section1:GlobalEnvironment###Section2:'Main'serverconfiguration###Section3:VirtualHosts
2、显示服务器版本信息
ServerTokens Major|Minor|Min[imal]|Prod[uctOnly]|OS|Full
ServerTokens Prod[uctOnly]: Server: Apache
ServerTokens Major: Server: Apache/2
ServerTokens Minor: Server: Apache/2.0
ServerTokens Min[imal]: Server: Apache/2.0.41
ServerTokens OS: Server: Apache/2.0.41 (Unix)
ServerTokens Full (or not specified): Server: Apache/2.0.41 (Unix) PHP/4.2.2 MyMod/1.2
This setting applies to the entire server and cannot be enabled or disabled on a virtualhost-by-virtualhost basis.
#此设置适用于整个服务器,并且不能基于虚拟主机虚拟主机启用或禁用该设置
After version 2.0.44, this directive also controls the information presented by the ServerSignature directive.
#在版本2.0.44之后,该指令还控制由ServerSignature指令提供的信息。
建议使用:ServerTokens Prod
[root@Centos6-serverhtml]#vim/etc/httpd/conf/httpd.confServerTokensProd[root@Centos6-serverhtml]#curl-IHTTP/1.1200OKDate:Tue,23Jan201821:32:02GMTServer:Apache
3、修改监听的IP和Port
Listen [IP:]PORT
(1) 省略IP表示为本机所有IP
(2) Listen指令至少一个,可重复出现多次
Listen 80
Listen 8080示例:
Listen 192.168.1.100:8080 --->8080这个端口,只能用这个ip段访问
Lsten 80
[root@Centos6-serverhtml]#vim/etc/httpd/conf/httpd.confListen172.18.1.100:80[root@Centos6-serverhtml]#curl-Icurl:(7)couldn'tconnecttohost[root@Centos6-serverhtml]#curl-Ihttp://172.18.1.100/test.htmlHTTP/1.1200OKDate:Tue,23Jan201821:38:22GMTServer:ApacheLast-Modified:Tue,23Jan201814:58:06GMTETag:"121db5-a9-56372c63490d1"Accept-Ranges:bytesContent-Length:169Connection:closeContent-Type:text/html;charset=UTF-8
4、持久连接
Persistent Connection:连接建立,每个资源获取完成后不会断开连接,而是继续等待其它的请求完成,默认关闭持久连接
断开条件:数量限制:100
时间限制:以秒为单位, httpd-2.4 支持毫秒级副作用:对并发访问量较大的服务器,持久连接功能会使用有些请求得不到响应
折衷:使用较短的持久连接时间
设置:
KeepAlive On|Off
KeepAliveTimeout 15
MaxKeepAliveRequests 100测试:
telnet WEB_SERVER_IP PORT
GET /URL HTTP/1.1
Host: WEB_SERVER_IP
[root@centos7mini~]#telnet192.168.1.10080--->默认是不支持持久连接的Trying192.168.1.100...Connectedto192.168.1.100.Escapecharacteris'^]'.GET/test.htmlHTTP/1.1HOST:192.168.1.101HTTP/1.1200OKDate:Tue,23Jan201821:53:31GMTServer:Apache。。。</body></html>Connectionclosedbyforeignhost.[root@Centos6-serverhtml]#vim/etc/httpd/conf/httpd.confKeepAliveOnMaxKeepAliveRequests100KeepAliveTimeout150[root@centos7mini~]#telnet192.168.1.10080Trying192.168.1.100...Connectedto192.168.1.100.Escapecharacteris'^]'.GET/test.htmlHTTP/1.1HOST:192.168.1.101HTTP/1.1200OKDate:Tue,23Jan201821:56:19GMT。。。</body></html>GET/test.txtHTTP/1.1HOST:192.168.1.101HTTP/1.1200OKDate:Tue,23Jan201821:56:52GMT。。。</body></html>q
5、MPM( Multi-Processing Module)多路处理模块
prefork, worker, event(试验阶段)
httpd-2.2不支持同时编译多个模块,所以只能编译时选定一个
rpm安装的包提供三个二进制程序文件,分别用于实现对不同MPM机制的支持
确认方法:
ps aux | grep httpd
默认为/usr/sbin/httpd, 即prefork模式查看模块列表
查看静态编译的模块
httpd -l查看静态编译及动态装载的模块
httpd –M动态模块加载:不需重启即生效
动态模块路径
/usr/lib64/httpd/modules/更换使用的httpd程序:
/etc/sysconfig/httpd
HTTPD=/usr/sbin/httpd.worker
重启服务生效
pstree -p|grep httpd 查看进程和线程Httpd 2.4 与之不同
以动态模块方式提供
配置文件:/etc/httpd/conf.modules.d/00-mpm.conf
httpd –M |grep mpm
重启服务生效
pstree -p|grep httpd 查看进程和线程prefork的默认配置:
<IfModule prefork.c>
StartServers8--->默认启动服务的子进程数量
MinSpareServers 5 --->最小的空闲线程
MaxSpareServers 20 --->最大的空闲线程
ServerLimit 256 --->最多进程数,最大20000
MaxClients 256 --->最大并发
MaxRequestsPerChild 4000 --->服务器进程提供的最大请求数
</IfModule>#4000是子进程最多能处理的请求数量。在处理MaxRequestsPerChild 个请求之后,子进程将会被父进程终止,这时候子进程占用的内存就会释放(为0时永远不释放)
worker的默认配置:
<IfModule worker.c>
StartServers 4 --->默认启动服务的子进程数量
MaxClients 300 --->最大访问客户数量(最多支持300个客户端并发访问)
MinSpareThreads 25 --->最小的空闲线程
MaxSpareThreads 75--->最大的空闲线程
ThreadsPerChild 25 --->每个子进程带的线程数量
MaxRequestsPerChild 0 无限制 --->服务器进程提供的最大请求数
</IfModule>
[root@Centos6-serverhtml]#httpd-M--->查看动态模块,可加载的模块httpd:apr_sockaddr_info_get()failedforCentos6-serverhttpd:Couldnotreliablydeterminetheserver'sfullyqualifieddomainname,using127.0.0.1forServerNameLoadedModules:core_module(static)mpm_prefork_module(static)--->默认的就是Prefork模块。。。。。[root@Centos6-serverhtml]#httpd-l--->查看静态模块,编译进去的静态模块Compiledinmodules:core.cprefork.chttp_core.cmod_so.c[root@Centos6-serverhtml]#vim/etc/sysconfig/httpdHTTPD=/usr/sbin/httpd.worker[root@Centos6-serverhtml]#servicehttpdrestart[root@Centos6-serverhtml]#psauxroot414860.00.41777364068?Ss06:100:00/usr/sbin/httpd.workerapache414880.00.55219965348?Sl06:100:00/usr/sbin/httpd.workerapache414890.00.55875325368?Sl06:100:00/usr/sbin/httpd.workerapache414900.00.55219965368?Sl06:100:00/usr/sbin/httpd.workerapache414910.00.55219965352?Sl06:100:00/usr/sbin/httpd.worker。。。。[root@Centos6-serverhtml]#pstree-pinit(1)─┬─NetworkManager(1649)─┬─dhclient(1690)├─httpd.worker(41679)─┬─httpd.worker(41681)─┬─{httpd.worker}(41765)。。。。。。││└─{httpd.worker}(41790)│├─httpd.worker(41682)─┬─{httpd.worker}(41713)││├─{httpd.worker}(41714)。。。。。。││└─{httpd.worker}(41738)│├─httpd.worker(41683)─┬─{httpd.worker}(41689)。。。。。。││└─{httpd.worker}(41792)│└─httpd.worker(41684)─┬─{httpd.worker}(41739)。。。。。。│└─{httpd.worker}(41764)
压力测试r软件包
[root@Centos6-serverhtml]#yuminstallhttpd-tools[root@Centos6-serverhtml]#rpm-qlhttpd-tools/usr/bin/ab/usr/bin/htdbm/usr/bin/htdigest/usr/bin/htpasswd/usr/bin/logresolve[root@Centos6-serverhtml]#ulimit-n10240[root@Centos6-serverhtml]#ulimit-a。。。。。openfiles(-n)10240[root@Centos6-serverhtml]#ab-c2000-n10000--->压力测试\ThisisApacheBench,Version2.3<$Revision:655654$>Copyright1996AdamTwiss,ZeusTechnologyLtd,http://www.zeustech.net/LicensedtoTheApacheSoftwareFoundation,http://www.apache.org/Benchmarking192.168.1.100(bepatient)Completed1000requestsCompleted2000requestsCompleted3000requestsCompleted4000requestsCompleted5000requestsCompleted6000requestsCompleted7000requestsCompleted8000requestsCompleted9000requestsCompleted10000requestsFinished10000requestsServerSoftware:ApacheServerHostname:192.168.1.100ServerPort:80DocumentPath:/test.txtDocumentLength:169bytesConcurrencyLevel:2000Timetakenfortests:5.609secondsCompleterequests:10000Failedrequests:0Writeerrors:0Totaltransferred:4288374bytesHTMLtransferred:1713322bytesRequestspersecond:1782.80[#/sec](mean)Timeperrequest:1121.832[ms](mean)Timeperrequest:0.561[ms](mean,acrossallconcurrentrequests)Transferrate:746.61[Kbytes/sec]receivedConnectionTimes(ms)minmean[+/-sd]medianmaxConnect:0153441.0223082Processing:0414974.1584209Waiting:0401977.3