[root@localhost ~]# rpm -e `rpm -qa | grep httpd` --nodeps
如果编译安装无法执行,可能是开发工具没有安装,执行下面命令即可安装。(如已安装则跳过即可)。
[root@localhost ~]# yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel
1. 挂载rpm包的iso镜像。
查看虚拟机使用ISO镜像,设备状态勾选已连接和启动时连接后确定即可。
2. 通过脚本解压tar包并编译安装。
- [root@localhost ~]# mkdir /sh
-
- [root@localhost ~]# cd /sh
-
- [root@localhost sh]# vim add.sh
-
- #!/bin/bash
-
- mount /dev/cdrom /media/
-
- tar zxf /media/apr-1.5.2.tar.gz -C /usr/src
-
- cd /usr/src/apr-1.5.2
-
- ./configure --prefix=/usr/local/apr && make && make install
-
- tar zxf /media/apr-util-1.5.4.tar.gz -C /usr/src
-
- cd /usr/src/apr-util-1.5.4
-
- ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install
-
- yum -y install zlib-*
-
- tar zxf /media/pcre-8.39.tar.gz -C /usr/src
-
- cd /usr/src/pcre-8.39
-
- ./configure --prefix=/usr/local/pcre && make && make install
-
- tar zxf /media/openssl-1.0.1u.tar.gz -C /usr/src
-
- cd /usr/src/openssl-1.0.1u
-
- ./config -fPIC --prefix=/usr/local/openssl enable-shared && make && make install
保存退出脚本并执行脚本文件,等待脚本执行完成并确认成功。
[root@localhost sh]# sh add.sh
3. 安装Apache主程序。
下面也通过脚本来安装主程序。
- [root@localhost sh]# vim httpd.sh
-
- #!/bin/bash
-
- tar zxf /media/httpd-2.4.25.tar.gz -C /usr/src
-
- cd /usr/src/httpd-2.4.25
-
- ./configure --prefix=/usr/local/httpd --enable-so --enable-cgi --enable-cgid --enable-ssl --with-ssl=/usr/local/openssl --enable-rewrite --with-pcre=/usr/local/pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mods-shared=most --enable-mpms-shared=all --with-mpm=event --enable-proxy --enable-proxy-fcgi --enable-expires --enable-deflate && make && make install
保存退出脚本并执行脚本文件,等待脚本执行完成并确认成功。
4. 优化链接和添加系统服务。
- [root@localhost sh]# ln -s /usr/local/httpd/bin/* /usr/local/bin //链接优化
-
- [root@localhost sh]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd //添加服务
-
- [root@localhost sh]# vim /etc/init.d/httpd
- 第二行添加下面两行命令
-
- # chkconfig: 35 85 15 \\声明服务启动级别,开机启动顺序,关机关闭顺序
-
- # description: apache 2.4.25 \\服务声明,简要信息
-
-
-
- [root@localhost sh]# chkconfig --add httpd //添加到系统服务
-
- [root@localhost sh]# chkconfig httpd on //设置开机自启
-
- [root@localhost sh]# systemctl start httpd //开启服务
5. 查看httpd模块
安装后可以通过下面三行命令查看各项模块。
- httpd -V \\查看版本和已装模块
-
- httpd -l \\只查看静态编译模块
-
- httpd -M \\查看所有模块
6. 查看mpm配置文件
vim /usr/local/httpd/conf/extra/httpd-mpm.conf
StartServers 3 #apache 启动时候默认开始的子进程数
MinSpareThreads 75 #最小空闲数量的工作线程
MaxSpareThreads 250 #最大空闲数量的工作线程
ThreadsPerChild 25 #每个子进程产生的线程数量
MaxRequestWorkers 400 #允许同时的最大接入请求数量
MaxConnectionsPerChild 0 #每个子进程可处理的请求数
#企业推荐参数
StartServers 2 #推荐设置:小=默认 中=3~5 大=5~10
MaxClients 150 #推荐设置:小=500 中=500~1500 大型=1500~3000
MinSpareThreads 25 #推荐设置:小=默认 中=50~100 大=100~200
MaxSpareThreads 75 #推荐设置:小=默认 中=80~160 大=200~400 ThreadsPerChild 25 #推荐设置:小=默认 中=50~100 大型=100~200
MaxRequestsPerChild 0 #推荐设置:小=10000 中或大=10000~50000(此外,如果MaxClients/ThreadsPerChild大于16,还需额外设置ServerLimit参数,ServerLimit必须大于等于 MaxClients/ThreadsPerChild 的值。)
7. 查看apache主页
通过访问http://192.168.1.2可以看到apache默认网站。
8. 使用ab命令进行压力测试
通过上面命令已经启动httpd服务,通过ab工具测试httpd服务,如未安装通过下面命令安装ab命令工具(打开第二台服务器对第一台httpd服务器进行测试)。
[root@localhost ~]# yum -y install httpd-tools
安装完毕后执行下面命令对服务器进行200人并发访问,发出10000个请求。
- [root@localhost ~]# ab -c 200 -n 10000 http://192.168.1.2/index.html
-
- This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
-
- Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
-
- Licensed to The Apache Software Foundation, http://www.apache.org/
-
-
-
- Benchmarking 192.168.1.2 (be patient)
-
- Completed 1000 requests
-
- Completed 2000 requests
-
- Completed 3000 requests
-
- Completed 4000 requests
-
- Completed 5000 requests
-
- Completed 6000 requests
-
- Completed 7000 requests
-
- Completed 8000 requests
-
- Completed 9000 requests
-
- Completed 10000 requests
-
- Finished 10000 requests
-
-
-
-
-
- Server Software: Apache/2.4.25
-
- Server Hostname: 192.168.1.2
-
- Server Port: 80
-
-
-
- Document Path: /index.html
-
- Document Length: 45 bytes
-
-
-
- Concurrency Level: 200
-
- Time taken for tests: 3.803 seconds
-
- Complete requests: 10000
-
- Failed requests: 0
-
- Write errors: 0
-
- Total transferred: 2890000 bytes
-
- HTML transferred: 450000 bytes
-
- Requests per second: 2629.29 [#/sec] (mean)
-
- Time per request: 76.066 [ms] (mean)
-
- Time per request: 0.380 [ms] (mean, across all concurrent requests)
-
- Transfer rate: 742.06 [Kbytes/sec] received
- Connection Times (ms)
- min mean[+/-sd] median max
- Connect: 0 22 153.7 2 3008
- Processing: 8 47 34.1 44 451
- Waiting: 4 44 33.0 43 435
- Total: 28 69 157.9 46 3057
- Percentage of the requests served within a certain time (ms)
- 50% 46
- 66% 49
- 75% 51
- 80% 54
- 90% 62
- 95% 80
- 98% 267
- 99% 1048
- 100% 3057 (longest request)