fotogestoeber - Fotolia

Manage Learn to apply best practices and optimize your operations.

Optimize PHP with finely tuned IT resources and settings

Application ops should address the elephant in the room -- poorly optimized PHP -- to speed load and processing times.

On a daily basis, we interact with PHP-based websites. And you probably support at least one.

Despite the scripting language's prevalence and the importance of digitization to modern industry, organizations fail to effectively optimize PHP.

More than 90% of PHP-based websites still use PHP version 5. Of those websites, less than one quarter run the latest supported version, PHP 5.6. Despite the release of PHP 7 in December 2015, which has been documented and benchmarked as up to two times faster than PHP 5.6, the adoption rate is only around 3% among websites that use the language. The first step -- before you optimize PHP using the following tips -- is to upgrade to version 7.

Of course, some web apps or the app's plug-ins and extensions lack full compatibility with PHP 7, which has inhibited adoption. WordPress, Joomla and Drupal have led the way, with Magento finally getting up to speed as well. Application administrators will find it less of a chore to upgrade to PHP 7 with other users paving the way.

Identifying bottlenecks that affect PHP

In the early 2000s, the T1 network connection would be the biggest bottleneck for a PHP-4-powered vBulletin 2 forum, which turned out to use more bandwidth than the infrastructure could allocate to it. Times have changed.

A web server's upstream network is hardly ever a bottleneck for PHP today. However, bottlenecks in resources such as CPU, memory, storage I/O and database -- often MySQL -- slow PHP performance greatly.

CPU. Application performance monitoring tools and debuggers, from the likes of New Relic, Xdebug and Zend, monitor CPU load during normal and high usage. These tools spot when PHP hits capacity, and help app support staffers identify which code/pages are the most time-consuming on the CPU.

Memory. Insufficient server memory can also contribute to poor performance and throughput of PHP web apps. Cache using RAM to optimize PHP in this scenario, but be judicious. Low memory conditions reduce available file-system caches, a setup which, in turn, increases the server's reliance on slower storage.

Storage. Slow storage leads to high I/O wait times, which are easy to spot in the server's top utility stats. Consider how the application uses memory (RAM) versus disk or solid-state storage. Accessing a solid-state drive or hard disk can be 10 to 100 times slower than reading data from RAM.

MySQL. Do you log MySQL's slow queries? Are you using MyISAM, InnoDB or Aria storage engine table types? Have you typed in show processlist to get an overview of the tasks keeping MySQL busy? Optimize PHP and MySQL's relationship to improve application performance without changing any underlying hardware.

For a deeper understanding of the PHP and MySQL relationship, tailor application performance monitoring settings and analytics reports.

A hidden part of daily life

PHP is used by more than 80% of all websites with an identified server-side programming language. PHP powers popular websites, such as Facebook, Wikipedia and WordPress, along with millions of other personal and commercial sites. WordPress holds almost 60% of the market share, followed by other PHP-based content management systems such as Joomla, Drupal and Magento.

PHP OPcache

For teams running PHP 7, or at least PHP 5.6, consider using the PHP OPcache extension to eliminate redundant opcode script compile actions. OPcache improves PHP performance and scalability.

OPcache stores precompiled script bytecode in shared memory, rather than having PHP load and parse scripts on each request. As of PHP 5.5, the OPcache extension is bundled with PHP; earlier versions require the administrator to install it manually.

To check if OPcache is already enabled, use the function phpinfo() or type php -v via the Linux command line:

root@status:~# php -v

PHP 7.0.8-0ubuntu0.16.04.3 (cli)

Copyright (c) 1997-2016 The PHP Group

Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

 with Zend OPcache v7.0.8-0ubuntu0.16.04.3, Copyright (c) 1999-2016, by Zend Technologies

PHP offers simple instructions to enable OPcache manually via the opcache.so extension, e.g. zend_extension=opcache.so.

PHP realpath_cache

Most application operators will tune and tweak the configuration file php.ini. To optimize PHP for websites with high concurrent traffic and page views, you can take things a step further -- enable PHP's realpath cache. Set PHP realpath_cache_size and realpath_cache_ttl correctly given the particular requirements of the application. For example, the default time to live (TTL) of 120 seconds is generous for busy web servers with millions of daily page views. Conversely, you may need to increase TTL for PHP web apps with less traffic.

About the author:
Hayden James is Linux systems analyst and internet entrepreneur who supports and optimizes high-traffic web app and data servers for clients around the globe with a focus on scalability, performance and security.

Next Steps

The PHPMailer patch needed its own patch

Bolster web safety with these PHP tips

Scripting languages to get ahead in modern IT

This was last published in February 2017

Dig Deeper on Matching IT Resources to Application Requirements

PRO+

Content

Find more PRO+ content and other member only offers, here.

Join the conversation

7 comments

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

Where does your organization need to optimize PHP?
Cancel
Thanks for this!! Shared internally.
Cancel
"The first step -- before optimizing PHP using the following tips -- is to upgrade to version 7."

Wrong.  The reason only 3% of websites have upgraded to 7 is because they have a lot of legacy code that will fail in PHP 7.  MySQL connections are the biggest reason.

Unless you need something that isn't available in your current PHP installation, upgrading is risky.  Why fix what isn't broken?
Cancel
The vast majority of PHP websites on the web are free and paid web apps which are already PHP 7 compatible. So are their reputable plugins and extensions. Most PHP programmers stay up to speed as well.

Updating old code is as simple as using:
https://github.com/wimg/PHPCompatibility

PHP also introduces additional security and stability features.
Cancel
I can tell you that all the commercial websites I've developed for are not upgrading to PHP 7 because there is just too much code to unwind and modify in order for it to work in 7.

A lot of sites still use mysql_connect instead of PDO.  Right or wrong, even the step to PDO is a big undertaking for websites where their CMS and front ends are thousands of files with thousands of lines of code.

If it's a new site, sure, go with 7. If you're dealing with a legacy site that is more than a few years old, it may be more effort to fix the incompatibilities.  If the site works, don't break it.
Cancel
Also a great discussion on this article can be found here:
https://www.reddit.com/r/linux/comments/5su07w/more_than_80_of_the_web_is_powered_by_php_but_90/
Cancel
Thanks for the feedback. 

However, as noted in the article MySQL can affect PHP's performance. That should be checked as noted. 

If in your case "MySQL connections are the biggest reason." Then something is either wrong with your setup, queries, config or... the type of application you run requires PHP to wait at length on MySQL's queries (bottlneck). In this case making PHP faster (higher throughput) will only increase the effect of your DB bottleneck or max out # of connections, etc.

As per article you should check for bottlenecks. Naturally Disk and MySQL bottlenecks will suffer even more when PHP's speed/capacity is increased. 

Address bottlenecks first. 
Cancel

-ADS BY GOOGLE

SearchDataCenter

SearchAWS

SearchServerVirtualization

SearchCloudApplications

SearchCloudComputing

Close