Table of Contents
PHP 7.0发布,网上关于新版的介绍很多,介于 7.0 在正式发布之前已经发过若干个 Beta、8个 RC,应该不会出现重大问题。今日我将一台机器升级至 PHP 7.0 并将有关信息记录如下。
安装 PHP7.0与扩展
Redis 安装方法
Memcached 安装方法
异常处理与解决
监控与调优
(本文作者系 OneAPM 用户,授权 OneAPM 官方博客转发)
OneAPM for PHP 能够深入到所有 PHP 应用内部完成应用性能管理和监控,包括代码级别性能问题的可见性、性能瓶颈的快速识别与追溯、真实用户体验监控、服务器监控和端到端的应用性能管理。想阅读更多技术文章,请访问 OneAPM官方技术博客 。
Home Backend Development PHP Tutorial PHP 7.0 安装使用与性能监测!

PHP 7.0 安装使用与性能监测!

Jun 23, 2016 pm 01:20 PM

PHP 7.0发布,网上关于新版的介绍很多,介于 7.0 在正式发布之前已经发过若干个 Beta、8个 RC,应该不会出现重大问题。今日我将一台机器升级至 PHP 7.0 并将有关信息记录如下。

本人使用 Ubuntu 12.04 LTS,在网上已经找到 7.0 正式版的 ppa,所以不需要编译,使用如下命令可直接安装。

安装 PHP7.0与扩展

sudo add-apt-repository ppa:ondrej/php-7.0 sudo apt-get update sudo apt-get install php7.0-fpm php7.0-cli php7.0-common php7.0-json php7.0-mysql php7.0-opcache php7.0-curl 由于 Memcached、Redis 扩展并没有在 pecl 发布支持 PHP7 的最新版本,所以需要到 Github 找到 PHP7 的分支进行手动编译安装。

redis、memcached的github地址如下

https://github.com/phpredis/phpredis/ https://github.com/rlerdorf/php-memcached

Redis 安装方法

git clone https://github.com/phpredis/phpredis/ cd phpredis
git checkout php7
phpize
./configure make
ssudo make install

Memcached 安装方法

Memcached 需要先下载 libmemecached 库才能正常编译。

wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz tar -zxvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18
./configure make
sudo make install
sudo apt-get install pkg-config
git clone https://github.com/rlerdorf/php-memcached.git
cd php-memcached
git checkout php7
phpize
./configure make
sudo make install

自己编译的这2个扩展需要手动在配置文件里加载

sudo touch /etc/php/mods-available/redis.ini sudo touch /etc/php/mods-available/memcached.ini

并将两个文件内容写上

extension=redis.so extension=memcached.so cd /etc/php/7.0/fpm/conf.d sudo ln -s /etc/php/mods-available/redis.ini ./ sudo ln -s /etc/php/mods-available/memcached.ini ./

如果命令行下需要启用扩展,同样需要在 cli/conf.d 目录下将其链接过去。 最后重启服务器

sudo service php7.0-fpm restart

配置文件的调整 由于 PHP7.0 最大的改进是性能,所以务必要启用 opcache 保证其能发挥最大作用。 将 php.ini 的如下配置启用。

opcache.enable=1 opcache.enable_cli=1
opcache.file_cache=/tmp
opcache.error_log=/var/log/opcache_errors.log

ppa 安装的包默认 error display 是 off 的。 而且 error log 是注释的,意味着出现问题时查看不到任何信息。

因此请写入如下配置

error_log=/var/log/php_errors.log sudo chown www-data.www-data /var/log/php_errors.log

本人安装的是 Nginx 服务器,请确保用户数组更改为与自己 webserver 一样的,否则还是不会出现任何提示。 opcache_errors.log 文件同样如此。

关于 opcache 的更多内容可以访问这里查看 http://www.laruence.com/2015/12/04/3086.html

异常处理与解决

在配置完成后,就需要实际的将程序跑一下了。目前将老系统转移到 EN PHP7.0 后,第一个错误就是

09-Dec-2015 12:27:48 Asia/Chongqing] PHP Fatal error: Uncaught Error: Call to undefined function set_magic_quotes_runtime() in /init.php:46

已经不再存在set magic quotes_runtime 这个函数了。如果要兼容的话需要加上判断

if(PHP_VERSION_ID < 70000){ set_magic_quotes_runtime(); }

监控与调优

我在系统里安装了 OneAPM 提供的 Agent。这样可以实时监测到整个系统的运行情况。其他版本的 Agent 官方网站已经提供了下载。截止本文落笔,PHP 7.0版本官方提供了一个下载地址是: https://oneapm.kf5.com/attachments/download/366552/0015667f0036f47c827fcb8fcbfbc79/

在这之前更多人会使用 xhprof 来检测和优化系统,但是 xhprof 对整体的程序性能采集样本无法很好的归纳,也没有很好的可视化曲线图和 Web 事务跟踪,导致在短时间内很难对系统瓶颈进行评估。

所以我使用 OneAPM 的 PHP Agent 来完成这些工作,OneAPM 同样使用定时采样定时汇报的方式来收集性能信息,并且官方宣称耗费资源小于5%。不过对于使用性能提升数倍的 PHP7.0 来部署的话这些损耗可以忽略不计,而且本人只在集群若干机器内部署了一台。

下面介绍基本的性能分析和故常排查方法。

比如可以在 dashboard 中查看到具体某个时间段整个系统的稳定程度,我们在图上看到了一个异常波峰,时间在早上6点左右,通过列表筛选器移除 WEB External 后看图。

其他业务都很正常,执行到最后 PHP 层,平均时间也只用了 10ms 左右。回到上图点击波峰的指示器可以看到具体明细。

当打开详情时可以明显看到,原来是微信的接口在6点钟抽了。同样该页面还可以监控到第三方服务调用的响应情况。比如 217ms 的 api.hitokoto.us 服务。

再简单看一个 SQL 缓慢的监控。

通过 Web 事务的响应时间占比查看到一个脚本执行时间相对过长,通过上图可以看到数据库查询占了579ms

通过切换到详情页面,可以看到整个脚本的调用过程,最终发现是程序 mysqli.php:88 行执行的查询占用了过长的时间。

以上只是通过 OneAPM 持续检查程序稳定性的一个基本方法。

程序在日常运行中由于受到的访问量不同,很有可能在某个时间点上出现大面积的延迟,比如并发突然增高或访问某一部分接口的比例突然过高,而平时Apdex 指标却看起来非常漂亮,那么这个时候通过 OneAPM 就很容易发现程序中影响性能的部分,从而继续改进或优化代码。

(本文作者系 OneAPM 用户,授权 OneAPM 官方博客转发)

OneAPM for PHP 能够深入到所有 PHP 应用内部完成应用性能管理和监控,包括代码级别性能问题的可见性、性能瓶颈的快速识别与追溯、真实用户体验监控、服务器监控和端到端的应用性能管理。想阅读更多技术文章,请访问 OneAPM官方技术博客 。

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1271
29
C# Tutorial
1251
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles