Opcache issues resolved for symlink based atomic deploys and PHP-FPM

I recently ran into problems with php-fpm and opcache using symbolic links for atomic deploys. The solution was so simple, use $realpath_root instead of $document_root in your fastcgi config. Thank you nginx, you make me feel all warm and fuzzy inside.

fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;

Details below —

Folder structure:

├── current -> deploy1
├── deploy1
│   └── www/index.php
└── deploy2
    └── www/index.php

The problem:

Opcache does not use filesystem inodes when it saves file paths. What this means is when you flip the “current” symlink from “deploy1” to “deploy2”, the opcache thinks php-fpm requests are still referencing deploy1/www/index.php because the file path it has saved is current/www/index.php rather than the “real” file path.

The solution:

Have nginx to resolve the symbolic link before the request even gets to php-fpm:

fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;

Full disclosure, I found the above solution here: http://stackoverflow.com/a/23904770

I still recommend that you clear Opcache after doing a deploy and flipping the symlink, but this solution does not require you to do that.

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>