前一段时间为了搞 http2.0 把 nginx 升级到了最新版 1.11.10,今天闲来无事准备把 php 也升级一下。先去官网看一下最新版是 7.1.5,并且找到了下载地址。接下来开始操作
升级php
wget http://cn2.php.net/get/php-7.1.5.tar.gz/from/this/mirror #下载
tar -xvf mirror #解压
然后开始配置
./configure
--prefix=/server/php
--exec-prefix=/server/php
--bindir=/server/php/bin
--sbindir=/server/php/sbin
--includedir=/server/php/include
--libdir=/server/php/lib/php
--mandir=/server/php/php/man
--with-config-file-path=/server/php/etc
--with-mysql-sock=/var/run/mysql/mysql.sock
--with-mcrypt=/usr/include
--with-mhash
--with-openssl
--with-mysql=shared,mysqlnd
--with-mysqli=shared,mysqlnd
--with-pdo-mysql=shared,mysqlnd
--with-gd
--with-iconv
--with-zlib
--enable-zip
--enable-inline-optimization
--disable-debug
--disable-rpath
--enable-shared
--enable-xml
--enable-bcmath
--enable-shmop
--enable-sysvsem
--enable-mbregex
--enable-mbstring
--enable-ftp
--enable-gd-native-ttf
--enable-pcntl
--enable-sockets
--with-xmlrpc
--enable-soap
--without-pear
--with-gettext
--enable-session
--with-curl
--with-jpeg-dir
--with-freetype-dir
--enable-opcache
--enable-fpm
--enable-fastcgi
--with-fpm-user=nginx
--with-fpm-group=nginx
--without-gdbm
--disable-fileinfo
因为考虑到后面 fpm 的配置,所以我选择直接覆盖安装,指定 --prefix 的参数为原有 php 这样的话就不需要再修改其他参数。 接下来开始编译。
make clean && make && make install
编译之后使用 php -v 查看版本,提示如下
PHP Warning: PHP Startup: Unable to load dynamic library '/server/php/lib/php/extensions/no-debug-non-zts-20160303/redis.so' - /server/php/lib/php/extensions/no-debug-non-zts-20160303/redis.so: cannot open shared object file: No such file or directory in Unknown on line 0
PHP 7.1.5 (cli) (built: May 22 2017 16:06:29) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
发现 php 已经升级成功,但是 redis 扩展出现错误。搜索了一下得知原来装的 redis 扩展不支持 php7,那就需要重新安装redis 的扩展。 ##redis扩展安装 搜索找到了一个支持 php7 的扩展,地址是:https://github.com/phpredis/phpredis/archive/php7.zip。
wget https://github.com/phpredis/phpredis/archive/php7.zip
unzip php7.zip
/server/php/bin/phpize
./configure
# 如果 php 没有加入系统环境变量则编译命令需要带上 php 的 config 地址 ./configure --with-php-config=/server/php/bin/php-config
make && make install
编译完成后会得到如下反馈
Installing shared extensions: /server/php/lib/php/extensions/no-debug-non-zts-20131226/
我们将其拷贝到 php 扩展目录
cp /server/php/lib/php/extensions/no-debug-non-zts-20131226/redis.so /server/php/lib/php/extensions/
因为之前 php.ini 已经添加过 redis 扩展,就不需要再修改了。如果没有添加则需要在 php.ini 中添加以下配置项
vim /etc/php.ini
# add
[Redis]
extension=redis.so
然后重启 php-fpm 服务,正常!
mysql 链接方式修改
重启后打开网站却报了一个 database server error 错误。发现原来的数据库连接是 Mysql 方式,php7 已经弃用了,改为 Pdo_Mysql 方式即可。
开启 OPcache
OPcache 虽然名字中带 cache,但是和 memcache、redis 并不是一个东西。OPcache通过将 PHP 脚本预编译的字节码存储到共享内存中来提升 PHP 的性能, 存储预编译字节码的好处就是 省去了每次加载和解析 PHP 脚本的开销。简单来说就是开启 OPcache 之后会将 php 脚本编译后的字节码缓存,针对后面的请求可以不去加载和解析脚本,大大的提升性能。所以在生产环境中开启 OPcache 还是非常必要的。
打开 php.ini 找到 [opcache],设置格式如下。
[opcache]
; dll地址
zend_extension=php_opcache.dll
; 开关打开
opcache.enable=1
; 开启CLI
opcache.enable_cli=1
; 可用内存, 酌情而定, 单位为:Mb
opcache.memory_consumption=528
; Zend Optimizer + 暂存池中字符串的占内存总量.(单位:MB)
opcache.interned_strings_buffer=8
; 对多缓存文件限制, 命中率不到 100% 的话, 可以试着提高这个值
opcache.max_accelerated_files=10000
; Opcache 会在一定时间内去检查文件的修改时间, 这里设置检查的时间周期, 默认为 2, 定位为秒
opcache.revalidate_freq=60
; 打开快速关闭, 打开这个在PHP Request Shutdown的时候回收内存的速度会提高
opcache.fast_shutdown=1
这里主要说一下 opcache.revalidate_freq 这个配置,设置为 60 时,每隔 60 秒会去检测一次文件变动,如果变动则重新加载。理论来说时间越大、性能越好(因为不需要频繁去检测文件)。但是会出现 php 脚本文件修改后,过了 60 秒才会生效的情况。
效果
因为博客对内容做了 redis 缓存和 html 压缩。所以对同一地址分析了一下。
可以看到 redis 缓存和 html 压缩没有命中时,响应时间是 88ms。
redis 缓存和 html 压缩命中时,响应时间是 36ms。
发表评论
0 条评论