让PHP应用性能维持在更高水平,你怎么做
<span style="color: black;">点击进入“PHP开源社区” </span><span style="color: black;">免费获取进阶面试、文档、视频资源</span><span style="color: black;">默认<span style="color: black;">状况</span>下安装的PHP类似于在百货商店中购买的常规软件包,但它非常合适,但并不完美。调优的PHP就像是量身定制的装备。</span><span style="color: black;"><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">然则</span>,应该<span style="color: black;">重视</span>的是,调优PHP只是<span style="color: black;">加强</span>PHP性能和效率的一种手段,它与不良的代码和无响应的API调用无关。</p>
</span>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">php.ini文件</span></strong></span></h3><span style="color: black;">PHP 解释器在 </span><span style="color: black;">php.ini</span><span style="color: black;"> 文件中配置和调优,此文件的位置在<span style="color: black;">区别</span>的操作系统中是<span style="color: black;">区别</span>的,并且常规命令行对应 </span><span style="color: black;">php.ini</span><span style="color: black;"> 和 PHP-FPM 对应的 </span><span style="color: black;">php.ini</span><span style="color: black;"> 文件是<span style="color: black;">掰开</span>的。在<span style="color: black;">这儿</span>,<span style="color: black;">咱们</span>假设配置PHP-FPM <span style="color: black;">对应于</span></span><span style="color: black;">php.ini</span><span style="color: black;">,<span style="color: black;">然则</span>下面讲的优化<span style="color: black;">办法</span>适用于所有 </span><span style="color: black;">php.ini</span><span style="color: black;">。</span><span style="color: black;">注:<span style="color: black;">咱们</span><span style="color: black;">首要</span>应该<span style="color: black;">运用</span> PHP Iniscan 工具扫描 php.ini,<span style="color: black;">检测</span><span style="color: black;">运用</span>了安全方面的最佳实践。</span>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;"><span style="color: black;">内存</span></strong></h3><span style="color: black;">运行 PHP 时需要关心<span style="color: black;">每一个</span> PHP 进程要<span style="color: black;">运用</span>多少内存,</span><span style="color: black;">php.ini</span><span style="color: black;"> 中的 </span><span style="color: black;">memory_limit</span><span style="color: black;"> 设置用于设定单个 PHP 进程<span style="color: black;">能够</span><span style="color: black;">运用</span>的系统内存最大值。</span><span style="color: black;">此设置的默认值为128M,这可能适用于大<span style="color: black;">都数</span>中小型PHP应用程序。<span style="color: black;">然则</span>,<span style="color: black;">倘若</span>您正在运行微型PHP应用程序,则<span style="color: black;">能够</span>降低此值以节省系统资源。</span><span style="color: black;">另一方面,<span style="color: black;">倘若</span>您正在运行内存密集型的PHP应用程序,则<span style="color: black;">能够</span><span style="color: black;">增多</span>该值。此值的<span style="color: black;">体积</span>由可用的系统内存确定,确定为PHP分配多少值是一门艺术。</span><span style="color: black;">决定给 PHP 分配多少内存,以及能<span style="color: black;">包袱</span>起多少个 PHP-FPM 进程时,<span style="color: black;">能够</span><span style="color: black;">按照</span>以下维度信息进行判断:</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">一共<span style="color: black;">能够</span>分配给 PHP 多少内存?以一个 2G 内存的 VPS 为例,这台设备中可能还运行了其他进程,如 MySQL、Nginx 等,<span style="color: black;">那样</span>留 512M 给 PHP 是合适的。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">每一个</span> PHP 进程平均耗费多少内存?这个要监控进程的内存<span style="color: black;">运用</span>量,<span style="color: black;">能够</span><span style="color: black;">运用</span>命令行命令 </span><span style="color: black;">top</span><span style="color: black;">,<span style="color: black;">亦</span><span style="color: black;">能够</span>在 PHP 脚本中调用</span><span style="color: black;">memory_get_peak_usage()</span><span style="color: black;"> 函数,不管<span style="color: black;">运用</span>哪种方式,都要多次运行同一个脚本,<span style="color: black;">而后</span>取内存消耗的平均值。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">能<span style="color: black;">包袱</span>起多少个 PHP-FPM 进程?假设我给 PHP 分配了 512M 内存,<span style="color: black;">每一个</span> PHP 进程平均耗费 15M 内存,<span style="color: black;">那样</span><span style="color: black;">能够</span><span style="color: black;">包袱</span>起 34 个 PHP-FPM 进程。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">有足够的系统资源吗?最后还需要确认有足够的系统资源运行 PHP 应用并处理预期的流量。</span></p><span style="color: black;">注:<span style="color: black;">咱们</span>应该<span style="color: black;">运用</span> Apache Bench 或 Siege 在类似生产环境的<span style="color: black;">要求</span>下对 PHP 应用做压力测试,以确定生产环境<span style="color: black;">是不是</span>有足够的资源可用。</span>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;"><span style="color: black;">Zend OPcache</span></strong></h3><span style="color: black;">确定要分配多少内存后,就<span style="color: black;">能够</span>配置 PHP 的 Zend OPcache 扩展</span><span style="color: black;">PHP 5.5.0+ 内置了这个扩展,下面是在 </span><span style="color: black;">php.ini</span><span style="color: black;"> 文件中配置和优化 Zend OPcache 扩展所用的设置:</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">opcache.memory_consumption = 64</span><span style="color: black;">:为操作码缓存分配的内存量(以MB为单位)。分配的内存量应能够存储应用程序中所有PHP脚本编译的操作码。该值<span style="color: black;">能够</span><span style="color: black;">按照</span>应用程序的<span style="color: black;">体积</span>设置为<span style="color: black;">区别</span>的值。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">opcache.interned_strings_buffer = 16</span><span style="color: black;">:用于存储常驻字符串的内存量(以MB为单位)。什么是驻留字符串? PHP解释器将在其后找到同一字符串的多个实例,并将此字符串<span style="color: black;">保留</span>在内存中。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>再次<span style="color: black;">运用</span>相同的字符串,PHP解释器将<span style="color: black;">运用</span>指针,以节省内存。默认<span style="color: black;">状况</span>下,PHP驻留字符串在<span style="color: black;">每一个</span>PHP进程中都是隔离的。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此设置<span style="color: black;">准许</span>PHP-FPM进程池将所有进程驻留字符串存储在共享缓冲区中,以便<span style="color: black;">能够</span>将其存储在PHP-FPM进程池中。在多个进程之间引用常驻字符串,从而节省了<span style="color: black;">更加多</span>内存。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">opcache.max_accelerated_files = 4000</span><span style="color: black;">:<span style="color: black;">能够</span>存储在操作码缓存中的PHP脚本的最大数量。取值范围是2000〜100,000。此值必须大于PHP应用程序中的文件数。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">opcache.validate_timestamps = 1</span><span style="color: black;">:当此设置的值为1时,一段时间后,PHP将<span style="color: black;">检测</span>PHP脚本的内容<span style="color: black;">是不是</span>已更改,并<span style="color: black;">检测</span>间隔由 </span><span style="color: black;">opcache.revalidate_freq</span><span style="color: black;"> 设置指定。<span style="color: black;">倘若</span>此设置的值为0,则PHP将不会<span style="color: black;">检测</span>PHP脚本的内容<span style="color: black;">是不是</span>有更改,并且<span style="color: black;">咱们</span>必须自己清除缓存的操作码。<span style="color: black;">意见</span>在<span style="color: black;">研发</span>环境中将其设置为1,在生产环境中将其设置为0。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">opcache.revalidate_freq = 0</span><span style="color: black;">:设置多久(单位是秒)<span style="color: black;">检测</span>一次 PHP 脚本内容<span style="color: black;">是不是</span>有变化。设置为0秒的含义是仅当</span><span style="color: black;">opcache.validate_timestamps</span><span style="color: black;"> 设置为1时,每次请求PHP文件时都会重新验证它们,<span style="color: black;">因此呢</span>,每次在<span style="color: black;">研发</span>环境中而不是在生产环境中都会重新验证PHP文件。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">opcache.fast_shutdown = 1</span><span style="color: black;">:此设置<span style="color: black;">准许</span>操作码<span style="color: black;">运用</span>更快的停机时间,将对象破坏和内存释放留给Zend Engine的内存管理器。</span></p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;"><span style="color: black;">文件上传</span></strong></h3><span style="color: black;"><span style="color: black;">倘若</span>您的应用程序<span style="color: black;">准许</span>上传文件,则最好设置<span style="color: black;">能够</span>上传的最大文件<span style="color: black;">体积</span>。<span style="color: black;">另一</span>,最好设置一次<span style="color: black;">能够</span>上传多少个文件:</span><span style="color: black;">file_uploads</span> = <span style="color: black;">1</span><span style="color: black;">upload_max_filesize</span> = <span style="color: black;">10</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">M</p><span style="color: black;">max_file_uploads</span> = <span style="color: black;">3</span><span style="color: black;">默认<span style="color: black;">状况</span>下,PHP<span style="color: black;">准许</span>在单个请求中上传20个文件。 上传的最大文件为2MB。在<span style="color: black;">这儿</span>,我将其设置为在单个请求中最多上传3个文件,<span style="color: black;">每一个</span>文件的最大<span style="color: black;">体积</span>为10MB。<span style="color: black;">亦</span>不要设置此值。很大,否则会<span style="color: black;">出现</span>超时。</span><span style="color: black;">注:<span style="color: black;">倘若</span>非要上传大文件,Web 服务器的配置<span style="color: black;">亦</span>要做相应<span style="color: black;">调节</span>。除了在 </span><span style="color: black;">php.ini</span><span style="color: black;"> 中设置之外,还要<span style="color: black;">调节</span> Nginx 虚拟主机配置中的 </span><span style="color: black;">client_max_body_size</span><span style="color: black;"> 设置。</span>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;"><span style="color: black;">最长执行时间</span></strong></h3><span style="color: black;">php.ini</span><span style="color: black;"> 文件中的 </span><span style="color: black;">max_execution_time</span><span style="color: black;"> 用于设置单个PHP进程在终止之前<span style="color: black;">能够</span>运行的最<span style="color: black;">长期</span>。此设置默认为30秒,<span style="color: black;">意见</span>将其设置为5秒:</span><span style="color: black;">max_execution_time</span> = <span style="color: black;">5</span><span style="color: black;">注:在 PHP 脚本中<span style="color: black;">能够</span>调用 set_limit_time() 函数覆盖这个设置。</span><span style="color: black;">假设<span style="color: black;">咱们</span>要生成报告并将结果生<span style="color: black;">作为</span>PDF文件。该任务可能需要10分钟<span style="color: black;">才可</span>完成,并且<span style="color: black;">咱们</span>当然不<span style="color: black;">期盼</span>等待PHP请求10分钟。</span><span style="color: black;"><span style="color: black;">咱们</span>应该编写一个单独的PHP文件,并将其放在单独的背景中。在该流程中执行后,Web应用程序<span style="color: black;">能够</span>在几毫秒内生成一个单独的后台流程,<span style="color: black;">而后</span>返回HTTP响应:</span><span style="color: black;">exec</span>(<span style="color: black;">echo "create-report.php" | at now</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p><span style="color: black;">echo</span> <span style="color: black;">report pending...</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p><span style="color: black;">create-report.php</span><span style="color: black;">在单独的后台进程中运行。运行之后,您<span style="color: black;">能够</span>更新数据库或将报告<span style="color: black;">经过</span>电子邮件发送给收件人。</span><span style="color: black;"><span style="color: black;">然则</span>,这种用法很少见。<span style="color: black;">一般</span>,<span style="color: black;">咱们</span><span style="color: black;">经过</span>异步<span style="color: black;">运用</span>队列来实现类似的功能。无论在安全性,可伸缩性和可<span style="color: black;">守护</span>性方面,效果都更好。<span style="color: black;">关联</span>组件<span style="color: black;">拥有</span>轻量级<span style="color: black;">信息</span>队列PHPResque等。</span>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;"><span style="color: black;">处理会话</span></strong></h3><span style="color: black;"><span style="color: black;">PHP的默认会话处理程序减慢了大型应用程序的速度,<span style="color: black;">由于</span>该处理程序将会话数据存储在硬盘上,从而<span style="color: black;">导致</span>不必要的磁盘I / O并浪费时间。<span style="color: black;">咱们</span>应该将会话数据<span style="color: black;">保存</span>在内存中,例如<span style="color: black;">运用</span>Memcached或Redis。这还有一个额外的好处-将来<span style="color: black;">更易</span>扩展</span><span style="color: black;">。</span></span><span style="color: black;"><span style="color: black;">倘若</span>会话数据存储在硬盘上,则添加其他服务器不方便。<span style="color: black;">倘若</span>会话数据存储在Memcached或Redis中,则任何分布式PHP-FPM服务器都<span style="color: black;">能够</span><span style="color: black;">拜访</span>会话数据。</span><span style="color: black;"><span style="color: black;">倘若</span>想把会话数据<span style="color: black;">保留</span>在 Memcached 中,需要做如下配置:</span>session.save_handler =<span style="color: black;">memcached</span>session.save_path = <span style="color: black;">127.0.0.1:11211</span>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;"><span style="color: black;">缓冲输出</span></strong></h3><span style="color: black;"><span style="color: black;">倘若</span>在更少的块中发送<span style="color: black;">更加多</span>的数据,而不是在<span style="color: black;">更加多</span>的块中发送更少的数据,则网络将更加<span style="color: black;">有效</span>,<span style="color: black;">亦</span><span style="color: black;">便是</span>说,以更少的片段浏览器传递内容以进行<span style="color: black;">拜访</span>,这<span style="color: black;">能够</span>减少HTTP请求的总数。</span><span style="color: black;"><span style="color: black;">因此呢</span>,<span style="color: black;">咱们</span>必须让PHP缓冲输出。默认<span style="color: black;">状况</span>下,PHP已启用输出缓冲功能,并且PHP将在缓冲4096字节的输出后将内容发送到Web服务器。</span><span style="color: black;">output_buffering</span> = <span style="color: black;">4096</span><span style="color: black;">implicit_flush</span> = <span style="color: black;">false</span><span style="color: black;">注:<span style="color: black;">倘若</span>想要修改输出缓冲区的<span style="color: black;">体积</span>,<span style="color: black;">保证</span><span style="color: black;">运用</span>的值是4(32位系统)或8(64位系统)的倍数</span>。<h3 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;"><span style="color: black;">真实路径缓存</span></strong></h3><span style="color: black;">PHP 会缓存应用<span style="color: black;">运用</span>的文件路径,<span style="color: black;">这般</span>每次<span style="color: black;">包括</span>或导入文件时就无需<span style="color: black;">持续</span>搜索<span style="color: black;">包括</span>路径了,这个缓存叫真实路径缓存(realpath cache),<span style="color: black;">倘若</span>运行的是大型的 PHP 文件(如 Composer 组件),<span style="color: black;">运用</span>了<span style="color: black;">海量</span>文件,<span style="color: black;">增多</span> PHP 真实路径缓存的<span style="color: black;">体积</span>能得到更好的性能。</span><span style="color: black;">真实路径缓存的默认<span style="color: black;">体积</span>为16K。此缓存所需的确切<span style="color: black;">体积</span><span style="color: black;">很难</span>确定,但<span style="color: black;">能够</span><span style="color: black;">运用</span><span style="color: black;">有些</span>技巧:<span style="color: black;">首要</span>,<span style="color: black;">增多</span><span style="color: black;">实质</span>路径高速缓存的<span style="color: black;">体积</span>并将其设置为<span style="color: black;">尤其</span>大的值,例如256K。</span><span style="color: black;">最后在php脚本末尾添加上 </span><span style="color: black;">print_r(realpath_cache_size());</span><span style="color: black;">,输出真实路径缓存的真正<span style="color: black;">体积</span>,最后,把真实路径缓存的<span style="color: black;">体积</span>改为这个真正的值。<span style="color: black;">咱们</span><span style="color: black;">能够</span>在 </span><span style="color: black;">php.ini</span><span style="color: black;">文件中设置真实路径缓存的<span style="color: black;">体积</span>:</span><span style="color: black;">realpath_cache_size</span> = <span style="color: black;">64</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">K</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">*声明:本文于网络整理,版权归原作者所有,如<span style="color: black;">源自</span>信息有误或侵犯权益,请联系<span style="color: black;">咱们</span>删除或授权事宜。</span></p><img src="https://mmbiz.qpic.cn/mmbiz_gif/LFP9SpGv0PFonacOKxEOcN8bHYq5Hyn870JicAH1y51rLbO3fF46aibXLRXleDvibc7q9hIfZMj6QDKLVAzHibdBXw/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1" style="width: 50%; margin-bottom: 20px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/LFP9SpGv0PHn7RHfichibiaAhibKghiacicKX4LHaZXVjaibdzDdExc4DtEoXZAuDSA3SCicQ56Hkgq3AyODWtnibWh3w9g/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">END<img src="https://mmbiz.qpic.cn/mmbiz_jpg/LFP9SpGv0PHn7RHfichibiaAhibKghiacicKX4CLibX2vEufeljiadjkODkRPSzLXjMYXfq1U6BdYNMicUhEhe2EXlAr7VA/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">PHP开源社区<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">扫描关注 进入”PHP资料“</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">免费获取进阶</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">面试、文档、视频资源</p><strong style="color: blue;">点击“查看原文”获取<span style="color: black;">更加多</span></strong>
你的见解独到,让我受益匪浅,非常感谢。
页:
[1]