The Linux Page

Apache eating file descriptors

Today we again ran in an out of memory error. As we are adding more websites our foot print is growing... Our servers use a VPS that has limited resources and whenever the limit is reached, the system automatically kills processes. How the VPS selects the process to kill isn't clear to me, but twice it has been our database manager meaning that all of a sudden all the websites stop working.

So... We got an extra bit of RAM because our package allowed it, but that's still small.

Now as I was looking at that, I noticed that we had "too many files open" warning as well.

Looking at the open files with lsof I noticed many files opened by Apache. A lot more than I expected... (2,800+)

Searching the Internet I found out that Apache opens one file per Log (custom and error logs) and if you have multiple logs (as in one for HTML page, one for PHP pages, etc.) then each will be opened once. That is, it will be opened once per Apache sub-process. Similarly, one file descriptor is used per configuration file. Feel free to look at the output of your lsof command to determine all the files you have open.

So... say you limit your Apache server to open only 20 sub-processes and you have 20 sites that each have one error and one custom log file you get:

20 x 2 x 20 = 800 files.

One thing, if you do a full redirect, you probably don't need a specific log for that entry. For instance, I rewrite my domain name without anything as instead. That does not require a log. It probably does not even require a document root and the REQUEST_URL definition.

<VirtualHost *:80>
	DocumentRoot /usr/clients/www/
	RewriteEngine on
	RewriteRule ^/(.*)$1 [R=permanent,L]

By tweaking our setup we saved about 500 file descriptors.