您的位置 首页 技术

如何让PHP 7运行更加神速

导读 PHP 7 比5.x 快上很多,即使只有单纯的版本升级就已经很有感,不过大家还是希望它变得越来越快,这时再做些小调整就会更有fu,Let's try it! 事前准…

导读 PHP 7 比5.x 快上很多,即使只有单纯的版本升级就已经很有感,不过大家还是希望它变得越来越快,这时再做些小调整就会更有fu,Let's try it!

事前准备

说到PHP 7,那一定跑不了LAMP 或是LEMP,请先准备好底层服务的安装。

  • [CentOS 7] 整合Apache、MySQL、PHP 7 组成LAMP Server
  • [CentOS 7] 整合Nginx、MariaDB、PHP 7 组成LEMP Server

以前我们要让PHP加快处理速度,通常会配合APC、eAccelerator、XCache的任一个来使用;现在忘了它们吧,就从现在起开始改用OPcache来实作,它是PHP 7开发者之一的惠新宸协力开发的PHP支援模组。 这次实作以LEMP架构为主,套件库是用Remi的版本,别忘了要依各位实际的环境来修改路径及设定值。

相关学习推荐:PHP 编程从入门到精通

开始设定

安装OPcache套件。

sudo yum -y install php70-php-opcache

how-to-improve-php7-performance-011

编辑主设定档。

sudo vi /etc/opt/remi/php70/php.ini

how-to-improve-php7-performance-012

加上这些参数。

zend_extension=opcache.so opcache.enable=1 opcache.enable_cli=1opcache.file_cache=/ home/opcache opcache.huge_code_pages=1

how-to-improve-php7-performance-013

启动Huge Pages,它是一种大型暂存分页机制,详细说明请参阅The Linux Kernel Archives -Huge Pages,在我的机器上测试结果改到512就够了。

sudo sysctl -w vm.nr_hugepages=512

how-to-improve-php7-performance-014

建立OPcache专用目录。

sudo mkdir /home/opcache sudo chown nginx:nginx /home/opcache

how-to-improve-php7-performance-015

重新启动PHP-FPM,这边就会看到OPcache已经启动了。

sudo systemctl restart php70-php-fpm

how-to-improve-php7-performance-016

另外,我们还可以加装memcached,顾名思义它就是使用记忆体来当快取,加速系统的运作。

sudo yum -y install memcached

how-to-improve-php7-performance-021

编辑主程式档。

sudo vi /etc/sysconfig/memcached

how-to-improve-php7-performance-022

参数不多,请依需求修改。

PORT - 端口,别忘了开防火墙。MAXCONN - 总连接数。CACHESIZE - 内存使用量,单位是KB。PORT="11211"USER="memcached"MAXCONN="1024"CACHESIZE="1024"OPTIONS=""

how-to-improve-php7-performance-023

启动memcached,并让它在开机后自动启动。

sudo systemctl restart memcached sudo systemctl enable memcached

how-to-improve-php7-performance-024

开放防 火墙

sudo firewall-cmd --permanent --zone=public --add-port=11211/tcp

how-to-improve-php7-performance-025

再安装memcached for PHP的支援模组。

sudo yum -y install php70-php-pecl-memcached

how-to-improve-php7-performance-026

重新启动PHP-FPM。

sudo systemctl restart php70-php-fpm

how-to-improve-php7-performance-027

最后看一下phpinfo(); 函数的显示结果,出现memcached 的段落就代表成功了。

how-to-improve-php7-performance-028

实测结果

这边直接引用对岸的网友的资料,在OneAPM -使用PHP 7给Web应用加速这篇文章里,他测试了Wordpress 4.1.1、Drupal 8、phpBB 3.1.3、MediaWiki 1.24.1、Opencart 2.0.2.0 、WardrobeCMS 1.2.0、Geeklog 2.1.0、Magento 1.9.1.1、Traq 3.5.2、Cachet、Moodle 2.9-dev、ZenCart 1.5.4等12种套件的比较结果。 以Wordpress 4.1.1为例,我们可以看到PHP 7比起5.3 ~ 5.6的读取速度(Read)及延迟时间(Latency)都有大幅改善。

how-to-improve-php7-performance-051

以上就是如何让PHP 7运行更加神速的详细内容,更多请关注24课堂在线网其它相关文章!

本文来自网络,不代表24小时课堂在线立场,转载请注明出处:https://www.24ketang.cn/88935.html

为您推荐

返回顶部