Solved: Fatal Error Unable to create lock file: Bad file descriptor (9)

File Descriptors and Open Files Limit CentOS 7


Some programs like Apache and MySQL requires a higher number of file descriptors.By default its  1024, which is not enough for current high load servers. Which will give you error in apache like “Fatal Error Unable to create lock file: Bad file descriptor (9)“. To solve this problem, here are the steps to increase this limit.

Common errors we get  with file descriptor limits:
a) Fatal Error Unable to create lock file: Bad file descriptor (9)
b) Too many open files (24)
c) Unable to lock file

I) Check default limit:

$ ulimit -a

default will be 1024.

check global limit:

$ cat /proc/sys/fs/file-max

if you get output like 10000 that means  normal user can only open 10000 files in a single login session. To see the hard and soft values,

Hard limit:

$ ulimit -Hn
# To check by user 
$ su <user>
$ ulimit -Hn

Soft Limit:

$ ulimit -Sn
# To check by user
$ su <user>
$ulimit -Sn


II) Changing system-wide file descriptors limits:

a) increase global limit:

$ vi /etc/sysctl.conf 
# edit line 
 fs.file-max = 100000 #set this value as you need


b) Also need to increase hard and soft limit.

$ vi  /etc/security/limits.conf

Add / Edit following lines.

*      soft nproc 10000
*      hard nproc 10000
*      soft nofile 10000
*      hard nofile 10000

Note: If we need to increase limit by user then we need to replace * with username. For example, to increase limit by user apache we do as follows.

apache      soft nproc 10000
apache      hard nproc 10000
apache      soft nofile 10000
apache      hard nofile 10000


c) reload systl

$ sysctl -p
$ systemctl daemon-reload


d) Verify New Limits

Use following command to see max limit of file descriptors:

cat /proc/sys/fs/file-max

Hard Limit

ulimit -Hn

Soft Limit

ulimit -Sn

Fixing 500 internal server error

“500 inernal server error’ can be due to various server and code related issue. To fix ‘500 internal server error’ we have to look into various aspect in the server and code.

1. File permissions / File size issue: 

If server cant read / write / or execute different files or even directories, it throws 500 internal server errors. This can be writing to the file generated by php like log files, image files, or creating directories from php. Same error problem also be created when server services like httpd ( apache), mysql et.c cant save / read / or write logs files associated with it.  To solve the problem, first check the server side ,  so need to see ‘messages’ log file which is generally , inside /var/ log/messages every error / status / alert created by linux system will be copied to this file so just reading it and finding the error and fixing the problem will solve this issue.

2. Tmp folder might be full:

As we know every linux server has tmp folder, which can be just a folder within a drive or new mount to the server.  If this folder is full that will also cause 500 internal server error.

its always better to run tempwatch script in the server to manage the size of the tmp folder, it can be run as a cron job in a server which automatically clears the tmp folder on periodic basics.

3.  .htaccess syntax issue or misconfigured.

if there is any mistake on .htaccess syntax error or if some module related condition or module mention in that htaccess that is not loaded in the server then we will get this error. Best way to solve this problem is to  comment out the htaccess line that you have recently added and reloading the page to see if that is the problem.

4. Mod security or similar security proxies in the server.

Some of the rules in the mod security is set to give out 500 internal server error  in case of match with criteria. So checking the mod security log file can also give the idea if there is any problem there.  If server has other proxy security installed, we need to check it there too.

5. Mis-configuration of apache / nginx/ php

Any web server like apache / nginx misconfiguration can also cause the 500 internal server error.  Commonly, many times we are loading the module on web server that is not installed, or any module needs certain access to the files or folder can also cause this problem. Similarly misconfiguration of php.ini also cause this problem. To solve this problem log files of webserver and error log of php must be checked to get to the root of the problem.

6. php su_exec folder permission:

Many of us now, running php with su_exec for better security option. But this only allows folder to have 755 permission and if you have 777 permission to the folders then we will get 500 internal server error.

7. Bad email gateway:

if your code is trying to send an email and you get 500 internal server error , that means your email protocol is not set properly, so a quick look on this is worth trying.

I suggest to clear the browser cache, and if the server have internal cache like varnish, file cache e.t.c please clear those too and refresh after the problem is fixed, else even the problem is fixed we will get the same response as its being served by cache.

8. PHP and Apache settings:

Make sure to check few php config parameters:
a) upload_max_filesize : If you trying to upload and get 500 error make sure your file size is less than upload_max_filesize value in php.ini, if not change it to required value.

b) max_execution_time: If you script requires longer time to execute like importing xml data, computing large data set e.t.c you can try by increasing these value. If you have set your php as mod_fcgi then you have change two parameters in httpd.conf file or php.fcgi file.

FcgidMaxRequestLen 10737418240
FcgidIOTimeout 3600