<服务>PHP优化和扩展

时间:Dec. 5, 2016 分类:

目录:

php解析优化

LAMP下php的libphp5.so响应并处理php请求的方式

  1. apache服务器接收到php请求
  2. apache将php请求传递给libphp5.so模块
  3. libphp5.so定位php文件并加载到内存
  4. libphp5.so编译源代码成为opcode树
  5. libphp5.so执行opcode树

LNMP下php fcgi响应并处理php请求的方式

  1. nginx服务器接收到php请求
  2. nginx中的fastcgi_pass传递给fastcgiphp守护进程
  3. fastcgiphp定位php文件并加载到内存
  4. fastcgiphp编译源代码成为opcode树
  5. fastcgiphp执行opcode树

缓慢原因:

php每次处理请求都要重新通过php引擎来解析该程序,并将其编译为操作码,然后php对操作引擎执行并丢弃。

优化方式:

进行php操作码进行缓存,如果下一次的请求改页面,直接返回该操作码,进行执行即可,可以提高执行的效率,进而提高了web服务的响应效率

加速器种类:

xcache,eaccelerator,Zend,apc

添加加速模块准备:

[root@why-1 ~]# echo 'LC_ALL=C' >> /etc/prodfile
[root@why-1 ~]# yum install -y perl-devel

安装eaccelerator模块

eaccelerator模块为加速,优化和动态内容缓存的扩展模块 eaccelerator连接

[root@why-1 ~]# tar jxf eaccelerator-0.9.6.tar.bz2 
[root@why-1 ~]# cd eaccelerator-0.9.6

添加php的外挂模块

[root@why-1 eaccelerator-0.9.6]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626

编译指定参数

[root@why-1 eaccelerator-0.9.6]# ./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
[root@why-1 eaccelerator-0.9.6]# make
[root@why-1 eaccelerator-0.9.6]# make install
Installing shared extensions:     /usr/local/php5.3.27/lib/php/extensions/no-debug-non-zts-20090626/

添加eaccelerator配置到php配置

[root@why-1 xcache-1.3.2]# cat >> /usr/local/php/lib/php.ini<<EOF 
[eaccelerator]
extension=eaccelerator.so
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
EOF

查看一下模块信息

[root@why-1 html]# /usr/local/php/bin/php -v
PHP 5.3.27 (cli) (built: Nov  3 2016 01:45:55) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies
    with eAccelerator v0.9.6, Copyright (c) 2004-2010 eAccelerator, by eAccelerator
[root@why-1 html]# ll /tmp/eaccelerator  
总用量 64
drwxrwxrwx 18 root root 4096 12月  5 06:25 0
drwxrwxrwx 18 root root 4096 12月  5 06:25 1
drwxrwxrwx 18 root root 4096 12月  5 06:25 2
drwxrwxrwx 18 root root 4096 12月  5 06:25 3
drwxrwxrwx 18 root root 4096 12月  5 06:25 4
drwxrwxrwx 18 root root 4096 12月  5 06:25 5
drwxrwxrwx 18 root root 4096 12月  5 06:25 6
drwxrwxrwx 18 root root 4096 12月  5 06:25 7
drwxrwxrwx 18 root root 4096 12月  5 06:25 8
drwxrwxrwx 18 root root 4096 12月  5 06:25 9
drwxrwxrwx 18 root root 4096 12月  5 06:25 a
drwxrwxrwx 18 root root 4096 12月  5 06:25 b
drwxrwxrwx 18 root root 4096 12月  5 06:25 c
drwxrwxrwx 18 root root 4096 12月  5 06:25 d
drwxrwxrwx 18 root root 4096 12月  5 06:25 e
drwxrwxrwx 18 root root 4096 12月  5 06:25 f

如果有动态请求的话

[root@why-1 html]# find /tmp/eaccelerator/ -type f

可以看到一些缓存文件

部分参数解释

[eaccelerator]

  • extension=eaccelerator.so
  • eaccelerator.shm_size="64" #缓存大小
  • eaccelerator.cache_dir="/tmp/eaccelerator" #缓存目录
  • eaccelerator.enable="1" #是否缓存
  • eaccelerator.optimizer="1" #是否优化
  • eaccelerator.check_mtime="1" #是否检查修改时间
  • eaccelerator.debug="0" #是否调试
  • eaccelerator.filter="" #是否过滤
  • eaccelerator.shm_max="0" #
  • eaccelerator.shm_ttl="3600"
  • eaccelerator.shm_prune_period="3600"
  • eaccelerator.shm_only="0"
  • eaccelerator.compress="1" #是否压缩
  • eaccelerator.compress_level="9" #压缩级别

