Speed up WordPress on DreamHost with OPcache

Speed Up WordPress on DreamHost with OPcache

Enabling OPcache can improve your site’s performance by decreasing the php execution process when a script is requested. DreamHost permits a user to log into the server through secure shell with user level permissions.

What Is OPcache?

OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.

– php.net manual

In short; It will improve php performance by skipping the read and compile steps.

The diagram below is a great visual representation. Once the php file is compile and stored in the memory, each concurrent request for that file is served from memory.

PHP OPcache Flow Chart
Flow chart provided by EngineYard.

What if my files change?

When changes or updates are performed on your website, the cache will need to be reset. You can either kill the php service or reset the cache through the OPcache API. WordPress users can install the OPcache Dashboard plugin. The OPcache Dashboard plugin will handle resetting your OPcache when core, plugin, or theme files have been updated.

Configure OPcache on DreamHost for php 5.6 and later

As of November 2017, DreamHost offers php 5.6, 7.0, and 7.1 with opcache already installed. If your site does not already have a phprc file, create one and append the following opcache configuration.

[opcache]
; Set to 0 to disable
opcache.enable=1

; Max memory consumption. Default 32 megs
opcache.memory_consumption=32

; The amount of memory to store immutible strings
opcache.interned_strings_buffer=4

; Comments in code will not be compiled
opcache.save_comments=0

; Comments will not be loaded
opcache.load_comments=0

; Make sure each cached file has a distinct path
; See http://php.net/manual/en/opcache.configuration.php#ini.opcache.revalidate-path
opcache.revalidate_path=1

; Check opcace when php uses file checking functions, file_exists, etc
; See http://php.net/manual/en/opcache.configuration.php#ini.opcache.enable-file-override
opcache.enable_file_override=1

; Turn off cache experiation.
; See http://php.net/manual/en/opcache.configuration.php#ini.opcache.validate-timestamps
opcache.validate_timestamps=0

; How long to check a file if it needs to be re-cached
; If opcache.validate_timestamps is disabled, this is ignored.
; See http://php.net/manual/en/opcache.configuration.php#ini.opcache.revalidate-freq
opcache.revalidate_freq=300

; The max file size to be stored in cache
opcache.max_file_size=2097152

; Use faster mechanism for shutting down
opcache.fast_shutdown=0

; Specify the max number of files
opcache.max_accelerated_files=3907

; {{{ The following lines were automatically added by DreamHost
zend_extension=opcache.so
; }}} That's all from DreamHost

Warning

DreamHost will automatically append zend_extension=opcache.so to phprc files, which can cause issues if you attempt to specify the opcache.so file yourself. Let them do it.

Testing the install

To see if the module was installed correctly, you can run the following command. You should see the OPcache settings appear.

$ php -i | grep opcache

Install the OPcache Dashboard plugin

To reset the OPcache when WordPress has updates, I use the OPcache Dashboard plugin.

wp plugin install opcache --activate

Configure to fit your needs

The configuration I provided is very simple, you site may require more or less. Take the time to see what works best for you.

Published by

rfmeier

Product developer at WPEngine and runner from Hampshire, Illinois. I work with php a lot; Spending most of my time digging into source code to see how it all works. You can find me on Twitter @rfmeier.

2 thoughts on “Speed Up WordPress on DreamHost with OPcache”

    1. Andy,

      Thank you for reading.

      XCache does the same job as OPCache. However, OPCache is built into php with versions 5.5 and greater. While I have never used XCache (I used APC before OPCache), I did notice a little speed boost when I switched to OPCache. Personally, I would use the native opcache over a 3rd party opcache, but that is my choice.

      If you are on the edge with your decision, I would do the research and some tests to see what fits you the best.

      In the end any opcache is far better than no opcache.

Leave a Reply

Your email address will not be published. Required fields are marked *