Best Practise to activate php file edits when the server runs with opcache?

Marcus

Well-known member
I modified a php file with sed, but the php-fpm 5.5 opcache was still processing the old file from cache. Maybe with sed just the content changes, but not the "last modified date" file attribute?

When I restarted php-fpm, the modified file was processed.

What's the best common practise for this? To restart php after file changes?
 
It all depends on the following two settings:
Code:
opcache.validate_timestamps
opcache.revalidate_freq

as to why the opcache is serving "stale" content. What are those settings set for on your server?
 
This is the opcache output of phpinfo
Code:
# php -i  | grep opcache
(...)
opcache.consistency_checks => 0 => 0
opcache.dups_fix => Off => Off
opcache.enable => On => On
opcache.enable_cli => Off => Off
opcache.enable_file_override => Off => Off
opcache.error_log => no value => no value
opcache.fast_shutdown => 1 => 1
opcache.file_update_protection => 2 => 2
opcache.force_restart_timeout => 180 => 180
opcache.inherited_hack => On => On
opcache.interned_strings_buffer => 8 => 8
opcache.load_comments => 1 => 1
opcache.log_verbosity_level => 1 => 1
opcache.max_accelerated_files => 4000 => 4000
opcache.max_file_size => 0 => 0
opcache.max_wasted_percentage => 5 => 5
opcache.memory_consumption => 128 => 128
opcache.optimization_level => 0xFFFFFFFF => 0xFFFFFFFF
opcache.preferred_memory_model => no value => no value
opcache.protect_memory => 0 => 0
opcache.restrict_api => no value => no value
opcache.revalidate_freq => 2 => 2
opcache.revalidate_path => Off => Off
opcache.save_comments => 1 => 1
opcache.use_cwd => On => On
opcache.validate_timestamps => On => On
 
Last edited:
Those settings state that every 2 seconds check for changed timestamps on PHP files and if changed re-cache. Did the timestamp of the modified file actually change? Did you still see the "stale" contents being served after 2 seconds of saving the modified file?
 
I modified a php file so it redirects the output. The next morning I just checked it up whether it improved speed, and noticed the php file did not redirect the output. So there were some hours between the file change with "sed ..." and the time when I executed the php file.

Then I looked the php file up, but the edit was there. #service php-fpm restart then made the change happen.

I am not sure whether sed changes just the content or also the file properties.
 
I have just created the following PHP file
Code:
<?php

echo "This is a test for line 1<br>";
echo "This is a test for line 2";
and loaded the URL in my browser. Then I ran the following:
Code:
sed -i 's/for/on/g' 1.php
and reloaded the browser. The changes were instant.

My opcache settings for validation are
Code:
opcache.validate_timestamps=1
opcache.revalidate_freq=1

Can you try this?
 
There was another file edit made by a ruby and php did not recognize this at all even after some minutes later, so it's not sed. I am fine with restarting php-fpm, maybe it's just a weird setup (although a hugely used one).
 
Code:
# cat > test.php
<?php

echo "This is a test for line 1<br>";
echo "This is a test for line 2";
^C

# php test.php
This is a test for line 1<br>This is a test for line 2

# sed -i 's/for/on/g' test.php
# php test.php
This is a test on line 1<br>This is a test on line 2

That also works from the "browser" with php-fpm. I have no idea why my other php files were still in the (op?)cache. That's a very nice solution to test this issue, thanks for sharing!!
 
Back
Top Bottom