1.软件版本及架构
应用程序服务器:
192.168.10.31
php-5.6.5.tar.gz
http服务器:
192.168.10.39
httpd-2.4.12.tar.gz
2.安装httpd
请参照:http://my.oschina.net/forlinux/blog/362617
3.安装php-5.6.5
1.解决依赖关系
[root@php-fpm ~]#yum -y install libxml2-devel libmcrypt-devel mhash-devel bzip2-devel php-mbstring gd-devel curl-devel
2.编译php
[root@php-fpm ~]#useradd -r php-fpm
[root@php-fpm ~]# id php-fpm
uid=498(php-fpm) gid=498(php-fpm) groups=498(php-fpm)
[root@php-fpm ~]# tar xf php-5.6.5.tar.gz
[root@php-fpm ~]# cd php-5.6.5
[root@php-fpm php-5.6.5]# ./configure
>--prefix=/usr/local/php \
>--with-config-file-path=/etc \
>--with-config-file-scan-dir=/etc/php.d \
>--with-mysql=mysqlnd \
>--with-mysqli=mysqlnd \
>--with-pdo-mysql=mysqlnd \
>--with-openssl \
>--enable-mbstring \
>--with-jpeg-dir \
>--with-png-dir \
>--with-freetype-dir \
>--with-zlib \
>--with-libxml-dir=/usr \
>--enable-xml \
>--enable-sockets \
>--with-mcrypt \
>--with-bz2 \
>--with-gd \
>--enable-fpm \
>--with-fpm-user=php-fpm \
>--with-fpm-group=php-fpm \
>--with-curl \
>--with-mhash \
>--enable-fileinfo \
>--enable-json \
>--enable-phar \
[root@php-fpm php-5.6.5]# echo $?
0
[root@php-fpm php-5.6.5]# make && make install
3.为php提供配置文件
[root@php-fpm php-5.6.5]# cp php.ini-production /etc/php.ini
4.配置php-fpm
为php-fpm提供配置文件:
[root@php-fpm php-5.6.5]# cd /usr/local/php/etc/
[root@php-fpm etc]# ls
pear.conf php-fpm.conf.default
[root@php-fpm etc]# cp php-fpm.conf.default php-fpm.conf
[root@php-fpm etc]# vim php-fpm.conf
pid = run/php-fpm.pid
error_log = log/php-fpm.log
listen = 192.168.10.31:9000
pm.max_children = 50 #最多能启动多少个进程响应请求
pm.start_servers = 5 #fpm启动时启动几个空闲进程
pm.min_spare_servers = 2 #最少空闲进程数
pm.max_spare_servers = 5 #最大空闲进程数
pm.process_idle_timeout = 3s; #进程空闲超时时间
pm.max_requests = 500 #每个子进程能够接受多少个请求
为php-fpm提供SysV init脚本,并将其添加至服务列表:
[root@php-fpm etc]# cd ~/php-5.6.5/sapi/fpm/
[root@php-fpm fpm]# cp init.d.php-fpm /etc/init.d/php-fpm
[root@php-fpm fpm]# chmod +x /etc/init.d/php-fpm
[root@php-fpm fpm]# chkconfig --add php-fpm
[root@php-fpm fpm]# chkconfig --list php-fpm
php-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off
启动php-fpm,并查看其监听的端口:
[root@php-fpm fpm]# service php-fpm start
------------------------------------------------------------------------------------------
如果有如下报错:
以解决第一个curl那个报错为例:
[root@php-fpm php-5.6.5]# cd ext/curl/
[root@php-fpm curl]# /usr/local/php/bin/phpize
...
...
[root@php-fpm curl]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@php-fpm curl]#make && make instal
--------------------------------------------------------------------------------------------
5.添加防火墙规则,只允许192.168.10.39通过9000端口访问
[root@php-fpm php-5.6.5]# iptables -I INPUT 1 -s 192.168.10.39 -d 192.168.10.31 -p tcp --dport 9000 -j ACCEPT
[root@php-fpm php-5.6.5]# iptables -L -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT tcp -- any any 192.168.10.39 192.168.10.31 tcp dpt:cslistener
22689 3616K ACCEPT all -- any any anywhere anywhere state RELATED,ESTABLISHED
0 0 ACCEPT icmp -- any any anywhere anywhere
1426 85263 ACCEPT all -- lo any anywhere anywhere
2 104 ACCEPT tcp -- any any anywhere anywhere state NEW tcp dpt:ssh
191 19069 REJECT all -- any any anywhere anywhere reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- any any anywhere anywhere reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 5 packets, 716 bytes)
pkts bytes target prot opt in out source destination
4.配置httpd.conf
[root@web ~]# vim /etc/httpd/httpd.conf
#修改
DirectoryIndex index.php index.html
#添加
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
#加载虚拟主机配置文件
# Virtual hosts
Include /etc/httpd/extra/httpd-vhosts.conf
#如果出现以下错误:AH00112: Warning: DocumentRoot [/usr/local/httpd/docs/dummy-host.example.com] does not exist
#将/etc/httpd/extra/httpd-vhosts.conf里默认的配置注释即可
#启用相关模块
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
#说明:在Apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模
#块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩充,因此,这两
#个模块都要加载 :
#LoadModule proxy_module modules/mod_proxy.so
#LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
#注释
#DocumentRoot "/usr/local/httpd/htdocs"
5.配置httpd虚拟主机
[root@web extra]# pwd
/etc/httpd/extra
[root@web extra]# vim httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/web/htdocs/vhost1"
ServerName phpinfo.wsh.com
ErrorLog "logs/vhost1_error.log"
CustomLog "logs/vhost1_access.log" combined
<Directory "/web/htdocs/vhost1">
Options None
AllowOverride None
Require all granted
</Directory>
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.10.31:9000/www/vhost1/$1
</VirtualHost>
[root@web extra]# mkdir -pv /web/htdocs/vhost1
mkdir: created directory `/web'
mkdir: created directory `/web/htdocs'
mkdir: created directory `/web/htdocs/vhost1'
[root@web extra]# httpd -t
Syntax OK
[root@web extra]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
6.在php服务器上提供网页文件
[root@php-fpm ~]# mkdir -pv /www/vhost1
mkdir: created directory `/www'
mkdir: created directory `/www/vhost1'
[root@php-fpm ~]# vim /www/vhost1/index.php
[root@php-fpm ~]# cat /www/vhost1/index.php
<?php
phpinfo();
?>
7.测试
测试1:
前提:修改hosts文件
192.168.10.39 phpinfo.wsh.com
测试2:
[root@web ~]# vim /web/htdocs/vhost1/index.html
[root@web ~]# cat /web/htdocs/vhost1/index.html
<html>
<title>Test Page.</title>
<body>
<h1>Hi There.</h1>
Happy New Year!
</body>
</html>
8.使用ab命令进行压力测试
说明:
ab –c # –n # http://192.168.10.39/index.php
-c : 并发访问数量
-n : 总的请求数量
[root@web xcache-3.2.0]#ulimit -n 65536
[root@web xcache-3.2.0]# ab -c 100 -n 900 http://192.168.10.39/index.php
This is ApacheBench, Version 2.3 <$Revision: 1638069 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.10.39 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Finished 900 requests
Server Software: Apache/2.4.12
Server Hostname: 192.168.10.39
Server Port: 80
Document Path: /index.php
Document Length: 82363 bytes
Concurrency Level: 100
Time taken for tests: 4.926 seconds
Complete requests: 900
Failed requests: 844
(Connect: 0, Receive: 0, Length: 844, Exceptions: 0)
Total transferred: 74280538 bytes
HTML transferred: 74127538 bytes
Requests per second: 182.69 [#/sec] (mean)
Time per request: 547.372 [ms] (mean)
Time per request: 5.474 [ms] (mean, across all concurrent requests)
Transfer rate: 14724.81 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 7 20.7 0 108
Processing: 105 517 91.4 539 641
Waiting: 102 494 87.1 517 590
Total: 120 524 81.9 541 641
Percentage of the requests served within a certain time (ms)
50% 541
66% 554
75% 561
80% 567
90% 579
95% 587
98% 596
99% 606
100% 641 (longest request)
9.安装XCache后,进行压测
[root@php-fpm ~]# tar xf xcache-3.2.0.tar.gz
[root@php-fpm ~]# cd xcache-3.2.0
[root@php-fpm xcache-3.2.0]# phpize
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
[root@php-fpm xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
[root@php-fpm xcache-3.2.0]# echo $?
0
[root@php-fpm xcache-3.2.0]# make && make install
[root@php-fpm xcache-3.2.0]# mkdir /etc/php.d
[root@php-fpm xcache-3.2.0]# cp xcache.ini /etc/php.d/
[root@php-fpm xcache-3.2.0]# vim /etc/php.d/xcache.ini
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/xcache.ini
[root@php-fpm xcache-3.2.0]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
再次压测:
[root@web xcache-3.2.0]# ab -c 100 -n 900 http://192.168.10.39/index.php
This is ApacheBench, Version 2.3 <$Revision: 1638069 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.10.39 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Finished 900 requests
Server Software: Apache/2.4.12
Server Hostname: 192.168.10.39
Server Port: 80
Document Path: /index.php
Document Length: 86096 bytes
Concurrency Level: 100
Time taken for tests: 4.858 seconds
Complete requests: 900
Failed requests: 72
(Connect: 0, Receive: 0, Length: 72, Exceptions: 0)
Total transferred: 77639320 bytes
HTML transferred: 77486320 bytes
Requests per second: 185.27 [#/sec] (mean)
Time per request: 539.739 [ms] (mean)
Time per request: 5.397 [ms] (mean, across all concurrent requests)
Transfer rate: 15608.29 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 6 18.4 0 89
Processing: 82 508 102.3 530 659
Waiting: 80 486 96.9 508 612
Total: 97 514 92.4 531 659
Percentage of the requests served within a certain time (ms)
50% 531
66% 545
75% 554
80% 561
90% 582
95% 599
98% 613
99% 622
100% 659 (longest request)
XCache对于加速php执行,效果还是挺明显的,可能是由于我带宽的原因,没有明显的展示出来。
tips:
如果httpd的error log中有关于timezone的错误,请做如下修改:
#vim /etc/php.ini
date.timezone=Asia/Shanghai
#service php-fpm restart