xcache模块

与eAccelerator模块类似

xcache连接

[root@why-1 ~]# tar jxf xcache-1.3.2.tar.bz2 
[root@why-1 ~]# cd xcache-1.3.2
[root@why-1 xcache-1.3.2]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
[root@why-1 xcache-1.3.2]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
[root@why-1 xcache-1.3.2]# make
[root@why-1 xcache-1.3.2]# make install
Installing shared extensions:     /usr/local/php5.3.27/lib/php/extensions/no-debug-non-zts-20090626/
[root@why-1 html]# vi ~/xcache-1.3.2/xcache.ini
注释掉
zend_extension = /usr/local/lib/php/extensions/non-debug-non-zts-xxx/xcache.so
zend_extension_ts = c:/php/extensions/php_xcache.dll
取消注释
; extension = xcache.so
可修改参数
xcache.size  =               60M        可修改为128M
xcache.count =                 1        修改为cpu核数
xcache.ttl   =                 0            可修改为86400           生效时间        0为永久保存
xcache.gc_interval =           0        可修改为3600            回收间隔        0为不回收
xcache.var_size  =            4M        可修改为0
[root@why-1 html]# cat ~/xcache-1.3.2/xcache.ini >> /usr/local/php/lib/php.ini

php数据库连接优化

缓慢原因

每次在接收到php数据库请求时都需要调用数据库

优化方式

将一些常用的数据缓存到内存

memcache模块

用于缓存的客户端软件

[root@why-1 ~]# tar xf memcache-2.2.5.tgz 
[root@why-1 ~]# cd memcache-2.2.5
[root@why-1 memcache-2.2.5]#  /usr/local/php/bin/phpize
[root@why-1 memcache-2.2.5]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@why-1 memcache-2.2.5]# make
[root@why-1 memcache-2.2.5]# make install
Installing shared extensions:     
[root@why-1 memcache-2.2.5]# /usr/local/php5.3.27/lib/php/extensions/no-debug-non-zts-20090626/
[root@why-1 imagick-2.3.0]# vim /usr/local/php/lib/php.ini 
修改
extension_dir = "/usr/local/php5.3.27/lib/php/extensions/no-debug-non-zts-20090626/"
添加
extension = memcache.so

检验

[root@why-1 html]# vi index.php
<?php
phpinfo();
?>

扩展模块

PDO_MYSQL模块(连接mysql,oracle,sqlserver)

[root@why-1 ~]# tar xf PDO_MYSQL-1.0.2.tgz 
[root@why-1 ~]# cd PDO_MYSQL-1.0.2
[root@why-1 PDO_MYSQL-1.0.2]# /usr/local/php/bin/phpize
[root@why-1 PDO_MYSQL-1.0.2]# ./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
[root@why-1 PDO_MYSQL-1.0.2]# make
[root@why-1 PDO_MYSQL-1.0.2]# make install

imagick图片扩展依赖

在上传图片的时候对图片进行处理成不同种的略缩图

[root@why-1 ~]# tar xf ImageMagick.tar.gz 
[root@why-1 ~]# cd ImageMagick-6.5.1-2/
[root@why-1 ImageMagick-6.5.1-2]# ./configure 
[root@why-1 ImageMagick-6.5.1-2]# make
[root@why-1 ImageMagick-6.5.1-2]# make install

imagick图片扩展模块

[root@why-1 imagick-2.3.0]# make && make install
Installing shared extensions:     /usr/local/php5.3.27/lib/php/extensions/no-debug-non-zts-20090626/
[root@why-1 ~]# tar xf imagick-2.3.0.tgz 
[root@why-1 ~]# cd imagick-2.3.0
[root@why-1 imagick-2.3.0]# /usr/local/php/bin/phpize
[root@why-1 imagick-2.3.0]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@why-1 imagick-2.3.0]# make && make install

检验

[root@why-1 imagick-2.3.0]# vim /usr/local/php/lib/php.ini 
修改
extension_dir = "/usr/local/php5.3.27/lib/php/extensions/no-debug-non-zts-20090626/"
添加
extension = pdo_mysql.so
extension = imagick.so

也可以

[root@why-1 ~]# for i in `ls /usr/local/php5.3.27/lib/php/extensions/no-debug-non-zts-20090626/`;do echo "extension = $i" >> /usr/local/php/lib/php.ini ;done

