记录一次 LNMP 中的 php7 安装动态扩展出现的问题及其解决方法

发布于 2018-04-08  392 次阅读


首先介绍下 opcache

Zend OPcache 通过 opcode 缓存和优化提供更快的 PHP 执行过程。它将预编译的脚本文件存储在共享内存中供以后使用,从而避免了从磁盘读取代码并进行编译的时间消耗。同时,它还应用了一些代码优化模式,使得代码执行更快。

字面意思,启用了 opcache,你的站点会运行更快!

虽说 php7 自带 opcache,但因为之前编译安装 php7 的时候没添加"--enable-opcache"参数,所以只能以动态扩展的方法安装 opcache 了

编译安装 opcache

进入 php 安装包的 opcache 目录

[root@LZWP:/usr/local/src/php-7.2.4/ext/opcache#] ls

config.m4              shared_alloc_shm.c             zend_accelerator_debug.lo   ZendAccelerator.o               zend_persist_calc.o

config.w32             shared_alloc_shm.lo            zend_accelerator_debug.o    zend_accelerator_util_funcs.c   zend_persist.h

CREDITS                shared_alloc_shm.o             ZendAccelerator.h           zend_accelerator_util_funcs.h   zend_persist.lo

opcache.la             shared_alloc_win32.c           zend_accelerator_hash.c     zend_accelerator_util_funcs.lo  zend_persist.o

Optimizer              tests                          zend_accelerator_hash.h     zend_accelerator_util_funcs.o   zend_shared_alloc.c

README                 zend_accelerator_blacklist.c   zend_accelerator_hash.lo    zend_file_cache.c               zend_shared_alloc.h

shared_alloc_mmap.c    zend_accelerator_blacklist.h   zend_accelerator_hash.o     zend_file_cache.h               zend_shared_alloc.lo

shared_alloc_mmap.lo   zend_accelerator_blacklist.lo  ZendAccelerator.lo          zend_file_cache.lo              zend_shared_alloc.o

shared_alloc_mmap.o    zend_accelerator_blacklist.o   zend_accelerator_module.c   zend_file_cache.o

shared_alloc_posix.c   ZendAccelerator.c              zend_accelerator_module.h   zend_persist.c

shared_alloc_posix.lo  zend_accelerator_debug.c       zend_accelerator_module.lo  zend_persist_calc.c

shared_alloc_posix.o   zend_accelerator_debug.h       zend_accelerator_module.o   zend_persist_calc.lo

使用 php 自带的 phpize 命令生成./configure,但是有提示错误

[root@LZWP:/usr/local/src/php-7.2.4/ext/opcache#] /usr/local/php-fpm/bin/phpize

Configuring for:

PHP Api Version:         20170718

Zend Module Api No:      20170718

Zend Extension Api No:   320170718

Cannot find autoconf. Please check your autoconf installation and the

$PHP_AUTOCONF environment variable. Then, rerun this script.

yum 安装"m4.x86_64"和"autoconf.noarch",然后继续 phpize

[root@LZWP:/usr/local/src/php-7.2.4/ext/opcache#] yum -y install m4.x86_64

[root@LZWP:/usr/local/src/php-7.2.4/ext/opcache#] yum -y install autoconf.noarch

[root@LZWP:/usr/local/src/php-7.2.4/ext/opcache#] /usr/local/php-fpm/bin/phpize

Configuring for:

PHP Api Version:         20170718

Zend Module Api No:      20170718

Zend Extension Api No:   320170718

使用"ls"可以见到,"configure"文件出现了,之后就编译安装 opcache,注意编译安装完之后提示的目录路径,里面的"opcache.so"文件正是我们需要的!

[root@LZWP:/usr/local/src/php-7.2.4/ext/opcache#] ls

acinclude.m4     missing                shared_alloc_win32.c           zend_accelerator_hash.o         zend_persist.c

aclocal.m4       mkinstalldirs          tests                          ZendAccelerator.lo              zend_persist_calc.c

autom4te.cache   opcache.la             zend_accelerator_blacklist.c   zend_accelerator_module.c       zend_persist_calc.lo

build            Optimizer              zend_accelerator_blacklist.h   zend_accelerator_module.h       zend_persist_calc.o

config.guess     README                 zend_accelerator_blacklist.lo  zend_accelerator_module.lo      zend_persist.h

config.h.in      run-tests.php          zend_accelerator_blacklist.o   zend_accelerator_module.o       zend_persist.lo

config.m4        shared_alloc_mmap.c    ZendAccelerator.c              ZendAccelerator.o               zend_persist.o

config.sub       shared_alloc_mmap.lo   zend_accelerator_debug.c       zend_accelerator_util_funcs.c   zend_shared_alloc.c

configure        shared_alloc_mmap.o    zend_accelerator_debug.h       zend_accelerator_util_funcs.h   zend_shared_alloc.h

configure.ac     shared_alloc_posix.c   zend_accelerator_debug.lo      zend_accelerator_util_funcs.lo  zend_shared_alloc.lo

config.w32       shared_alloc_posix.lo  zend_accelerator_debug.o       zend_accelerator_util_funcs.o   zend_shared_alloc.o

CREDITS          shared_alloc_posix.o   ZendAccelerator.h              zend_file_cache.c

install-sh       shared_alloc_shm.c     zend_accelerator_hash.c        zend_file_cache.h

ltmain.sh        shared_alloc_shm.lo    zend_accelerator_hash.h        zend_file_cache.lo

Makefile.global  shared_alloc_shm.o     zend_accelerator_hash.lo       zend_file_cache.o

[root@LZWP:/usr/local/src/php-7.2.4/ext/opcache#] ./configure --with-php-config=/usr/local/php-fpm/bin/php-config --enable-opcache

[root@LZWP:/usr/local/src/php-7.2.4/ext/opcache#] make

[root@LZWP:/usr/local/src/php-7.2.4/ext/opcache#] make install

Installing shared extensions:     /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20170718/

把 opcache 动态扩展及其配置参数添加到"php.ini"配置文件中

[root@LZWP:/usr/local/src/php-7.2.4/ext/opcache#] vim /usr/local/php-fpm/etc/php.ini

[opcache]

zend_extension="/usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20170718/opcache.so"

opcache.force_restart_timeout=3600

opcache.memory_consumption=128

opcache.interned_strings_buffer=8

opcache.max_accelerated_files=4000

opcache.revalidate_freq=60

opcache.fast_shutdown=1

opcache.enable=1

opcache.enable_cli=1

重启 php-fpm

[root@LZWP:/usr/local/src/php-7.2.4/ext/opcache#] service php-fpm restart

Gracefully shutting down php-fpm . done

Starting php-fpm  done

[root@LZWP:/usr/local/src/php-7.2.4/ext/opcache#] ps -aux | grep php

root     24241  0.0  0.6 1170896 6384 ?        Ss   11:24   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)

php-fpm  24242  0.0  0.6 1172980 6544 ?        S    11:24   0:00 php-fpm: pool www

php-fpm  24243  0.0  0.6 1172980 6544 ?        S    11:24   0:00 php-fpm: pool www

php-fpm  24244  0.0  0.6 1172980 6544 ?        S    11:24   0:00 php-fpm: pool www

php-fpm  24245  0.0  0.6 1172980 6548 ?        S    11:24   0:00 php-fpm: pool www

root     24249  0.0  0.0 112652   964 pts/1    R+   11:24   0:00 grep --color=auto php

测试

[root@LZWP:/data/www/wordpress#] vim test.php

<?php

phpinfo();

?>

客户端测试,是已经启动 opcache 了!

记录一次 LNMP 中的 php7 安装动态扩展出现的问题及其解决方法