Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

My server uses NFS (Network File System), and I'm unable to use PHP's flock() -function. Is there a way to lock files on an NFS or is there a need to do so?

share|improve this question

3 Answers 3

I don't know how the PHP flock() function is implemented, but assuming it's an interface to the flock() syscall, then it does not work at all over NFS. From the flock() manpage:

   flock(2) does not lock files over NFS.  Use fcntl(2) instead: that does work over NFS, given
   a sufficiently recent version of Linux and a server which supports locking.

And, of course, everything what a man page says, no matter how outdated, is the ultimate truth.

share|improve this answer
+1, sarcasm! Item D10 in the NFS FAQ elaborates. –  themel Mar 14 '13 at 12:29

flock() works just fine on Linux NFS, including from PHP. We use it extensively and have tested it thoroughly to verify it's working as desired. Check to see if you're running all of the necessary services on both the client and server. Look for "portmapper" and "rpc.statd". If they're not running, you need to figure out which init script starts them on your distro. On Debian-based distros it's "/etc/init.d/portmap" and "/etc/init.d/nfs-common".

From the client, run "rpcinfo -u $NFSSERVER status" and see if you get a response. On my setup, I get "program 100024 version 1 ready and waiting" as the result.

Oh, also bear in mind that in some circumstances NFS and statd can get upset if both the client and the server don't have reliable hostname entries for each other. Double check /etc/hosts on both machines.

share|improve this answer
I'm not really in a position to alter server specific details. The flock() -function is even disabled from the php.ini, because it would not work, at least that's what I've been told to. –  rFactor Sep 19 '09 at 8:13
up vote 1 down vote accepted

Just wanted to answer to myself. The solution can be found here: http://us3.php.net/manual/en/function.flock.php#82521

share|improve this answer
The second option listed is exactly what I describe: using the built-in lock server in Linux NFS. The troubleshooting steps were designed to determine why it (apparently) wasn't working... –  Insyte Sep 20 '09 at 2:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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