How To Enable PHP 5.5 Opcache on Ubuntu 14.04

How To Enable PHP 5.5 Opcache on Ubuntu 14.04

With the release of Ubuntu 14.04 and the proliferation of PHP 5.5, there is going to be a migration away from Alternative Performance Cache (APC) and toward PHP’s new built-in OPcache.

This is a logical move that seems destined for any interpreted language.  As websites have become more and more complicated with many processes running, opcode caching has become a necessity – fortunately, it’s simple to implement.

The php.net site has a nice page of all the runtime options available, but we will cover the basics here to get you started quickly.

All you need to do to get OPcache set up is to make changes in the php.ini file on your server.

Open php.ini In Your Favorite Text Editor

To get started open your php.ini file.

Apache web-servers

Nginx web-servers with PHP-FPM

Enable the OPcache

To enable the OPcache, change to the following lines — easy enough!

Change to:

Note: you have to uncomment this line as well as change the “0″ to “1″.

Modify the Amount of RAM the OPcache Will Use

With OPcache, there is a trade-off between speed and the amount of RAM used. The more RAM you are willing to dedicate to storing opcode, the more opcode that can be stored. There is a diminishing return at some point, because some code will execute rarely, or your code base might not be that big. It is worth playing with this setting to see where you get the best performance-versus-RAM trade-off.  This setting is in megabytes.

Change to:

Boost the Number of Scripts that Can Be Cached

OPcache has a strange setting that requires you to not only adjust the amount of RAM, but also define the number of scripts that can be cached. You have the option of tuning this parameter for your own application too, especially if you find that your hit rate is not close to 100 percent.

Change to:

Change the Revalidate Frequency

To make sure that the OPcache notices when you change your PHP code, you can set the revalidate frequency. Basically, this will tell the cache how often to check the timestamp on the files. This is measured in seconds.

Change to:

Verify that the PHP OPcache Mod is Enabled

Believe it or not, that converts most of the settings you will need to get started. PHP5 has its own module system (since 5.4), so make sure that OPcache is enabled.

Restart PHP and Your Server

You should now be all set to start using PHP 5.5’s OPcache. You just need to restart your server to get it going.

Apache Web-Servers

Nginx Web-Servers

Conclusion

Well, there you have it. It’s actually amazingly simple to get this up and running. There are a number of options that allow you to monitor your hit rate with OPcache. Here is an open-source solution (OPcache Status) that can be found on GitHub.

Ryan Frankel

Questions or Comments? Ask Ryan!

Ask a question and Ryan will respond to you. We strive to provide the best advice on the net and we are here to help you in any way we can.

  • Bo

    I love guides like this. So simple and to the point. Thank you for putting it together. Got it working “in one try” in about 2 minutes… And another 2 minutes to install this status viewer (again, super amazingly easy to deal with) : https://github.com/rlerdorf/opcache-status/

  • frankel0

    Thanks Bo. I think as time goes on we will see more and more advanced monitoring/status viewers. It’s nice to see that people have already started on this.

  • I didn’t know turning on OPcache was so easy! Question: on a newly built server, where/which directory would I install that Opcache Status, i.e., so it’s accessible to me but not publicly?

    • frankel0

      Hey Joe,

      You could just put Opcache Status in a directory under your web root and then protect that directory with htaccess. For basic security I think that would be the simplest method. Here is a simple tutorial that will get you going if you need it. Things changed slightly between Ubuntu 12.04 and 14.04 so watch out for that. https://www.digitalocean.com/community/tutorials/how-to-use-the-htaccess-file

  • I tied php5enmod opcache but I found the message permission is denies…! Any recommendation..?

    • frankel0

      Kimseasok,

      Try sudo php5enmod opcache as you need root permissions to enable php mods. We will update the article accordingly. Let me know if that works for ya.

      • Thanks, for responding..! I’m looking forward that post you mentioned..

  • Hi, is ‘sudo php5enmod opcache’ supposed to return any message?

    • frankel0

      I don’t believe that that command returns anything to sdtout. The best way to verify that Opcache is running and how it is performing is to use the Opcache Status script mentioned above.

  • Thanks for the good information!
    By the way, can I use other Cache Plugins(like W3 total Cache) in the WordPress at the same time?

    • frankel0

      Yes Junuk, in fact, it should help increase the performance of most caching plugins that still utilize spooling up PHP.