Guru: Speed Up Command-Line PHP
January 21, 2019 Alan Seiden
While PHP runs quickly via the Apache web server, command-line PHP scripts (run from PASE directly or via CL or RPG) have a lag on start-up. In this article, I show how to speed up PHP when called from the command line (known as PHP-CLI).
Why does PHP-CLI (command line PHP) have a slow start-up? While several reasons are often given, I’ve found the culprit to be the loading of PHP extensions that are enabled by php.ini and other configuration files of Zend Server. Examples of extensions are ibm_db2, simplexml, and Zend’s proprietary components.
Within a normal web server environment, loading extensions doesn’t slow down PHP, because FastCGI preloads all configured extensions when it preloads PHP. On the other hand, when you run PHP from a command line (or from CL or RPG), PHP can’t be pre-loaded; it’s loaded at runtime. In that case, several seconds may be required to load extensions.
You can use the –no-php-ini and –define options to speed up PHP-CLI. Here’s a quick reference from http://php.net/manual/en/features.commandline.options.php:
–no-php-ini | Ignore php.ini completely. |
–define | Set a custom value for any of the configuration directives allowed in php.ini. |
Note that all examples below assume PHP 7, which is installed in /usr/local/zendphp7, but you could substitute any valid recent PHP version or location.
Let’s consider a simple script:
<?php // simple.php code echo "PHP script running.\n"; ?>
Simple.php outputs a message to show that it is running. Because we wish to measure the overhead of running a script, not the time required by the script itself, a simple script is best. Save simple.php somewhere on your IFS. Ensure that your user profile has *R permissions and all directories above it have at least *X. For our example, let’s save it in /www/zendphp7/htdocs, although it could go anywhere.
Run simple.php
First, go into a PASE terminal. (An SSH shell with Bash would work better, but I realize that you may not have Bash installed, so I’ll demonstrate using traditional QP2TERM.)
CALL PGM(QP2term)
Now let’s go to the directory of the PHP 7 binary:
cd /usr/local/zendphp7/bin
Run the speed test using the default php.ini:
php /www/zendphp7/htdocs/simple.php
If you used a stopwatch, you’d notice that the test needed up to a couple of seconds to run, even though the script did virtually nothing.
Now add –no-php-ini, which prevents PHP from reading any .ini file, and thus not loading any extensions.
php --no-php-ini /www/zendphp7/htdocs/simple.php
Using the –no-php-ini option, you’ll observe that the script returned almost immediately.
Our choices aren’t “all or nothing.” We can add configuration values on the command line.
Run our test script again, this time adding –define to load the ibm_db2 extension, a commonly used extension for IBM i.
php --no-php-ini --define extension=/usr/local/zendphp7/lib/php_extensions/ibm_db2.so /www/zendphp7/htdocs/simple.php
The script still ran quickly with a single extension loaded.
Speed at your command
I have demonstrated that PHP-CLI runs substantially faster when we cut back on extensions, using PHP’s –no-php-ini and –define options. Try this technique and enjoy the improvement.
An IBM Champion and founder of Seiden Group, Alan Seiden leads a team that mentors clients in building APIs and web/mobile applications using open source, PHP, Python, Node.js, and IBM i business logic. Alan’s passion for the IBM i community inspires him to host the bi-annual CIO Summit and offer a free monthly tips newsletter.
Hello Alan,
Maybe it’s also worth to mention that you can use the –php-ini option to start PHP from command line using a separate ini-file. This comes in especially handy when you have multiple CLI calls, which all use the same configuration.
Best Martin
Thanks, Martin! You make an excellent point about –php-ini. I plan to write a second article about PHP-CLI, demonstrating –php-ini and how to use multiple –define options.
Alan