安装过程中出现的问题:
问题1
configure: error: Please reinstall the BZip2 distribution
解决方法:
[root@lotus php-5.4.45]# yum install -y bzip2-devel
问题2
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
解决方法:
https://sourceforge.net/projects/mcrypt/下载libmcrypt
编译安装libmcrypt即可
在web服务器中,PHP的三种运行方式:
- CGI
- 模块
- FastCGI
安装过程
- 下载php5.4.45,并解压
[root@lotus ~]# wget https://www.php.net/distributions/php-5.4.45.tar.gz
[root@lotus ~]# tar xf php-5.4.45.tar.gz
[root@lotus ~]# cd php-5.4.45
- 编译安装php
参数介绍:
–prefix:安装目录
–with-mysql:mysql的安装目录
–with-openssl:支持openssl
–with-mysqli:mysql与php的交互接口
–enable-mbstring:支持多语言
–with-freetype-dir:支持freetye
–with-jpeg-dir:支持jpeg
–with-png-dir:支持png
–with-zlib:支持压缩,让文件先压缩再传送
–with-libxml-dir:支持xml,并指定xml的目录
–enable-xml:
–enable-sockets:支持套接字
–with-apxs2:让php编译成apache模块
–with-mcrypt:支持加密
–with-config-file-path:php配置文件存放目录
–with-config-file-scan:配置文件扫描
–with-bz2:支持bz2压缩
–enable-maintainer-zts:支持apache的worker或event两个MPM,如使用prefork,则此项不用
–enable-fpm:启用FastCGI,与enable-maintainer-zts互斥
[root@lotus php-5.4.45]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts
[root@lotus php-5.4.45]# make && make install
#安装完成后,拷贝配置文件至/etc/php.ini
[root@lotus php-5.4.45]# cp php.ini-production /etc/php.ini
- 修改httpd配置文件,让httpd服务器可以解析php文件
#/etc/httpd/httpd.conf
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
#添加php文件
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
#修改原html文件为php文件
[root@lotus php-5.4.45]# mv /usr/local/apache/htdocs/index.html /usr/local/apache/htdocs/index.php
- 测试php的访问情况
#在index.php文件中,添加php代码
<html><body><h1>It works!</h1>
<?php
phpinfo();
?>
</body></html>
测试结果:
5. 测试php与mysql的连通性
#修改index.php文件
<html><body><h1>It works!</h1>
<?php
$conn=mysql_connect("localhost","root","");
if ($conn)
echo "Successfullly Connect MySQL...";
else
echo "Failure...";
phpinfo();
?>
</body></html>
测试结果:
关闭mysql服务器,测试结果如下
6. 配置xcache对php的opcode进行加速
到github下载xcache最新版本源码xcache
xcache是php的扩展
#安装php扩展模块,都需先执行phpize
[root@lotus xcache-master]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
[root@lotus xcache-master]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
[root@lotus xcache-master]# make && make install
安装完成后,修改相应配置
[root@lotus xcache-master]# mkdir /etc/php.d
[root@lotus xcache-master]# cp xcache.ini /etc/php.d/
[root@lotus xcache-master]# ls /etc/php.d
xcache.ini
#修改配置文件
[xcache-common]
;; non-Windows example:
#extension = xcache.so
extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so
配置完成后,重新启动httpd服务器,在index.php添加phpinfo()函数,会显示如下图的xCache的信息
7. 启用httpd的虚拟主机
注释中心主机,启用虚拟主机配置项
#注释中心主机
#DocumentRoot "/usr/local/apache/htdocs"
#启用虚拟主机配置文件
# Virtual hosts
Include /etc/httpd/extra/httpd-vhosts.conf
#启用虚拟主机,需在httpd.conf文件中确认LoadModule log_config_module modules/mod_log_config.so这个模块开启
#创建文件存放目录
[root@lotus xcache-master]# mkdir /www/{tye.com,edison.com} -pv
mkdir: created directory `/www'
mkdir: created directory `/www/tye.com'
mkdir: created directory `/www/edison.com'
#修改/etc/httpd/extra/httpd-vhost.conf
<VirtualHost *:80>
DocumentRoot "/www/tye.com"
<Directory /www/tye.com>
Options none
AllowOverride none
Require all granted
</Directory>
ServerName www.tye.com
ErrorLog "/var/log/httpd/tye/error_log"
CustomLog "/var/log/httpd/tye/access_log" combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/www/edison.com"
<Directory /www/edison.com>
Options none
AllowOverride none
Require all granted
</Directory>
ServerName www.edison.com
ErrorLog "/var/log/httpd/edison/error_log"
CustomLog "/var/log/httpd/edison/access_log" common
</VirtualHost>
- 修改windows主机的hosts配置文件
#在C:/windows/system32/drivers/etc/hosts文件中添加以下行
192.168.88.135 www.edison.com www.tye.com
- 访问www.tye.com和www.edison.com
- 使用apache ab对网站进行压力测试
ab
-c #【concurrency】 并发数量,默认为1
-n # 发起的模拟请求数量,默认为1,请求数量一定要大于并发数量
-i 测试时使用HEAD方法,默认为GET
-k 启用HTTP长连接请求方式
-q 静默模式,请求数大于150个不输出请求完成百分比
[root@lotus bin]# ab -n 100 -c 10 http://www.edison.com/index.html
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.edison.com (be patient).....done
Server Software: Apache/2.4.46
Server Hostname: www.edison.com
Server Port: 80
#文档路径
Document Path: /index.html
#文档大小
Document Length: 20 bytes
#一次并发数
Concurrency Level: 10
#请求结束一共花费的时间
Time taken for tests: 0.032 seconds
#完成的请求数
Complete requests: 100
#失败的请求数
Failed requests: 0
#失败的写入次数
Write errors: 0
#服务器响应的整体的大小,,不仅包含报文本身,而且包含有http头,tcp头,帧首部等信息
Total transferred: 27775 bytes
#仅html信息大小,不包含http首部,tcp首部信息
HTML transferred: 2020 bytes
#每秒钟的请求数
Requests per second: 3106.55 [#/sec] (mean)
#一批请求完成的花费的平均时间【本次是10个】
Time per request: 3.219 [ms] (mean)
# 单个请求完成的平均时间
Time per request: 0.322 [ms] (mean, across all concurrent requests)
#传输速率,每秒传输多少字节
Transfer rate: 842.62 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 1.5 1 7
Processing: 0 1 1.3 1 8
Waiting: 0 1 1.1 1 7
Total: 1 3 2.1 2 8
Percentage of the requests served within a certain time (ms)
50% 2
66% 4
75% 4
80% 5
90% 8
95% 8
98% 8
99% 8
100% 8 (longest request)
给httpd服务器添加ssl功能–参见httpd服务器添加ssl功能
将PHP编译为FPM
- 编译安装PHP
[root@lotus php-5.5.6]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-fpm
[root@lotus php-5.5.6]# make && make install
#安装完成后,拷贝配置文件至/etc/php.ini
[root@lotus php-5.5.6]# cp php.ini-production /etc/php.ini
- 配置php-fpm
#为php-fpm提供Sysv init脚本,并将其添加至服务列表
/root/php-5.5.6/sapi/fpm
[root@lotus fpm]# cp init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@lotus fpm]# chmod +x /etc/rc.d/init.d/php-fpm
[root@lotus fpm]# chkconfig --add php-fpm
[root@lotus fpm]# chkconfig php-fpm on
#为php-fpm提供配置文件
[root@lotus fpm]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
#编译php-fpm.conf配置文件
[root@lotus fpm]# vim /usr/local/php/etc/php-fpm.conf
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pid = /usr/local/php/var/run/php-fpm.pid
启动php-fpm出现警告信息
[root@lotus xcache-master]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm [17-Apr-2021 06:42:04] NOTICE: PHP message: PHP Deprecated: Comments starting with ‘#’ are deprecated in /etc/php.d/xcache.ini on line 4 in Unknown on line 0
[17-Apr-2021 06:42:04] NOTICE: PHP message: PHP Warning: PHP Startup: Unable to load dynamic library ‘/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/xcache.so’ - /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/xcache.so: undefined symbol: core_globals_id in Unknown on line 0
done
解决方法:因为xcache和php版本不符,重新下载对应的xcache版本安装后,正常重启
[root@lotus xcache-3.1]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 123585/php-fpm
- 配置httpd以fastcgi方式访问php-fpm
在httpd2.4后,专门有一个模块对应FastCGI的实现,此模块mod_proxy_fcgi.so,它是mod_proxy.so模块的扩展,因此,这两个模块都要加载
#编辑/etc/httpd/httpd.conf配置文件,去除以下两行注释
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
#配置虚拟主机支持使用fcgi
#关闭apache的正向代理
ProxyRequests off
#把以.php结尾的文件请求发送至php-fpm进程,php-fpm至少要知道运行的目录及URI,所以这里直接fcgi://127.0.0.1:9000后指明两个参数,其它的参数传递已经被mod_proxy_fcgi.so进行了封装,不用手动指定。
ProxyPassMatch ^/(.*.php)$ fcgi://127.0.0.1:9000/www/tye.com/$1
常用压力测试工具
ab
http_load
webbench
siege