How to Fix “ulimit: error setting limit (Operation not permitted)” in Apache2 on Debian


2 views

When working with Apache 2.2.22-9 on Debian Wheezy, you might encounter this persistent warning during server operations:

/usr/sbin/apache2ctl: 87: ulimit: error setting limit (Operation not permitted)

While this message appears during common operations like:

  • apache2ctl configtest
  • service apache2 reload
  • service apache2 restart

The server continues to function normally, as confirmed by:

# service apache2 status
Apache2 is running (pid 32045).

The issue stems from line 87 in /usr/sbin/apache2ctl where the script attempts to increase file descriptor limits using:

ulimit -n 8192

This fails because:

  1. On modern systems, these limits should be configured via systemd or init scripts
  2. The Apache process may not have permission to modify its own limits
  3. Debian's default configuration already sets appropriate limits

Option 1: Modify apache2ctl (Quick Fix)

Edit the script to comment out the problematic line:

sudo sed -i 's/^ulimit/## ulimit/' /usr/sbin/apache2ctl

Or use a more surgical approach with awk:

sudo awk '{ if (NR == 87) print "## "$0; else print $0 }' /usr/sbin/apache2ctl > /tmp/apache2ctl.new \
&& sudo mv /tmp/apache2ctl.new /usr/sbin/apache2ctl \
&& sudo chmod 755 /usr/sbin/apache2ctl

Option 2: Proper System Configuration

For systemd systems (Debian 8+), create a custom override:

sudo mkdir -p /etc/systemd/system/apache2.service.d
sudo tee /etc/systemd/system/apache2.service.d/limits.conf <<EOF
[Service]
LimitNOFILE=8192
EOF
sudo systemctl daemon-reload

Option 3: SysVInit Configuration (Legacy Systems)

For older Debian versions using init.d:

sudo tee /etc/default/apache2 <<EOF
# Set ulimit for file descriptors
ulimit -n 8192
EOF

After making changes, verify the limits are properly set:

cat /proc/$(pgrep apache2 | head -1)/limits | grep "Max open files"

You should see output similar to:

Max open files            8192                8192                files

In most cases, this warning is harmless because:

  • Debian's default Apache configuration already includes reasonable limits
  • Modern systems handle file descriptors more efficiently
  • The effective limits are often higher than what the script attempts to set

However, for production systems handling heavy traffic, implementing the proper system configuration (Option 2) is recommended for optimal performance.


When working with Apache 2.2.22-9 on Debian Wheezy systems, you might encounter this persistent but non-fatal error during server operations:

/usr/sbin/apache2ctl: 87: ulimit: error setting limit (Operation not permitted)

The error appears during common operations like:

apache2ctl configtest
service apache2 reload
service apache2 restart

This occurs when the Apache init script attempts to modify system limits (via ulimit) without sufficient privileges. The ulimit command in /usr/sbin/apache2ctl line 87 tries to set resource limits that the current user isn't permitted to change.

Here are three approaches to resolve this:

1. Modify the apache2ctl script (recommended):

sudo nano /usr/sbin/apache2ctl

Locate the ulimit line (around line 87) and either:

# Comment it out:
# ulimit -n $MAX_FDS 2>/dev/null

# OR make it conditional:
if [ "$(id -u)" = "0" ]; then
    ulimit -n $MAX_FDS 2>/dev/null
fi

2. Configure system limits properly:

sudo nano /etc/security/limits.conf

Add these lines:

www-data soft nofile 8192
www-data hard nofile 16384

3. Alternative systemd approach (for newer systems):

sudo systemctl edit apache2.service

Add:

[Service]
LimitNOFILE=16384

After making changes, verify with:

# For method 1:
sudo apache2ctl configtest

# For method 2:
su - www-data -c 'ulimit -n'

# For method 3:
systemctl show apache2 --property LimitNOFILE

The error is essentially cosmetic - Apache continues to function normally. The solutions either:

  • Remove the problematic ulimit call when not running as root
  • Properly set the limits through system configuration
  • Use modern systemd mechanisms for resource control