导读 | PHP 7 比5.x 快上很多,即使只有单纯的版本升级就已经很有感,不过大家还是希望它变得越来越快,这时再做些小调整就会更有fu,Let's try it! |
说到PHP 7,那一定跑不了LAMP 或是LEMP,请先准备好底层服务的安装。
以前我们要让PHP加快处理速度,通常会配合APC、eAccelerator、XCache的任一个来使用;现在忘了它们吧,就从现在起开始改用OPcache来实作,它是PHP 7开发者之一的惠新宸协力开发的PHP支援模组。 这次实作以LEMP架构为主,套件库是用Remi的版本,别忘了要依各位实际的环境来修改路径及设定值。
安装OPcache套件。
sudo yum -y install php70-php-opcache
![how-to-improve-php7-performance-011](https://i-blog.csdnimg.cn/blog_migrate/3e7721503455903c11f879739e6176c9.jpeg)
编辑主设定档。
sudo vi /etc/opt/remi/php70/php.ini
![how-to-improve-php7-performance-012](https://i-blog.csdnimg.cn/blog_migrate/0cf46d203309c570a5f8636719b2cf99.jpeg)
加上这些参数。
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](https://i-blog.csdnimg.cn/blog_migrate/423699888aaf8aeb5301c9891677c883.jpeg)
启动Huge Pages,它是一种大型暂存分页机制,详细说明请参阅The Linux Kernel Archives - Huge Pages,在我的机器上测试结果改到512就够了。
sudo sysctl -w vm.nr_hugepages=512
![how-to-improve-php7-performance-014](https://i-blog.csdnimg.cn/blog_migrate/f4e515d01569853cf0b3b35393632608.jpeg)
建立OPcache专用目录。
sudo mkdir /home/opcache sudo chown nginx:nginx /home/opcache
![how-to-improve-php7-performance-015](https://i-blog.csdnimg.cn/blog_migrate/a249242787d66dbb389c20690f75ab46.jpeg)
重新启动PHP-FPM,这边就会看到OPcache已经启动了。
sudo systemctl restart php70-php-fpm
![how-to-improve-php7-performance-016](https://i-blog.csdnimg.cn/blog_migrate/382a5e7d41345df5d68a1b7d3c4debf9.jpeg)
另外,我们还可以加装memcached,顾名思义它就是使用记忆体来当快取,加速系统的运作。
sudo yum -y install memcached
![how-to-improve-php7-performance-021](https://i-blog.csdnimg.cn/blog_migrate/71ddc60d5ae87c4996c077e2372bd8d8.jpeg)
编辑主程式档。
sudo vi /etc/sysconfig/memcached
![how-to-improve-php7-performance-022](https://i-blog.csdnimg.cn/blog_migrate/e585d9341ba052d60374b170172a8efe.jpeg)
参数不多,请依需求修改。
PORT - 端口,别忘了开防火墙。 MAXCONN - 总连接数。 CACHESIZE - 内存使用量,单位是KB。 PORT="11211" USER="memcached" MAXCONN="1024" CACHESIZE="1024" OPTIONS=""
![how-to-improve-php7-performance-023](https://i-blog.csdnimg.cn/blog_migrate/1020c863604fc58008582b1bf9e5b682.jpeg)
启动memcached,并让它在开机后自动启动。
sudo systemctl restart memcached sudo systemctl enable memcached
![how-to-improve-php7-performance-024](https://i-blog.csdnimg.cn/blog_migrate/4f0da4e4fc8194ae22b52f8d9e0054c7.jpeg)
开放防 火墙
sudo firewall-cmd --permanent --zone=public --add-port=11211/tcp
![how-to-improve-php7-performance-025](https://i-blog.csdnimg.cn/blog_migrate/8b6c276fddf7c0ecebd5b4acd2068084.jpeg)
再安装memcached for PHP的支援模组。
sudo yum -y install php70-php-pecl-memcached
![how-to-improve-php7-performance-026](https://i-blog.csdnimg.cn/blog_migrate/30473ee7543a0b90a098b3bdf466fc84.jpeg)
重新启动PHP-FPM。
sudo systemctl restart php70-php-fpm
![how-to-improve-php7-performance-027](https://i-blog.csdnimg.cn/blog_migrate/6591e639eddb9994b5f92d509ff8e7a7.jpeg)
最后看一下phpinfo(); 函数的显示结果,出现memcached 的段落就代表成功了。
![how-to-improve-php7-performance-028](https://i-blog.csdnimg.cn/blog_migrate/79c1e892adb38b00c8b2b58293ab59e0.jpeg)
这边直接引用对岸的网友的资料,在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](https://i-blog.csdnimg.cn/blog_migrate/2e60f2b6cba80f98e08e0900a5154370.jpeg)
参考资料
- 风雪之隅- 让你的PHP7更快之Hugepage
- 风雪之隅- 让PHP7达到最高性能的几个Tips
- OneAPM - 使用PHP 7 给Web 应用加速
- liquidweb - How to Install Memcached on CentOS 7
本文转载自:http://www.linuxprobe.com/php-7/
免费提供最新Linux技术教程书籍,为开源技术爱好者努力做得更多更好:http://www.linuxprobe.com/