可以理解为:一种单线程,一种多线程的两种模式
备注:使用另外一个终端去测试,这样得到的效果最准确
most 共享模块
ab 【options】 网站网址
-n 请求数量
-c 并发次数
-t
-v
document 站点路径
[root@localhost ~]# which ab
/usr/bin/which: no ab in (/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin)
没有安装httpd
[root@localhost ~]# yum install gcc gcc-c++ perl zlib-devel pcre* make expat-devel -y
[root@localhost ~]# mount.cifs //192.168.254.10/linuxs /mnt
[root@localhost ~]# cd /mnt/LAMP-C7/
[root@localhost LAMP-C7]# tar zxvf apr-1.6.2.tar.gz -C /opt
[root@localhost LAMP-C7]# tar zxvf apr-util-1.6.0.tar.gz -C /opt
[root@localhost LAMP-C7]# tar jxvf httpd-2.4.29.tar.bz2 -C /opt
[root@localhost LAMP-C7]# cd /opt
[root@localhost opt]# ls
apr-1.6.2 apr-util-1.6.0 httpd-2.4.29 rh
[root@localhost opt]# mv apr-1.6.2/ httpd-2.4.29/srclib/apr
[root@localhost opt]# mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util
[root@localhost httpd-2.4.29]# ./configure --prefix=/usr/local/httpd --enable-so --enable-charset-lite --enable-rewrite --enable-cgi --enable-deflate
configure: summary of build options:
Server Version: 2.4.29
Install prefix: /usr/local/httpd
C compiler: gcc -std=gnu99
CFLAGS: -g -O2 -pthread
CPPFLAGS: -DLINUX -D_REENTRANT -D_GNU_SOURCE
LDFLAGS:
LIBS:
C preprocessor: gcc -E
[root@localhost httpd-2.4.29]# make && make install
[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/bin/* /usr/local/bin
[root@localhost /]# which ab
/usr/local/bin/ab
[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/httpd.conf
[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[root@localhost httpd-2.4.29]# chkconfig --add httpd
[root@localhost /]# chkconfig --level 35 httpd on
开启服务
[root@localhost /]# vim /etc/httpd.conf
51 Listen 192.168.247.149:80
52 #Listen 80
198 ServerName www.kgc.com:80
[root@localhost /]# systemctl stop firewalld.service
[root@localhost /]# setenforce 0
[root@localhost /]# systemctl start httpd
[root@localhost /]# netstat -natp | grep "httpd"
tcp 0 0 192.168.247.149:80 0.0.0.0:* LISTEN 72053/httpd
[root@localhost /]# echo "192.168.247.149 www.kgc.com" >> /etc/hosts
[root@localhost /]# ping www.kgc.com
PING www.kgc.com (192.168.247.149) 56(84) bytes of data.
64 bytes from www.kgc.com (192.168.247.149): icmp_seq=1 ttl=64 time=0.123 ms
64 bytes from www.kgc.com (192.168.247.149): icmp_seq=2 ttl=64 time=0.026 ms
64 bytes from www.kgc.com (192.168.247.149): icmp_seq=3 ttl=64 time=0.029 ms
^C
--- www.kgc.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.026/0.059/0.123/0.045 ms
在网页里多加几张图片
[root@localhost /]# cd /usr/local/httpd/htdocs/
[root@localhost htdocs]# ls
index.html
[root@localhost htdocs]# vim index.html
It works!
-n 指定访问量 -c 指定并发量
[root@localhost htdocs]# ab -n 2000 -c 1000 www.kgc.com/index.html
This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.kgc.com (be patient)
Completed 200 requests
Completed 400 requests
Completed 600 requests
Completed 800 requests
Completed 1000 requests
Completed 1200 requests
Completed 1400 requests
Completed 1600 requests
Completed 1800 requests
Completed 2000 requests
Finished 2000 requests
Server Software: Apache/2.4.29
Server Hostname: www.kgc.com
Server Port: 80
Document Path: /index.html
Document Length: 706 bytes
Concurrency Level: 1000
Time taken for tests: 4.471 seconds
Complete requests: 2000
Failed requests: 0
Total transferred: 1904000 bytes
HTML transferred: 1412000 bytes
Requests per second: 447.28 [#/sec] (mean)
Time per request: 2235.730 [ms] (mean)
Time per request: 2.236 [ms] (mean, across all concurrent requests)
Transfer rate: 415.83 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 49 49.2 34 231
Processing: 25 384 772.5 120 3476
Waiting: 0 376 773.8 113 3476
Total: 41 433 770.1 170 3484
Percentage of the requests served within a certain time (ms)
50% 170
66% 224
75% 309
80% 460
90% 656
95% 2879
98% 3459
99% 3482
100% 3484 (longest request)
[root@localhost htdocs]#
多测试几次
Time taken for tests: 4.471 seconds
Time taken for tests: 20.758 seconds
Time taken for tests: 20.564 seconds
Time taken for tests: 21.045 seconds
Time taken for tests: 20.653 seconds
Time taken for tests: 20.485 seconds
Time taken for tests: 21.584 seconds
Time taken for tests: 20.563 seconds
Time taken for tests: 21.055 seconds
Time taken for tests: 21.057 seconds
Time taken for tests: 0.878 seconds
Time taken for tests: 21.053 seconds
[root@localhost htdocs]# vim /etc/httpd.conf
LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so
LoadModule filter_module modules/mod_filter.so
//文章末尾增加压缩参数
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/j
avascript text/jpg text/png
DeflateCompressionLevel 6
SetOutputFilter DEFLATE
[root@localhost htdocs]# netstat -natp | grep "httpd"
tcp 0 0 192.168.247.149:80 0.0.0.0:* LISTEN 72055/httpd
重启服务,开始压测
[root@localhost htdocs]# service httpd stop
[root@localhost htdocs]# service httpd start
优化后
[root@localhost htdocs]# ab -n 2000 -c 1000 www.kgc.com/index.html
Time taken for tests: 23.457 seconds
Time taken for tests: 21.042 seconds
Time taken for tests: 21.061 seconds
Time taken for tests: 20.489 seconds
Time taken for tests: 21.055 seconds
Time taken for tests: 0.559 seconds
Time taken for tests: 1.102 seconds
Time taken for tests: 1.007 seconds
Time taken for tests: 1.270 seconds
效果不太明显
查看80端口当前启用的线程,
[root@localhost htdocs]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 74866 root 3u IPv4 190231 0t0 TCP www.kgc.com:http (LISTEN)
httpd 74868 daemon 3u IPv4 190231 0t0 TCP www.kgc.com:http (LISTEN)
httpd 74869 daemon 3u IPv4 190231 0t0 TCP www.kgc.com:http (LISTEN)
httpd 74870 daemon 3u IPv4 190231 0t0 TCP www.kgc.com:http (LISTEN)
httpd 74958 daemon 3u IPv4 190231 0t0 TCP www.kgc.com:http (LISTEN)
httpd 74986 daemon 3u IPv4 190231 0t0 TCP www.kgc.com:http (LISTEN)
httpd 74987 daemon 3u IPv4 190231 0t0 TCP www.kgc.com:http (LISTEN)
httpd 75042 daemon 3u IPv4 190231 0t0 TCP www.kgc.com:http (LISTEN)
httpd 75043 daemon 3u IPv4 190231 0t0 TCP www.kgc.com:http (LISTEN)
httpd 75044 daemon 3u IPv4 190231 0t0 TCP www.kgc.com:http (LISTEN)
httpd 75045 daemon 3u IPv4 190231 0t0 TCP www.kgc.com:http (LISTEN)
httpd 75154 daemon 3u IPv4 190231 0t0 TCP www.kgc.com:http (LISTEN)
httpd 75155 daemon 3u IPv4 190231 0t0 TCP www.kgc.com:http (LISTEN)
httpd 75156 daemon 3u IPv4 190231 0t0 TCP www.kgc.com:http (LISTEN)
httpd 75157 daemon 3u IPv4 190231 0t0 TCP www.kgc.com:http (LISTEN)
httpd 75158 daemon 3u IPv4 190231 0t0 TCP www.kgc.com:http (LISTEN)
httpd 75159 daemon 3u IPv4 190231 0t0 TCP www.kgc.com:http (LISTEN)
[root@localhost htdocs]# vim /etc/httpd.conf
开启工作模式
463 Include conf/extra/httpd-mpm.conf
//切换到子配置文件中
[root@localhost htdocs]# cd /usr/local/httpd/conf/extra/
[root@localhost extra]# ls
httpd-autoindex.conf httpd-languages.conf httpd-ssl.conf
httpd-dav.conf httpd-manual.conf httpd-userdir.conf
httpd-default.conf httpd-mpm.conf httpd-vhosts.conf
httpd-info.conf httpd-multilang-errordoc.conf proxy-html.conf
[root@localhost extra]# httpd -l
Compiled in modules:
core.c
mod_so.c
http_core.c
event.c
[root@localhost extra]# vim httpd-mpm.conf
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 250
MaxConnectionsPerChild 0
StartServers 3
MinSpareThreads 75
MaxSpareThreads 250
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 0
StartServers 3
MinSpareThreads 75
MaxSpareThreads 250
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 0
//修改prefork参数
StartServers 5
MinSpareServers 2
MaxSpareServers 5
MaxRequestWorkers 250
MaxConnectionsPerChild 0
[root@localhost extra]# service httpd stop
[root@localhost extra]# service httpd start
出现三个子线程
[root@localhost extra]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 76666 root 3u IPv4 251071 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 76670 daemon 3u IPv4 251071 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 76671 daemon 3u IPv4 251071 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 76672 daemon 3u IPv4 251071 0t0 TCP localhost.localdomain:http (LISTEN)
//关闭服务重新查看
[root@localhost extra]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 76944 root 3u IPv4 252394 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 76946 daemon 3u IPv4 252394 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 76947 daemon 3u IPv4 252394 0t0 TCP localhost.localdomain:http (LISTEN)
Apache的prefork模式优缺点
优点:成熟,兼容所有新老模块。进程之间完全独立,使得它非常稳定。同时,不需要担心线程安全的问题。(我们常用的mod_php,PHP的拓展不需要支持线程安全)
缺点:一个进程相对占用更多的系统资源,消耗更多的内存。而且,它并不擅长处理高并发请求,在这种场景下,它会将请求放进队列中,一直等到有可用进程,请求才会被处理。
1、Apache的worker模式工作原理
worker模式和prefork模式相比,worker模式使用了多进程和多线程的混合模式,worker模式也同样会先预派生一些子进程,然后每个子进程创建一些线程,同时包括一个监听线程,每个请求过来会被分配到一个线程来服务。线程比起进程会更轻量,因为线程是通过共享父进程的内存空间,因此,内存的占用会减少一些,在高并发的场景下会比prefork有更多可用的线程,表现会更优秀一些。
另外,如果一个线程出现了问题也会导致同一进程下的线程出现问题,如果是多个线程出现问题,也只是影响Apache的一部分,而不是全部。由于用到多进程多线程,需要考虑到线程的安全了,在使用keep-alive长连接的时候,某个线程会一直被占用,即使中间没有请求,需要等待到超时才会被释放(该问题在prefork模式下也存在)。
Apache总是试图维持一个备用(spare)或是空闲的服务线程池。这样,客户端无须等待新线程或新进程的建立即可得到处理。在Unix中,为了能够绑定80端口,父进程一般都是以root身份启动,随后,Apache以较低权限的用户建立子进程和线程。User和Group指令用于配置Apache子进程的权限。虽然子进程必须对其提供的内容拥有读权限,但应该尽可能给予他较少的特权。另外,除非使用了suexec ,否则,这些指令配置的权限将被CGI脚本所继承。
Apache的worker模式优缺点
优点:占据更少的内存,高并发下表现更优秀。
缺点:必须考虑线程安全的问题,因为多个子线程是共享父进程的内存地址的。如果使用keep-alive的长连接方式,也许中间几乎没有请求,这时就会发生阻塞,线程被挂起,需要一直等待到超时才会被释放。如果过多的线程,被这样占据,也会导致在高并发场景下的无服务线程可用。(该问题在prefork模式下,同样会发生)。
说明:http1.1的keep-alive的长连接方式,是为了让下一次的socket通信复用之前创建的连接,从而,减少连接的创建和销毁的系统开销。保持连接,会让某个进程或者线程一直处于等待状态,即使没有数据过来。
Apache的event模式工作原理
event是Apache最新的工作模式,它和worker模式很像,不同的是在于它解决了keep-alive长连接的时候占用线程资源被浪费的问题(某些线程因为被keep-alive,挂在那里等待,中间几乎没有请求过来,一直等到超时)。
在event工作模式中,会有一些专门的线程用来管理这些keep-alive类型的线程,当有真实请求过来的时候,将请求传递给服务器的线程,执行完毕后,又允许它释放。这样,一个线程就能处理几个请求了,实现了异步非阻塞。这增强了在高并发场景下的请求处理。
event工作模式在遇到某些不兼容的模块时,会失效,将会回退到worker模式,一个工作线程处理一个请求。官方自带的模块,全部是支持event工作模式的。
event工作模式需要Linux系统(Linux 2.6+)对epoll的支持,才能启用。需要补充的是HTTPS的连接(SSL),它的运行模式仍然是类似worker的方式,线程会被一直占用,知道连接关闭。部分比较老的资料里,说event MPM不支持SSL,那个说法是几年前的说法,现在已经支持了。
工作模式与web应用的选择
Apache能更好的为有特殊要求的站点定制。例如,要求 更高伸缩性的站点可以选择使用线程的MPM,即 worker 或 event; 需要可靠性或者与旧软件兼容的站点可以使用 prefork。就使用PHP而言,fastCGI和php-fpm是更推荐的使用模式。
现在的最新浏览器,在单个域名下的连接数变得越来越多(通常都是使用keep-alive),主流浏览器是2-6个(还有继续增长趋势,为了加快页面的并发下载速度)。高并发场景,会越来越成为Web系统的一种常态。Apache很成熟,同时也背负了比较重的历史代码和模块,因此,在Web系统比较方面,Nginx在不少场景下,表现比起Apache更为出色。
切换到工作模式需要在configure中进行声明,或者修改配置文件
[root@localhost bin]# httpd -l //查看apache当前工作模式
Compiled in modules:
core.c
mod_so.c
http_core.c
event.c //
老版本时的默认模式是worker
新版本的是event,————2.x
ssl 安全套接字
envent 允许线程释放
而worker 不可以线程释放
线程是一对一处理
线程锁,线程执行完一个,才会执行下一个
分布式锁,把程序细化,执行完成之后汇总结束,开始执行下一个
threads 代表线程
上限
默认开启
最小
最大
一个进程可以包含的线程
worker 与event 是一对一的工作模式
超出物理内存溢出时,便把多出的线程交给交换空间swap处理,后果就会使swap访问的县城卡顿
一对一的工作模式对比一对多的工作模式要强
[root@localhost httpd2.4.2版本]# tar zxvf apr-1.4.6.tar.gz -C /opt
[rootlocalhost httpd2.4.2版本]# tar zxvf apr-util-1.4.1.tar.gz -C /opt
[root@localhost httpd2.4.2版本]# tar zxvf httpd-2.4.2.tar.gz -C /opt
[root@localhost httpd2.4.2版本]# cd /opt
[root@localhost opt]# ls
apr-1.4.6 apr-util-1.4.1 httpd-2.4.2 rh
[root@localhost opt]# mv apr-1.4.6/ httpd-2.4.2/srclib/apr
[root@localhost opt]# mv apr-util-1.4.1/ httpd-2.4.2/srclib/apr-util
[root@localhost opt]# ls
httpd-2.4.2 rh
[root@localhost opt]# cd httpd-2.4.2/
[root@localhost httpd-2.4.2]# yum install gcc gcc-c++ perl pcre* zlib-devel make expat-devel -y
[root@localhost httpd-2.4.2]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-deflate --enable-expires --enable-cgi --with-mpm=prefork
[root@localhost httpd-2.4.2]# make && make install
[root@localhost httpd-2.4.2]# ln -s /usr/local/httpd/conf/httpd.conf /etc/httpd.conf
[root@localhost httpd-2.4.2]# vim /etc/httpd.conf
Include conf/extra/httpd-mpm.conf
Listen 192.168.247.178:80
#Listen 80
ServerName www.kgc.com:80
[root@localhost httpd-2.4.2]# cd /usr/local/httpd/conf/extra/
[root@localhost extra]# ls
httpd-autoindex.conf httpd-languages.conf httpd-ssl.conf
httpd-dav.conf httpd-manual.conf httpd-userdir.conf
httpd-default.conf httpd-mpm.conf httpd-vhosts.conf
httpd-info.conf httpd-multilang-errordoc.conf proxy-html.conf
[root@localhost extra]# vim httpd-mpm.conf
[root@localhost extra]# cd ../
[root@localhost conf]# cd ../bin/
[root@localhost bin]# ls
ab apu-1-config dbmmanage fcgistarter htdigest httxt2dbm
apachectl apxs envvars htcacheclean htpasswd logresolve
apr-1-config checkgid envvars-std htdbm httpd rotatelogs
[root@localhost bin]# ./apachectl start
[root@localhost bin]# netstat -natp | grep 80
tcp 0 0 192.168.247.178:80 0.0.0.0:* LISTEN 32202/httpd
[root@localhost bin]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 32202 root 3u IPv4 49991 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 32204 daemon 3u IPv4 49991 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 32205 daemon 3u IPv4 49991 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 32206 daemon 3u IPv4 49991 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 32207 daemon 3u IPv4 49991 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 32208 daemon 3u IPv4 49991 0t0 TCP localhost.localdomain:http (LISTEN)
[root@localhost bin]# vim ../conf/extra/httpd-mpm.conf
StartServers 10
MinSpareServers 10
MaxSpareServers 20
MaxRequestWorkers 200
MaxConnectionsPerChild 0
[root@localhost bin]# ./apachectl stop
[root@localhost bin]# ./apachectl start
[root@localhost bin]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 32439 root 3u IPv4 53259 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 32441 daemon 3u IPv4 53259 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 32442 daemon 3u IPv4 53259 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 32443 daemon 3u IPv4 53259 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 32444 daemon 3u IPv4 53259 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 32445 daemon 3u IPv4 53259 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 32446 daemon 3u IPv4 53259 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 32447 daemon 3u IPv4 53259 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 32448 daemon 3u IPv4 53259 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 32449 daemon 3u IPv4 53259 0t0 TCP localhost.localdomain:http (LISTEN)
httpd 32450 daemon 3u IPv4 53259 0t0 TCP localhost.localdomain:http (LISTEN)
[root@localhost bin]# ./httpd -l
Compiled in modules:
core.c
mod_so.c
http_core.c
prefork.c
cgi 通用网关接口
[root@localhost bin]# vim /etc/httpd.conf
Options Indexes FollowSymLinks
[root@localhost bin]# cd /usr/local/httpd/htdocs/
[root@localhost htdocs]# ls
index.html
[root@localhost htdocs]# cat index.html
It works!
[root@localhost htdocs]# systemctl stop firewalld.service
[root@localhost htdocs]# setenforce 0
此时可以访问首页了
默认情况下是有首页的,修改下名字
[root@localhost htdocs]# !vim
vim /etc/httpd.conf
DirectoryIndex index.html
[root@localhost htdocs]# mv index.html a.html
[root@localhost htdocs]# touch b.html
[root@localhost htdocs]# touch c.html
[root@localhost htdocs]# touch d.html
[root@localhost htdocs]# ls
a.html b.html c.html d.html
重新刷新页面,发现以文件列表的形式显示出来
[root@localhost htdocs]# ln -s /usr/share/man ./
[root@localhost htdocs]# ls
a.html b.html c.html d.html man
可以点击,还可以下载里面的文件
这种功能就是刚才的两个参数提供的
如果没有indexs首页,那么就展现所有文件