设置主页index.php

[root@why-1 html]# vi index.php
<?php
phpinfo();
?>

重启服务

kill -USR2 `cat /app/logs/php-fpm.pid`

建议

  • 功能性插件一般开发人员不要求可以不考虑安装
  • 优化性插件一般需要安装

apc扩展(20170825更新)

网上更多的APC-3.1.13.tgz包在PHP 5.5.30版本是无法正常编译的。

会出现的问题

[ec2-user@test-web-ads-00 APC-3.1.13]$ make && make install
/bin/sh /home/ec2-user/APC-3.1.13/libtool --mode=compile cc  -I. -I/home/ec2-user/APC-3.1.13 -DPHP_ATOM_INC -I/home/ec2-user/APC-3.1.13/include -I/home/ec2-user/APC-3.1.13/main -I/home/ec2-user/APC-3.1.13 -I/home/ec2-user/web/php/include/php -I/home/ec2-user/web/php/include/php/main -I/home/ec2-user/web/php/include/php/TSRM -I/home/ec2-user/web/php/include/php/Zend -I/home/ec2-user/web/php/include/php/ext -I/home/ec2-user/web/php/include/php/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -c /home/ec2-user/APC-3.1.13/apc.c -o apc.lo 
mkdir .libs
 cc -I. -I/home/ec2-user/APC-3.1.13 -DPHP_ATOM_INC -I/home/ec2-user/APC-3.1.13/include -I/home/ec2-user/APC-3.1.13/main -I/home/ec2-user/APC-3.1.13 -I/home/ec2-user/web/php/include/php -I/home/ec2-user/web/php/include/php/main -I/home/ec2-user/web/php/include/php/TSRM -I/home/ec2-user/web/php/include/php/Zend -I/home/ec2-user/web/php/include/php/ext -I/home/ec2-user/web/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /home/ec2-user/APC-3.1.13/apc.c  -fPIC -DPIC -o .libs/apc.o
/home/ec2-user/APC-3.1.13/apc.c: In function ‘apc_search_paths’:
/home/ec2-user/APC-3.1.13/apc.c:416:20: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
         exec_fname = zend_get_executed_filename(TSRMLS_C);
                    ^
/bin/sh /home/ec2-user/APC-3.1.13/libtool --mode=compile cc  -I. -I/home/ec2-user/APC-3.1.13 -DPHP_ATOM_INC -I/home/ec2-user/APC-3.1.13/include -I/home/ec2-user/APC-3.1.13/main -I/home/ec2-user/APC-3.1.13 -I/home/ec2-user/web/php/include/php -I/home/ec2-user/web/php/include/php/main -I/home/ec2-user/web/php/include/php/TSRM -I/home/ec2-user/web/php/include/php/Zend -I/home/ec2-user/web/php/include/php/ext -I/home/ec2-user/web/php/include/php/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -c /home/ec2-user/APC-3.1.13/php_apc.c -o php_apc.lo 
 cc -I. -I/home/ec2-user/APC-3.1.13 -DPHP_ATOM_INC -I/home/ec2-user/APC-3.1.13/include -I/home/ec2-user/APC-3.1.13/main -I/home/ec2-user/APC-3.1.13 -I/home/ec2-user/web/php/include/php -I/home/ec2-user/web/php/include/php/main -I/home/ec2-user/web/php/include/php/TSRM -I/home/ec2-user/web/php/include/php/Zend -I/home/ec2-user/web/php/include/php/ext -I/home/ec2-user/web/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /home/ec2-user/APC-3.1.13/php_apc.c  -fPIC -DPIC -o .libs/php_apc.o
/bin/sh /home/ec2-user/APC-3.1.13/libtool --mode=compile cc  -I. -I/home/ec2-user/APC-3.1.13 -DPHP_ATOM_INC -I/home/ec2-user/APC-3.1.13/include -I/home/ec2-user/APC-3.1.13/main -I/home/ec2-user/APC-3.1.13 -I/home/ec2-user/web/php/include/php -I/home/ec2-user/web/php/include/php/main -I/home/ec2-user/web/php/include/php/TSRM -I/home/ec2-user/web/php/include/php/Zend -I/home/ec2-user/web/php/include/php/ext -I/home/ec2-user/web/php/include/php/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -c /home/ec2-user/APC-3.1.13/apc_cache.c -o apc_cache.lo 
 cc -I. -I/home/ec2-user/APC-3.1.13 -DPHP_ATOM_INC -I/home/ec2-user/APC-3.1.13/include -I/home/ec2-user/APC-3.1.13/main -I/home/ec2-user/APC-3.1.13 -I/home/ec2-user/web/php/include/php -I/home/ec2-user/web/php/include/php/main -I/home/ec2-user/web/php/include/php/TSRM -I/home/ec2-user/web/php/include/php/Zend -I/home/ec2-user/web/php/include/php/ext -I/home/ec2-user/web/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /home/ec2-user/APC-3.1.13/apc_cache.c  -fPIC -DPIC -o .libs/apc_cache.o
