fcgi模式编译安装 LAMP+xcache
php 的工作模式:
php 在 lamp 环境下共有三种工作模式:CGI 模式、apache 模块、FastCGI 模式。CGI 模式下运行 PHP,性能不是很好。作为 apache 的模块方式运行,在以前的课程中编译安装 lamp 已经介绍过了。FastCGI 的方式和 apache 模块的不同点在于:FastCGI 方式 PHP 是一处独立的进程,所有 PHP 子进程都由 PHP 的一个叫作 php-fpm 的组件负责管理;而 apache 模块化方式运行的 PHP,则是 apache 负责调用 PHP 完成工作。PHP 的 FastCGI 方式性能要比 apache模块化方式强很多,今天我们以 FastCGI 方式编译安装 lamp。
FastCGI 工作机制:
首先客户端发起请求,请求分为 2 种,一种是静态请求它可以直接由 Apache 直接响应返回;另一种是动态的请求,如其中包含中 php 或者 Perl 这种脚本解释性语言,则由 Apache 服务器通过fastcgi协议调用php服务器执行并返回给Apache由Apache返回解释执行后的结果,如果这个过程中涉及到对数据的操作,此时 php 服务器还会还会通过 mysql 协议调用 mysql服务器。
环境:
Linux |
Web服务器 |
PHP |
MySQL |
xcache |
Centos7.2 |
httpd-2.4.23 |
php-5.6.27 |
mysql-5.7.20 |
xcache-3.2.0 |
主机规划
至少 3 台主机,操作系统都是 centos7.2.网段在 192.168.133.0/24 网关 192.168.133.1
分配如下:
1 台 httpd 服务器(192.168.133.111)
1 台 php 服务器(192.168.133.112)
1 台 mysql 服务器(192.168.133.113)
编译安装 LAMP
编译安装 apache(请参考前面 apache 的安装)
编译安装 mysql(请参考 mysql 安装)
1、解决依赖关系
[root@php ~]# yum install libxml2-devel lzip2-devel libcurl-devel libmcrypt-devel openssl-devel bzip2-devel
安装 libmcrypt
[root@php src]# tar zxf libmcrypt-2.5.7.tar.gz
[root@php src]# cd libmcrypt-2.5.7/
[root@php libmcrypt-2.5.7]# ./configure --prefix=/usr/local/libmcrypt && make && make install
2、编译安装PHP
[root@php src]# tar zxf php-5.6.27.tar.gz
[root@php src]# cd php-5.6.27/
[root@php php-5.6.27]# ./configure --prefix=/usr/local/php5.6 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt=/usr/local/libmcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts && make && make install
3、提供 php 配置文件
[root@php php-5.6.27]# cp php.ini-production /etc/php.ini
4、为 php-fpm 提供脚本
[root@php php-5.6.27]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@php php-5.6.27]# chmod +x /etc/init.d/php-fpm
[root@php php-5.6.27]# chkconfig --add php-fpm
[root@php php-5.6.27]# chkconfig php-fpm on
5、提供 php-fpm 配置文件并编辑
[root@php php-5.6.27]# cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf
[root@php php-5.6.27]# vim /usr/local/php5.6/etc/php-fpm.conf
修改内容如下:
pid = run/php-fpm.pid
listen = 192.168.133.112:9000
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
启动 php-fpm 服务:
[root@php php-5.6.27]# service php-fpm start
Starting php-fpm done
[root@php php-5.6.27]# netstat -anpt | grep php-fpm
tcp 0 0 192.168.133.112:9000 0.0.0.0:* LISTEN 1573/php-fpm: maste
在该主机上新建虚拟主机目录用于存放网页文件
[root@php php-5.6.27]# mkdir -p /var/www/benet
至此 php 安装配置完毕,下面配置 apache 通过 fastcgi 协议调用 php
6、配置 apache(切换到 apache 主机上操作)
在 Apache2.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
[root@httpd http-2.4.23]# apachectl -M | grep proxy
proxy_module (shared)
proxy_fcgi_module (shared)
建立一个目录作为虚拟主机的家目录
[root@httpd http-2.4.23]# mkdir -p /var/www/benet
编辑主配置文件 httpd.conf,开启虚拟主机
启用 Include conf/extra/httpd-vhosts.conf
同时定位 AddType;添加下面两行:让 apache 能识别 php 格式的页面
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
并且定位至 DirectoryIndex:支持 php 格式的主页
#添加 index.php(最好添加在最前面)
配置虚拟主机支持使用 fcgi
[root@httpd http-2.4.23]# vim /usr/local/http-2.4.23/conf/extra/httpd-vhosts.conf
ServerAdmin [email protected]
DocumentRoot "/var/www/benet"
ServerName www.benet.com
ServerAlias benet.com
ErrorLog "logs/benet.com-error_log"
CustomLog "logs/benet.com-access_log" common
ProxyRequests Off
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://192.168.133.112:9000/var/www/benet/$1
Options FollowSymLinks
AllowOverride None
Require all granted
测试 LAMP 环境:
测试 LAMP 环境:
在 mysql 主机上创建用于 php 服务器连接的 mysql 账户
mysql> grant all on *.* to testuser@'%' identified by '123';
注意防火墙要允许 mysql 连接
在 php 服务器上的/var/www/benet 目录下创建.php 的测试页:
[root@php php-5.6.27]# cat /var/www/benet/index.php
phpinfo();
?>
[root@php php-5.6.27]# cat /var/www/benet/test1.php
测试访问 php 测试页:
看到上面两个测试页说明 apache、php、mysql 之间可以协同工作了。
压力测试
网站性能压力测试是服务器网站性能调优过程中必不可缺少的一环。只有让服务器处在高压情况下,才能真正体现出软件、硬件等各种设置不当所暴露出的问题。
性能测试工具目前最常见的有以下几种:ab、http_load、webbench、siege。今天我们专门来介绍 ab。
ab 是 apache 自带的压力测试工具。ab 非常实用,它不仅可以对 apache 服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试。比如 nginx、tomcat、IIS 等。
下面我们开始介绍有关 ab 命令的使用:
1.ab 的原理
2.ab 的安装
3.ab 参数说明
4.ab 性能指标
5.ab 实际使用
6.测试 nginx 性能
1)ab 的原理
ab 是 apachebench 命令的缩写。
ab 的原理:ab 命令会创建多个并发访问线程,模拟多个访问者同时对某一 URL 地址进行访问。它的测试目标是基于 URL 的,因此,它既可以用来测试 apache 的负载压力,也可以测试 nginx、lighthttp、tomcat、IIS 等其它 Web 服务器的压力。
ab 命令对发出负载的计算机要求很低,它既不会占用很高 CPU,也不会占用很多内存。但却会给目标服务器造成巨大的负载,其原理类似 CC 攻击。自己测试使用也需要注意,否则一次上太多的负载。可能造成目标服务器资源耗完,严重时甚至导致死机。
2)ab 的安装
ab 的安装非常简单,如果是源码安装 apache 的话,那就更简单了。apache 安装完毕后 ab命令存放在 apache 安装目录的 bin 目录下。如下:
/usr/local/http2.4.23/bin/ab
如果 apache 是通过 yum 的 RPM 包方式安装的话,ab 命令默认存放在/usr/bin 目录下。如下:
which ab
注意:如果不想安装 apache 但是又想使用 ab 命令的话,我们可以直接安装 apache 的工具
包 httpd-tools。如下:
[root@httpd src]# yum -y install httpd-tools查看 ab 是否安装成功,可以切换到上述目录下,使用 ab –V 命令进行检测。如下:
[root@httpd htdocs]# /usr/local/bin/ab -V
This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
如上图,如果 ab –V 命令出错,可以 exporexportLD_LIBRARY_PATH="/usr/local/openssl/lib/",就可以了。
3)ab 参数说明
有关 ab 命令的使用,我们可以通过帮助命令进行查看。如下:
[root@httpd htdocs]# ab --help
下面我们对这些参数,进行相关说明。如下:
-n:在测试会话中所执行的请求个数(即总请求数)。
-c:一次产生的请求个数(即 并发用户数 )。
[root@mysql src]# ab -c 500 -n 10000 192.168.133.111/index.index
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.133.111 (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.23
Server Hostname: 192.168.133.111
Server Port: 80
Document Path: /index.index #请求的资源
Document Length: 208 bytes #HTTP 响应数据的正文长度
Concurrency Level: 500 #并发个数(并发用户数)
Time taken for tests: 3.085 seconds #所有这些请求处理完成所花费的时间
Complete requests: 10000 #完成请求数
Failed requests: 0 #失败的请求数
Write errors: 0
Non-2xx responses: 10000
Total transferred: 3860000 bytes #表示所有请求的响应数据长度总和,包括每个 HTTP响应数据的头信息和正文数据的长度。注意这里不包括 HTTP 请求数据的长度,仅仅为 web服务器流向用户 PC 的应用层数据总长度。
HTML transferred: 2080000 bytes # 表示所有请求的响应数据中正文数据的总和,也就是减去了 Total transferred 中 HTTP 响应数据中的头信息的长度。
Requests per second: 3241.43 [#/sec] (mean) #吞吐量-每秒请求数。计算公式:Complete requests/Time taken for tests
Time per request: 154.253 [ms] (mean) #用户平均请求等待时间,计算公式:Timetoken for tests/(Complete requests/Concurrency Level)。
Time per request: 0.309 [ms] (mean, across all concurrent requests) #服务器平均请求等待时间,计算公式:Time taken for tests/Complete requests。
Transfer rate: 1221.87 [Kbytes/sec] received #表示这些请求在单位时间内从服务器获取的数据长度,计算公式:Total trnasferred/ Time taken for tests,这个统计很好的说明服务器的处理能力达到极限时,其出口宽带的需求量。(即平均每秒网络上的流量)
Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 58 72.5 57 1075
Processing: 10 91 64.4 82 543
Waiting: 2 72 63.6 61 518
Total: 37 150 97.8 144 1166
Percentage of the requests served within a certain time (ms)
50% 144
66% 155
75% 161
80% 167
90% 181
95% 296
98% 352
99% 530
100% 1166 (longest request)
这部分数据用于描述每个请求处理时间的分布情况,比如以上测试,80%的请求处理时间都不超过 66ms,这个处理时间是指前面的 Time per request,即对于单个用户而言,平均每个请求的处理时间。
我们再来进行一次压力测试,此时并发用户数为 1000,其他条件不变,查看两次测试结果的 吞吐量差别
4 )ab 性能指标
在进行性能测试过程中有几个指标比较重要:
1、吞吐率(Requests per second)
服务器并发处理能力的量化描述,单位是 reqs/s,指的是在某个并发用户数下单位时间内处理的请求数。某个并发用户数下单位时间内能处理的最大请求数,称之为最大吞吐率。
记住:吞吐率是基于并发用户数的。这句话代表了两个含义:
a、吞吐率和并发用户数相关
b、不同的并发用户数下,吞吐率一般是不同的
计算公式:总请求数/处理完成这些请求数所花费的时间,即
Request per second=Complete requests/Time taken fortests
必须要说明的是,这个数值表示当前机器的整体性能,值越大越好。
2、并发连接数(The number of concurrent connections)
并发连接数指的是某个时刻服务器所接受的请求数目,简单的讲,就是一个会话。
3、并发用户数(Concurrency Level)
要注意区分这个概念和并发连接数之间的区别,一个用户可能同时会产生多个会话,也即连接数。
4、用户平均请求等待时间(Time per request)
计算公式:处理完成所有请求数所花费的时间/(总请求数/并发用户数),即:
Time per request=Time taken for tests/(Complete requests/Concurrency Level)
5、服务器平均请求等待时间(Time per request:across all concurrent requests)
计算公式:处理完成所有请求数所花费的时间/总请求数,即:
Time taken for/testsComplete requests
可以看到,它是吞吐率的倒数。
同时,它也等于用户平均请求等待时间/并发用户数,即
Time per request/Concurrency Level
8、CentOS7.2 下安装 php 加速软件 Xcache(在 php 主机上完成下面的操作)
说明:
php 安装目录:/usr/local/php5.6
php.ini 配置文件路径:/etc/php.ini
php 网页根目录:/var/www/benet
1)安装 xcache
[root@php src]# wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz
[root@php src]# tar zxf xcache-3.2.0.tar.gz
[root@php src]# cd xcache-3.2.0/
[root@php xcache-3.2.0]# /usr/local/php5.6/bin/phpize
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
e#用 phpize 生成 configure 配置文件
[root@php xcache-3.2.0]#./configure --enable-xcache --enable-xcache-coverager --enable-xcache-optimizer --with-php-config=/usr/local/php5.6/bin/php-config && make && make install
安装完成之后,出现下面的界面,记住以下路径,后面会用到
2)创建 xcache 缓存文件
[root@php xcache-3.2.0]# touch /tmp/xcache
[root@php xcache-3.2.0]# chmod 777 /tmp/xcache
3)拷贝 xcache 后台管理程序到网站根目录
[root@php xcache-3.2.0]# cp -r htdocs/ /var/www/benet/xcache
4)配置 php 支持 xcache
vi / etc/php.ini #编辑配置文件,在最后一行添加以下内容
[xcache-common]
extension = /usr/local/php5.6/lib/php/extensions/no-debug-zts-20131226/xcache.so
[xcache.admin]
xcache.admin.enable_auth = Off
[xcache]
xcache.shm_scheme ="mmap"
xcache.size=60M
xcache.count =1
xcache.slots =8K
xcache.ttl=0
xcache.gc_interval =0
xcache.var_size=64M
xcache.var_count =1
xcache.var_slots =8K
xcache.var_ttl=0
xcache.var_maxttl=0
xcache.var_gc_interval =300
xcache.test =Off
xcache.readonly_protection = Off
xcache.mmap_path ="/tmp/xcache"
xcache.coredump_directory =""
xcache.cacher =On
xcache.stat=On
xcache.optimizer =Off
[xcache.coverager]
xcache.coverager =On
xcache.coveragedump_directory =""
将 xcache 目录拷贝到 apache 主机的网页文档目录下
[root@php xcache-3.2.0]# scp -r /var/www/benet/xcache/ 192.168.133.111:/var/www/benet/
6)测试
浏览器打开网站根目录下面的 xcache
http://192.168.133.111/xcache 可以看到如下页面:
至此,Linux 下安装 php 加速软件 Xcache 教程完成
执行 ab 压力测试:
执行第一次压力测试:
[root@mysql src]# ab -c 100 -n 1000 192.168.133.111/index.php
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.133.111 (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
Completed 1000 requests
Finished 1000 requests
Server Software: Apache/2.4.23
Server Hostname: 192.168.133.111
Server Port: 80
Document Path: /index.php
Document Length: 84894 bytes
Concurrency Level: 100
Time taken for tests: 3.006 seconds
Complete requests: 1000
Failed requests: 116
(Connect: 0, Receive: 0, Length: 116, Exceptions: 0)
Write errors: 0
Total transferred: 85064873 bytes
HTML transferred: 84893873 bytes
Requests per second: 332.66 [#/sec] (mean)
Time per request: 300.606 [ms] (mean)
Time per request: 3.006 [ms] (mean, across all concurrent requests)
Transfer rate: 27634.53 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 8 11.8 2 60
Processing: 14 282 62.9 275 705
Waiting: 4 226 48.4 227 463
Total: 15 290 62.5 282 709
Percentage of the requests served within a certain time (ms)
50% 282
66% 310
75% 327
80% 336
90% 371
95% 400
98% 437
99% 447
100% 709 (longest request)
执行第二次压力测试
[root@mysql src]# ab -c 100 -n 1000 192.168.133.111/index.php
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.133.111 (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
Completed 1000 requests
Finished 1000 requests
Server Software: Apache/2.4.23
Server Hostname: 192.168.133.111
Server Port: 80
Document Path: /index.php
Document Length: 84894 bytes
Concurrency Level: 100
Time taken for tests: 3.054 seconds
Complete requests: 1000
Failed requests: 102
(Connect: 0, Receive: 0, Length: 102, Exceptions: 0)
Write errors: 0
Total transferred: 85064882 bytes
HTML transferred: 84893882 bytes
Requests per second: 327.47 [#/sec] (mean)
Time per request: 305.374 [ms] (mean)
Time per request: 3.054 [ms] (mean, across all concurrent requests)
Transfer rate: 27203.14 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 26 24.4 19 111
Processing: 12 270 80.1 275 669
Waiting: 7 201 67.0 208 611
Total: 12 296 76.8 297 703
Percentage of the requests served within a certain time (ms)
50% 297
66% 318
75% 344
80% 349
90% 397
95% 411
98% 442
99% 506
100% 703 (longest request)
查看 xcache 的命中率:
部署 bbs 论坛
Discuz 的程序文件解压,并且将 upload 中所有文件放置到网站目录(php 服务器的操作)
[root@php src]# unzip Discuz_7.0.0_FULL_SC_UTF8.zip -d discus
[root@php src]# mv discus/Discuz_7.0.0_FULL_SC_UTF8/upload/ /var/www/benet/bbs
设置 php-fpm 的服务用户为下面文件的属主或者对其设置写权限,否则安装时会报错
[root@php src]# cd /var/www/benet/bbs
[root@php bbs]# chown -R nobody config.inc.php attachments/ forumdata/ uc_client/data/cache/ templates/
[root@php bbs]# chmod -R 777 uc_server/data/
修改 php.ini 文件
short_open_tag = On
[root@php bbs]# service php-fpm restart
web 服务器也需要有静态文件(apache 服务器上操作)
[root@httpd src]# unzip Discuz_7.0.0_FULL_SC_UTF8.zip -d discus
[root@httpd src]# mv discus/Discuz_7.0.0_FULL_SC_UTF8/upload/ /var/www/benet/bbs
设置 httpd 的服务用户对指定文件也需要有写权限
[root@httpd src]# cd /var/www/benet/bbs
[root@httpd bbs]# chown -R daemon config.inc.php attachments/ forumdata/ uc_client/data/cache/ templates/
在数据库服务器上创建 bbs 数据库及授权帐户
mysql> create database bbsdb;
mysql> grant all on bbsdb.* to runbbs@'%' identified by 'pwd@123';
配置完成之后,输入 httd://192.168.133.111/bbs/install 即可安装
出现上面这种情况是由于 php 服务器安装了 discuz 之后导致程序发生变化从而导致动态服务器和静态服务器的程序不一致,只需要手动把 bbs 服务器的文件和 web 服务器进行一次同步即可,如果想实现自动同步,需要使用其他服务,如 initory+rsync、sersync 等工具。
[root@php bbs]# scp -r /var/www/benet/bbs/* [email protected]:/var/www/benet/bbs/
动态服务器和静态服务器同步文件之后,再次访问 bbs 的网址就正常了。