fotogestoeber - Fotolia
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.
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:
[email protected]:~# 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.
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.
Scripting languages to get ahead in modern IT