/bin/sh /home/ec2-user/APC-3.1.13/libtool --mode=compile cc  -I. -I/home/ec2-user/APC-3.1.13 -DPHP_ATOM_INC -I/home/ec2-user/APC-3.1.13/include -I/home/ec2-user/APC-3.1.13/main -I/home/ec2-user/APC-3.1.13 -I/home/ec2-user/web/php/include/php -I/home/ec2-user/web/php/include/php/main -I/home/ec2-user/web/php/include/php/TSRM -I/home/ec2-user/web/php/include/php/Zend -I/home/ec2-user/web/php/include/php/ext -I/home/ec2-user/web/php/include/php/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -c /home/ec2-user/APC-3.1.13/apc_compile.c -o apc_compile.lo 
 cc -I. -I/home/ec2-user/APC-3.1.13 -DPHP_ATOM_INC -I/home/ec2-user/APC-3.1.13/include -I/home/ec2-user/APC-3.1.13/main -I/home/ec2-user/APC-3.1.13 -I/home/ec2-user/web/php/include/php -I/home/ec2-user/web/php/include/php/main -I/home/ec2-user/web/php/include/php/TSRM -I/home/ec2-user/web/php/include/php/Zend -I/home/ec2-user/web/php/include/php/ext -I/home/ec2-user/web/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /home/ec2-user/APC-3.1.13/apc_compile.c  -fPIC -DPIC -o .libs/apc_compile.o
