PHP opcache reset + symlink-style deployment
Asked Answered
E

3

21

I am trying to reset PHP opcache after a symlink-style deployment. There is the opcache_reset.php file in my project which is executing by wget after the document root's symlink replacement:

<?php
clearstatcache(true);
opcache_reset();

In spite of that, the old files are still used. According to opcache_get_status() output, the number of manual_restarts increases, last_restart_time keeps up-to-date, but the file paths remains outdated. I need to call opcache_reset.php manually after a minute or so after deploy to make things right.

PHP version is 5.5.6, ZendOpcache is 7.0.3-dev. Opcache config:

opcache.blacklist_filename => no value
opcache.consistency_checks => 0
opcache.dups_fix => Off
opcache.enable => On
opcache.enable_cli => On
opcache.enable_file_override => Off
opcache.error_log => no value
opcache.fast_shutdown => 1
opcache.force_restart_timeout => 180
opcache.inherited_hack => On
opcache.interned_strings_buffer => 8
opcache.load_comments => 1
opcache.log_verbosity_level => 1
opcache.max_accelerated_files => 4000
opcache.max_file_size => 0
opcache.max_wasted_percentage => 5
opcache.memory_consumption => 128
opcache.optimization_level => 0xFFFFFFFF
opcache.preferred_memory_model => no value
opcache.protect_memory => 0
opcache.restrict_api => no value
opcache.revalidate_freq => 60
opcache.revalidate_path => Off
opcache.save_comments => 1
opcache.use_cwd => On
opcache.validate_timestamps => On
Entity answered 19/5, 2014 at 12:21 Comment(0)
E
42

Reasons and two possible solutions described in the ZendOptimizerPlus issue. We solved it by using $realpath_root in the nginx config:

fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
Entity answered 28/5, 2014 at 7:0 Comment(1)
I am using the same NGINX directives. However, I need to reload php fpm in order to make sure that php fpm uses the code from the new directory. Reloading fpm makes me lose some requests though. If i dont reload fpm, then it still executes code in the old directory. Same issue described here: serverfault.com/questions/630573/… Can you help?Breskin
R
7

If you are for some reason not able to use fastcgi_param with $realpath_root and using symlink style deployment, then try to set the opcache.revalidate_path = On in your PHP ini configuration.

I was not able to find any good documentation that explains how this ini directory works under the hood, but it did work after I changed the symlinks.

Rational answered 18/10, 2018 at 8:15 Comment(1)
Good catch! I'm using it in my Nginx conf like this fastcgi_param PHP_VALUE opcache.revalidate_path="1";Corium
D
1

I also faced that issue then finally I found a solution.

$ curl -sO http://gordalina.github.io/cachetool/downloads/cachetool.phar
$ chmod +x cachetool.phar

You can connect to a automatically guessed fastcgi server (if /var/run/php5-fpm.sock is a file or 127.0.0.1:9000)

apc
  apc:bin:dump             Get a binary dump of files and user variables
  apc:bin:load             Load a binary dump into the APC file and user variables
  apc:cache:clear          Clears APC cache (user, system or all)
  apc:cache:info           Shows APC user & system cache information
  apc:cache:info:file      Shows APC file cache information
  apc:key:delete           Deletes an APC key
  apc:key:exists           Checks if an APC key exists
  apc:key:fetch            Shows the content of an APC key
  apc:key:store            Store an APC key with given value
  apc:sma:info             Show APC shared memory allocation information
opcache
  opcache:configuration    Get configuration information about the cache
  opcache:reset            Resets the contents of the opcode cache
  opcache:status           Show summary information about the opcode cache
  opcache:status:scripts   Show scripts in the opcode cache

Example :

[root@ip-172-31-5-244 ~]# php cachetool.phar opcache:status
+----------------------+---------------------------------+
| Name                 | Value                           |
+----------------------+---------------------------------+
| Enabled              | Yes                             |
| Cache full           | No                              |
| Restart pending      | No                              |
| Restart in progress  | No                              |
| Memory used          | 42.71 MiB                       |
| Memory free          | 85.29 MiB                       |
| Memory wasted (%)    | 0 b (0%)                        |
| Strings buffer size  | 8 MiB                           |
| Strings memory used  | 5.31 MiB                        |
| Strings memory free  | 2.69 MiB                        |
| Number of strings    | 103847                          |
+----------------------+---------------------------------+
| Cached scripts       | 1261                            |
| Cached keys          | 2748                            |
| Max cached keys      | 7963                            |
| Start time           | Thu, 08 Feb 2018 02:28:56 +0000 |
| Last restart time    | Thu, 08 Feb 2018 03:10:19 +0000 |
| Oom restarts         | 0                               |
| Hash restarts        | 0                               |
| Manual restarts      | 1                               |
| Hits                 | 47839                           |
| Misses               | 1269                            |
| Blacklist misses (%) | 0 (0%)                          |
| Opcache hit rate     | 97.415899649752                 |
+----------------------+---------------------------------+
[root@ip-172-31-5-244 ~]#
[root@ip-172-31-5-244 ~]#
[root@ip-172-31-5-244 ~]# php cachetool.phar opcache:reset
[root@ip-172-31-5-244 ~]#
[root@ip-172-31-5-244 ~]#
[root@ip-172-31-5-244 ~]# php cachetool.phar opcache:status
+----------------------+---------------------------------+
| Name                 | Value                           |
+----------------------+---------------------------------+
| Enabled              | Yes                             |
| Cache full           | No                              |
| Restart pending      | No                              |
| Restart in progress  | No                              |
| Memory used          | 10.43 MiB                       |
| Memory free          | 117.57 MiB                      |
| Memory wasted (%)    | 0 b (0%)                        |
| Strings buffer size  | 8 MiB                           |
| Strings memory used  | 545.69 KiB                      |
| Strings memory free  | 7.47 MiB                        |
| Number of strings    | 103847                          |
+----------------------+---------------------------------+
| Cached scripts       | 0                               |
| Cached keys          | 0                               |
| Max cached keys      | 7963                            |
| Start time           | Thu, 08 Feb 2018 02:28:56 +0000 |
| Last restart time    | Thu, 08 Feb 2018 03:19:00 +0000 |
| Oom restarts         | 0                               |
| Hash restarts        | 0                               |
| Manual restarts      | 2                               |
| Hits                 | 0                               |
| Misses               | 2                               |
| Blacklist misses (%) | 0 (0%)                          |
| Opcache hit rate     | 0                               |
+----------------------+---------------------------------+

You can notice that the memory, cache keys, hits everything became 0. It's very useful. I also intrigrated it with Ansible easily.

It’s and application for APCU and others stuffs: Check more info here.

Dublin answered 8/2, 2018 at 3:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.