Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

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

This was meant to be a comment on janneb's answer above, but I didn't have the reputation at the time:

The manual page was out of date for a long time, but has since been updated to say (emphasis mine):

In Linux kernels up to 2.6.11, flock() does not lock files over NFS (i.e., the scope of locks was limited to the local system). Instead, one could use fcntl(2) byte-range locking, which does work over NFS, given a sufficiently recent version of Linux and a server which supports locking. Since Linux 2.6.12, NFS clients support flock() locks by emulating them as byte-range locks on the entire file. This means that fcntl(2) and flock() locks do interact with one another over NFS. Since Linux 2.6.37, the kernel supports a compatibility mode that allows flock() locks (and also fcntl(2) byte region locks) to be treated as local; see the discussion of the local_lock option in nfs(5).

The official man-pages web site leads to which shows the new version from man-pages 4.00

Doc update happened in 2014:

Linux 2.6.12 was released on 18 June 2005 - almost a decade ago now.

share|improve this answer
up vote 1 down vote accepted

Just wanted to answer to myself. The solution can be found here:

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.