php-1544x500

Optimize Your PHP Handler and WordPress


If you are on a VPS or a dedicated server, you have some optimization issues to grapple with. Your PHP process can run faster if you set up an opcode cache, which will store byte-compiled PHP files in memory for a while so that your server doesn’t have to recompile them every time it runs them. The default choice for opcode caching (for PHP versions higher than 5.5) is Zend OPCache, which can be compiled in using Easy Apache on your WHM. Or you can get it installed on your server with your friendly hosting provider‘s help.

OpCache display

Zend OpCache monitoring, along with configuration directives.

Another level of caching is for user data. Many apps, mine included, need their data cached for performance purposes. WordPress does too, and uses a Transient API layer to store the temporary data in your database, in the same table as your options (wp_options) with identifiable keys. The default choice for a better performing, memory-based cache used to be APC (Alternative PHP Cache), which used to do both opcode caching and user caching, but is defunct now. But, you can still use the user data caching part of it by installing APCu.

APCu monitoring tool.

APCu monitoring. Note the run time settings, which need to be correctly set.

Note that both opcode caching and user data caching make sense only if the PHP process persists over multiple requests. If each request is handled by a newly spawned PHP process, it has no way of taking advantage of any kind of caching. The mode in which your PHP handler operates can be selected using your WHM, typically found under Configure PHP and suEXEC. The per-request spawning will happen if you set your php handler as suPHP. And one persistent mode DSO (Dynamic Shared Object).

WHM - How to configure PHP handler?

WHM – How to configure PHP handler? Note that APCu and OpCache can work only if PHP handler is in DSO (or other shared) mode.

After setting up APCu, you will still need to let your WordPress installation know that you have a user data cache, and that there is no need to pollute the wp_options table with transients. You can do this by using the ACPu Object Cache Backend plugin.

To summarize, there are the optimization techniques you will need to undertake:

  1. Switch to the DSO mode for your PHP handler
  2. Get Zend OpCache installed.
  3. Get APCu, and provide it to object-cache.php, for instance. pecl install channel://pecl.php.net/APCu-4.0.10 if you are using PHP5.5. Or pecl install channel://pecl.php.net/APCu-5.1.2 for PHP7. See APCu PECL page for the latest version info.
  4. Download and copy apc.php (ensure you download the right version) and opcache.php to visualize and optimize the caches.
  5. Install the ACPu Object Cache Backend plugin for all your WordPress blogs.

As I mentioned earlier, you need to run your PHP handler in DSO mode (or one of the other shared modes) to benefit from caching. One pesky problem I encountered when I switched to DSO was that my scripts could not use ini_set() and set_include_path(). I will write up the solution in the next article, coming up later this week. Stay tuned.

If you cannot install APCu for whatever reason, it is possible to simulate it. I am developing this simulation, and will release it as a plugin soon. And you will see a write-up about it on this blog. Agin, stay tuned.