4

I have two websites with two codebases, yet when I change one codebase, I see the change in both.

I have two checkouts of the same website. They are set up to use PHP-FPM through Apache2 with FastCGI. The checkouts are in:

/var/www/site1
/var/www/site2

The Apache configs look like this:

<VirtualHost *:80>
    ServerName site1.myserver.com
    DocumentRoot /var/www/site1
    <IfModule mod_fastcgi.c>
        AddHandler php5-fcgi-handler .php
        Action php5-fcgi-handler /php5-fcgi-uri
        Alias /php5-fcgi-uri fcgi-application
        FastCgiExternalServer fcgi-application -socket /var/run/site1-fpm.sock -pass-header Authorization -idle-timeout 30000 -flush
    </IfModule>
</VirtualHost>

<VirtualHost *:80>
    ServerName site2.myserver.com
    DocumentRoot /var/www/site2
    <IfModule mod_fastcgi.c>
        AddHandler php5-fcgi-handler .php
        Action php5-fcgi-handler /php5-fcgi-uri
        Alias /php5-fcgi-uri fcgi-application
        FastCgiExternalServer fcgi-application -socket /var/run/site2-fpm.sock -pass-header Authorization -idle-timeout 30000 -flush
    </IfModule>
</VirtualHost>

The FPM pool configs look like these:

[site1]
user = site1-user
group = site1-group
listen = /var/run/site1-fpm.sock
listen.owner = www-data
listen.group = www-data
chdir = /
pm = ondemand
pm.max_children = 5
pm.max_requests = 500 ;default to unlimited

[site2]
user = site2-user
group = site2-group
listen = /var/run/site2-fpm.sock
listen.owner = www-data
listen.group = www-data
chdir = /
pm = ondemand
pm.max_children = 5
pm.max_requests = 500 ;default to unlimited

I am not using the chroot feature of FPM (as far as I can tell).

When I change the code for site1 and restart PHP-FPM and visit site1 and site2 then I see the change on both sites.

If I restart PHP-FPM and visit site2 first (the unchanged site), followed by site1 then I don't see the change on either site.

What is wrong with my configs? I do see separate PHP-FPM processes running for site1 and site2 under the main FPM master.

11

When I've seen similar issues in our environments, it appears to have been because of the way that OpCache (by default) shares a single cache across all users on a shared hosting environment. A bug has been submitted (and you can, and should, go and vote to let the maintainers know how important this might be for your use case) though no commitment has been made on delivering a fix.

TL;DR : By default when OpCache is enabled, the cache that is used to store compiled byte-code is shared across all users. In an environment where hosting is shared between multiple sites / users, this can result in a site grabbing the cached output of php scripts from another site or, if specific security settings are enabled even generating errors.

If you plan on using PHP-FPM with PHP 5.5+'s built in opcache, please read the blog post below before you actually do that. Turns out that the opcode cache can be read by any user on the server. This means that if there are say, 10 separate users, with their own vhosts and directories, and you configure one PHP-FPM pool per user, each user can still see what scripts are cached and their locations. Since they have read access to the cache they could potentially view all this data.

This is obviously a massive security concern, and even if no one exploits this, there is still a chance of scripts being read by the wrong user when generating a page, so websites could possibly be displaying the wrong data / info if there are multiple index.php scripts in the cache.

Although no fix has been officially released, if you're using cPanel, this wiki has a documented way of configuring the php-fpm pools to be created and secured on a per-user basis and if you follow the instructions below as well as the IMPORTANT NOTES at the bottom of this answer you should be able to get the functionality you desire without any errors

That post also documents how you might configure this by hand on a per-site/per-user basis (though I'd wager that might become tedious if you're hosting a lot of sites). If you're not using cPanel, you may need to modify the scripts to specify your individual paths and usernames instead of the variables being used by cPanel's config engine.


IMPORTANT NOTES

During testing and additional research I came across this article which provide a few clarifications that may be relevant to your specific situation:

  1. You need to make sure that the opcache.use_cwd parameter is set to true for your application's configuration of OpCache - it's set to false by default and leaving it set to default will probably cause collisions if you're hosting more than one PHP application on your system:

First of all, probably in each typical project you will have to ensure that the opcache.use_cwd option is set to true. Enabling this setting means that the OpCache engine will look at the full file paths to distinguish between files with the same names. Setting it to false will lead to collisions between files with the same base name.

  1. If you're running an application powered by Zend Framework or another similar framework that makes use of annotations, you ALSO need to ensure that the opcache.load_comments and opcache.save_comments directives are set to true. You should double-check this suggestion with your application / framework documentation as most have now updated their docs with specific instructions on enabling the use of OpCache properly for their systems:

There is also a setting that is important in tools and frameworks that make use of annotations. If you use Doctrine, Zend Framework 2 or PHP Unit, remember to set the opcache.load_comments and opcache.save_comments settings to true. In result, the documentation comments from your files will also be included in the precompiled code generated by OpCache. This setting will allow you to work with annotations without any disruptions.

If your project is based on a specific framework or a web application, it’s always a good idea to check the documentation for any guidelines regarding the OpCache configuration

IMPORTANT NOTES


Hopefully this helped - and if you're using cPanel, drop a comment to let us know how you tackled that portion of the configuration!

  • 1
    I am not using the chroot option in PHP-FPM, yet when I disable the opcode cache this issue disappears. This is very strange.... – Sander Marechal Jul 13 '15 at 11:52
  • 1
    Although the article DOES specifically mention chroot, I've tested this today and it occurs even when chroot isn't enabled. Are you hosting more than one site in your environment that is using Opcache? If so, then you may want to try implementing the fix suggested there. Another option is to ONLY enable OpCache for one site, and disable it for others. Sub-optimal to say the least but at least you'll be able to get it working for your most important site... – Bryan 'BJ' Hoffpauir Jr. Jul 14 '15 at 22:43
  • 1
    Thanks. For now I have simply replaced the built-in opcache with the XCache opcache. That one does work properly. – Sander Marechal Jul 15 '15 at 6:28
  • Glad to hear that option worked for you Sander. We're working with Zend to see if we can get a fix for this escalated in priority as it appears to pretty much rule out using OpCache on any type of shared hosting environment and makes their Zend Server (which I'm a big fan of...) perform less quickly than it otherwise might. – Bryan 'BJ' Hoffpauir Jr. Jul 15 '15 at 9:36
  • Sander a quick follow up - I added a few additional IMPORTANT NOTES to the answer I originally provided. I didn't realize that I had modified these 3 directives in my own environment for another unrelated reason, but NOT having them changed them from their default values of false may be the reason you were unable to get OpCache to behave as expected. I know you've replaced it with XCache so it may not matter enough for you to go through the trouble...but if you DO test it out, could you confirm if updating these directives solved your root problem via a comment here? – Bryan 'BJ' Hoffpauir Jr. Jul 15 '15 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.