Table of Contents
参考资料
Home Backend Development PHP Tutorial 快还要更快,让PHP 7 运行更加神速

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

Jun 23, 2016 pm 01:05 PM

导读 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
    Copy after login

    编辑主设定档。

    sudo vi /etc/opt/remi/php70/php.ini
    Copy after login

    加上这些参数。

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

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

    sudo sysctl -w vm.nr_hugepages=512
    Copy after login

    建立OPcache专用目录。

    sudo mkdir /home/opcache sudo chown nginx:nginx /home/opcache
    Copy after login

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

    sudo systemctl restart php70-php-fpm
    Copy after login
    Copy after login

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

    sudo yum -y install memcached
    Copy after login

    编辑主程式档。

    sudo vi /etc/sysconfig/memcached
    Copy after login

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

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

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

    sudo systemctl restart memcached sudo systemctl enable memcached
    Copy after login

    开放防 火墙

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

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

    sudo yum -y install php70-php-pecl-memcached
    Copy after login

    重新启动PHP-FPM。

    sudo systemctl restart php70-php-fpm
    Copy after login
    Copy after login

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

    实测结果

    这边直接引用对岸的网友的资料,在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)都有大幅改善。

    参考资料

  • 风雪之隅- 让你的PHP7更快之Hugepage
  • 风雪之隅- 让PHP7达到最高性能的几个Tips
  • OneAPM - 使用PHP 7 给Web 应用加速
  • liquidweb - How to Install Memcached on CentOS 7
  • Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

    Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

    Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

    JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

    How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

    Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

    Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

    The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

    How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

    How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

    Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

    Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

    How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

    How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

    See all articles