• 快还要更快,让PHP 7 运行更加神速


    导读 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的版本,别忘了要依各位实际的环境来修改路径及设定值。

    开始设定

    安装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

    参考资料

    • 风雪之隅- 让你的PHP7更快之Hugepage
    • 风雪之隅- 让PHP7达到最高性能的几个Tips
    • OneAPM - 使用PHP 7 给Web 应用加速
    • liquidweb - How to Install Memcached on CentOS 7

    免费提供最新Linux技术教程书籍,为开源技术爱好者努力做得更多更好:https://www.linuxprobe.com/

  • 相关阅读:
    扫雷游戏:C++生成一个扫雷底板
    为《信息学奥赛一本通》平反(除了OJ很差和代码用宋体)
    关于最大公约数欧几里得算法辗转相除及最小公倍数算法拓展C++学习记录
    【Python】如何用较快速度用Python程序增加博客访问量——CSDN、博客园、知乎(应该包括简书)
    STL学习笔记之next_permutation函数
    C++集合容器STL结构Set
    11111111111
    express + mysql实践
    express实践
    ws:一个 Node.js WebSocket 库
  • 原文地址:https://www.cnblogs.com/linuxprobe/p/5452167.html
Copyright © 2020-2023  润新知