/home/ec2-user/APC-3.1.13/apc_compile.c: In function ‘my_copy_class_entry’:
/home/ec2-user/APC-3.1.13/apc_compile.c:755:38: warning: assignment from incompatible pointer type [enabled by default]
         CHECK(dst->trait_precedences =
                                      ^
/home/ec2-user/APC-3.1.13/apc_compile.c:47:25: note: in definition of macro ‘CHECK’
 #define CHECK(p) { if ((p) == NULL) return NULL; }
                         ^
/home/ec2-user/APC-3.1.13/apc_compile.c: In function ‘apc_copy_class_entry_for_execution’:
/home/ec2-user/APC-3.1.13/apc_compile.c:1956:38: warning: assignment from incompatible pointer type [enabled by default]
         CHECK(dst->trait_precedences = 
                                      ^
/home/ec2-user/APC-3.1.13/apc_compile.c:47:25: note: in definition of macro ‘CHECK’
 #define CHECK(p) { if ((p) == NULL) return NULL; }
                         ^
/home/ec2-user/APC-3.1.13/apc_compile.c: In function ‘apc_copy_trait_alias’:
/home/ec2-user/APC-3.1.13/apc_compile.c:2379:12: error: ‘zend_trait_alias’ has no member named ‘function’
     if (src->function) {
            ^
/home/ec2-user/APC-3.1.13/apc_compile.c:2380:18: error: ‘zend_trait_alias’ has no member named ‘function’
         CHECK(dst->function = my_copy_function(NULL, src->function, ctxt TSRMLS_CC));
                  ^
/home/ec2-user/APC-3.1.13/apc_compile.c:47:25: note: in definition of macro ‘CHECK’
 #define CHECK(p) { if ((p) == NULL) return NULL; }
                         ^
/home/ec2-user/APC-3.1.13/apc_compile.c:2380:57: error: ‘zend_trait_alias’ has no member named ‘function’
         CHECK(dst->function = my_copy_function(NULL, src->function, ctxt TSRMLS_CC));
                                                         ^
/home/ec2-user/APC-3.1.13/apc_compile.c:47:25: note: in definition of macro ‘CHECK’
 #define CHECK(p) { if ((p) == NULL) return NULL; }
                         ^
/home/ec2-user/APC-3.1.13/apc_compile.c: In function ‘apc_copy_trait_precedence’:
/home/ec2-user/APC-3.1.13/apc_compile.c:2416:12: error: ‘zend_trait_precedence’ has no member named ‘function’
     if (src->function) {
            ^
/home/ec2-user/APC-3.1.13/apc_compile.c:2417:18: error: ‘zend_trait_precedence’ has no member named ‘function’
         CHECK(dst->function = my_copy_function(NULL, src->function, ctxt TSRMLS_CC));
                  ^
/home/ec2-user/APC-3.1.13/apc_compile.c:47:25: note: in definition of macro ‘CHECK’
 #define CHECK(p) { if ((p) == NULL) return NULL; }
                         ^
/home/ec2-user/APC-3.1.13/apc_compile.c:2417:57: error: ‘zend_trait_precedence’ has no member named ‘function’
         CHECK(dst->function = my_copy_function(NULL, src->function, ctxt TSRMLS_CC));
                                                         ^
/home/ec2-user/APC-3.1.13/apc_compile.c:47:25: note: in definition of macro ‘CHECK’
 #define CHECK(p) { if ((p) == NULL) return NULL; }
                         ^
make: *** [apc_compile.lo] Error 1

这是因为这个版本不支持php5.5,更多可以参考http://git.php.net/?p=pecl/caching/apc.git;a=shortlog,可以看到新版本已经支持php5.5,使用新版本即可

[ec2-user@test-web-ads-00 ~]$ tar xf apc-6a90406.tar.gz 
[ec2-user@test-web-ads-00 ~]$ cd apc-6a90406
apc-6a90406/        apc-6a90406.tar.gz  
[ec2-user@test-web-ads-00 ~]$ cd apc-6a90406
[ec2-user@test-web-ads-00 apc-6a90406]$ /home/ec2-user/web/php/bin/phpize 
Configuring for:
PHP Api Version:         20121113
Zend Module Api No:      20121212
Zend Extension Api No:   220121212
[ec2-user@test-web-ads-00 apc-6a90406]$  ./configure --with-php-config=/home/ec2-user/web/php/bin/php-config
make && make install

[ec2-user@test-web-ads-00 apc-6a90406]$ ll /home/ec2-user/web/php/lib/php/extensions/no-debug-non-zts-20121212/
total 12332
-rwxr-xr-x 1 ec2-user ec2-user  799963 Aug 22 18:32 apc.so
-rw-rw-r-- 1 ec2-user ec2-user  300786 May  8 11:47 igbinary.so
-rwxr-xr-x 1 ec2-user ec2-user 1341682 Jun 23 11:13 imagick.so
-rw-rw-r-- 1 ec2-user ec2-user  256949 May  8 11:47 json.so
-rwxr-xr-x 1 ec2-user ec2-user  374176 May  8 11:47 memcached.so
-rw-rw-r-- 1 ec2-user ec2-user  501501 May  8 11:47 memcache.so
-rw-rw-r-- 1 ec2-user ec2-user 1952331 May  8 11:47 mongo.so
-rwxr-xr-x 1 ec2-user ec2-user 1205076 May  8 11:47 opcache.a
-rwxr-xr-x 1 ec2-user ec2-user  564590 May  8 11:47 opcache.so
-rw-rw-r-- 1 ec2-user ec2-user 1819509 May  8 11:47 redis.so
-rw-rw-r-- 1 ec2-user ec2-user 2090881 May  8 11:47 swoole.so
-rwxr-xr-x 1 root     root       97145 Aug 17 14:28 xhprof.so
-rwxr-xr-x 1 ec2-user ec2-user 1294601 May  8 11:47 yaf.so

添加模块

[ec2-user@test-web-ads-00 apc-6a90406]$ php -m | grep -i apc
[ec2-user@test-web-ads-00 apc-6a90406]$ vi /home/ec2-user/web/php/etc/php.ini
[PHP]

;;;;;;;;;;;;;;;;;;;
; About php.ini   ;
;;;;;;;;;;;;;;;;;;;
; PHP's initialization file, generally called php.ini, is responsible for
; configuring many of the aspects of PHP's behavior.
extension = "imagick.so"
extension = "apc.so"

检查

[ec2-user@test-web-ads-00 apc-6a90406]$ php -m | grep -i apc
apc

可能遇到的问题

如果遇到以下情况
[ec2-user@afp-wap-A-000 apc-6a90406]$ /home/ec2-user/web/php/bin/phpize 
Configuring for:
PHP Api Version:         20121113
Zend Module Api No:      20121212
Zend Extension Api No:   220121212
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

解决方式

[ec2-user@afp-wap-A-001 apc-6a90406]$ sudo yum install -y autoconf