{"id":1206,"date":"2018-10-15T18:34:48","date_gmt":"2018-10-15T18:34:48","guid":{"rendered":"https:\/\/www.s-sols.com\/?p=1206"},"modified":"2021-11-05T09:05:15","modified_gmt":"2021-11-05T09:05:15","slug":"enable-php-code-cache","status":"publish","type":"post","link":"https:\/\/www.s-sols.com\/enable-php-code-cache","title":{"rendered":"Increase site\u2019s speed by enable PHP code caching"},"content":{"rendered":"<p style=\"text-align:justify\">\n  PHP cache incredible increases the <strong>speed of a site<\/strong>. For instance, if request preparation takes 1.2 seconds, after cache enabling it will take 0.6 seconds (increase by about 50%). Particularly, it influences on <strong>WordPress<\/strong> significantly as well because it contains a lot of PHP code.\n<\/p>\n<p style=\"text-align:justify\">\n  On some sites based on e.g. WAMP (Apache, MySQL and PHP bundle for Windows) PHP code cache is turned off by default so <strong>it\u2019s better<\/strong> for us <strong>to check the settings<\/strong>.\n<\/p>\n<p style=\"text-align:justify\">\n  The most popular way to do that is to use OpCache \u2013 a special PHP module that does code caching. In PHP 5.5 and above this module already integrated into.\n<\/p>\n<h2>\n  How it works<br \/>\n<\/h2>\n<p style=\"text-align:justify\">\n  OpCache provides faster PHP execution through compiled code caching and optimization. It improves PHP performance by storing precompiled script bytecode in the shared memory. Every time a PHP script is requested, the PHP script will be parsed and compiled into opcode, which then is executed in the engine. When an opcode cache is introduced, after a PHP script is interpreted and turned into opcode (compiled), it\u2019s saved in shared memory, and subsequent requests will skip the parsing and compilation phases and leverage the opcode stored in memory, reducing the script\u2019s execution time.\n<\/p>\n<p style=\"text-align:center\">\n  <img decoding=\"async\" src=\"\/data\/content\/post\/solutions\/enable-php-code-cache\/script-cache-scheme-en.png\" alt=\"script-cache-scheme\" title=\"script-cache-scheme - Increase site\u2019s speed by enable PHP code caching\" \/>\n<\/p>\n<h2>\n  Configure<br \/>\n<\/h2>\n<p style=\"text-align:justify\">\n  Configuration mainly is done by editing a special file named <strong>php.ini<\/strong>.\n<\/p>\n<h3><a id=\"php_ini_location\"><\/a>Locate configuration file<br \/>\n<\/h3>\n<p style=\"text-align:justify\">\n  The location is depend on a system and can be like \/etc\/php5\/apache2\/php.ini or \/etc\/php5\/fpm\/php.ini, \/usr\/local\/lib\/php.ini on Unix-similar systems and C:\\wamp\\bin\\php\\php5.5.12\\php.ini on Windows.\n<\/p>\n<p style=\"text-align:justify\">\n  In WAMP there is a special menu item that opens it.\n<\/p>\n<p style=\"text-align:center\">\n  <img decoding=\"async\" src=\"\/data\/content\/post\/solutions\/enable-php-code-cache\/wamp-php-ini-en.png\" alt=\"wamp-php-ini\" title=\"wamp-php-ini - Increase site\u2019s speed by enable PHP code caching\" \/>\n<\/p>\n<p style=\"text-align:justify\">\n  Also, a file\u2019s location can always be discovered from PHP info. Just create phpinfo.php file with content <code>&lt;?php phpinfo(); ?&gt;<\/code>, copy it to the web server\u2019s root directory and run it by http:\/\/localhost\/phpinfo.php. The location is in the <strong>Configuration File (php.ini) Path<\/strong> section of the first block (look at the picture below).\n<\/p>\n<p style=\"text-align:center\">\n  <img decoding=\"async\" src=\"\/data\/content\/post\/solutions\/enable-php-code-cache\/phpinfo-php-ini-location-en.png\" alt=\"phpinfo-php-ini-location\" title=\"phpinfo-php-ini-location - Increase site\u2019s speed by enable PHP code caching\" \/>\n<\/p>\n<h3>\n  Set settings<br \/>\n<\/h3>\n<p style=\"text-align:justify\">\n  All settings are located in [opcache] section of <strong>php.ini<\/strong>.\n<\/p>\n<p style=\"text-align:justify\">\n  Ensure that the main settings exist there:\n<\/p>\n<p><code class=\"p\">[opcache]<br \/>zend_extension=&lt;correct_path_to_php_opcache_module&gt;<br \/>opcache.enable=1<\/code>\n<\/p>\n<p style=\"text-align:justify\">\n  Other additional settings can be left untouched with default values. But sometimes it\u2019s required to tune them. So here is a short description of each important one with recommended values.\n<\/p>\n<ul>\n<li>\n<div style=\"text-align:justify\"><strong>opcache.enable = 1<\/strong>\n    <\/div>\n<p style=\"text-align:justify\">\n      Enables or disables the cache. The main setting.\n    <\/p>\n<\/li>\n<li>\n<div style=\"text-align:justify\"><strong>opcache.use_cwd = 1<\/strong>\n    <\/div>\n<p style=\"text-align:justify\">\n      Eliminates possible collisions between files with the same base name.\n    <\/p>\n<\/li>\n<li>\n<div style=\"text-align:justify\"><strong>opcache.validate_timestamps = 1<\/strong>\n    <\/div>\n<p style=\"text-align:justify\">\n      Enables runtime validation of script files changes. It\u2019s recommended for use if the site is being changed with different PHP scripts. E.g. plugins installation in WordPress. But if there is insurance that all set of scripts is stable, this setting can be turned off to increase a little bit of runtime execution.\n    <\/p>\n<\/li>\n<li>\n<div style=\"text-align:justify\"><strong>opcache.revalidate_freq = 2<\/strong>\n    <\/div>\n<p style=\"text-align:justify\">\n      How often to check script changes, in seconds. 0 will result in checking on every request. It doesn\u2019t work if opcache.validate_timestamps is disabled.\n    <\/p>\n<\/li>\n<li>\n<div style=\"text-align:justify\"><strong>opcache.max_accelerated_files = 10000<\/strong>\n    <\/div>\n<p style=\"text-align:justify\">\n      The maximum number of cached files. Bigger values recommended for bigger sites with a huge amount of scripts and installed memory.\n    <\/p>\n<\/li>\n<\/ul>\n<p style=\"text-align:justify\">\n  The actual parameters can always be checked at the <a href=\"http:\/\/php.net\/manual\/en\/opcache.installation.php\" target=\"_blank\" rel=\"noopener nofollow\">OPcache source configuration page<\/a> and in the <a href=\"http:\/\/php.net\/manual\/en\/opcache.configuration.php\" target=\"_blank\" rel=\"noopener nofollow\">list of all parameters<\/a>.\n<\/p>\n<h3>\n  Restart web service<br \/>\n<\/h3>\n<p style=\"text-align:justify\">\n  Just restart Web service (e.g. Apache) and PHP will be restarted too.\n<\/p>\n<p style=\"text-align:justify\">\n  In WAMP it can be done by restart Apache.\n<\/p>\n<p style=\"text-align:center\">\n  <img decoding=\"async\" src=\"\/data\/content\/post\/solutions\/enable-php-code-cache\/wamp-restart-apache-en.png\" alt=\"wamp-restart-apache\" title=\"wamp-restart-apache - Increase site\u2019s speed by enable PHP code caching\" \/>\n<\/p>\n<h2>\n  Check<br \/>\n<\/h2>\n<p style=\"text-align:justify\">\n  The obvious way to check it is to compare server response before and after turning on a cache. E.g. we <a href=\".\/measure-site-speed-with-chrome-browser\">can do that by Google Chrome browser<\/a>.\n<\/p>\n<p style=\"text-align:justify\">\n  Another way to check that OpCache is working is to create a file opcache.php with content <code>&lt;pre&gt;&lt;?php print_r(opcache_get_status());?&gt;&lt;\/pre&gt;<\/code>, copy it to the web server\u2019s root directory and run it by http:\/\/localhost\/opcache.php. The sample result in the picture below.\n<\/p>\n<p style=\"text-align:center\">\n  <img decoding=\"async\" src=\"\/data\/content\/post\/solutions\/enable-php-code-cache\/opcache-configuration-check-en.png\" alt=\"opcache-configuration-check\" title=\"opcache-configuration-check - Increase site\u2019s speed by enable PHP code caching\" \/>\n<\/p>\n<p style=\"text-align:justify\">\n  The value of <strong>opcache_enabled<\/strong> should be 1. And lower there is PHP scripts statistics can be viewed like total count and detailed info for each one.\n<\/p>\n<p style=\"text-align:justify\">\n  If the site\u2019s performance is not enough it\u2019s possible to <a href=\".\/wordpress-site-response-time-optimization\">tune up WordPress by cache plugins<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p style=\"text-align:justify\">\n  One of the ways to increase site\u2019s speed.<\/p>\n","protected":false},"author":17,"featured_media":1240,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[1],"tags":[151,163,155,154,152,153,92],"class_list":["post-1206","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-solutions","tag-apache","tag-cache","tag-opcode","tag-performance","tag-php","tag-wamp","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/www.s-sols.com\/api\/wp\/v2\/posts\/1206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.s-sols.com\/api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.s-sols.com\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.s-sols.com\/api\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.s-sols.com\/api\/wp\/v2\/comments?post=1206"}],"version-history":[{"count":0,"href":"https:\/\/www.s-sols.com\/api\/wp\/v2\/posts\/1206\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.s-sols.com\/api\/wp\/v2\/media\/1240"}],"wp:attachment":[{"href":"https:\/\/www.s-sols.com\/api\/wp\/v2\/media?parent=1206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.s-sols.com\/api\/wp\/v2\/categories?post=1206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.s-sols.com\/api\/wp\/v2\/tags?post=1206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}