How to Fix “getpwnam(‘www’) failed” Error in Nginx Configuration on Ubuntu


1 views

The error message getpwnam("www") failed occurs when Nginx tries to start using a specified user account that doesn't exist on your system. This is a common issue when copying configuration files from different environments without proper adaptation.

In many Nginx examples (including the official FullExample), the configuration specifies:

user www www;
worker_processes 4;
pid /var/run/nginx.pid;

The problem arises because:

  • The user 'www' isn't created by default on Ubuntu systems
  • Nginx needs a valid system user to run worker processes
  • Permissions and ownership need to be properly configured

Option 1: Create the www user

sudo adduser --system --no-create-home --group www

Option 2: Use an existing user (recommended for Ubuntu)

Modify your nginx.conf:

user www-data www-data;
# Create www user if needed
sudo adduser --system --no-create-home --group www

# Set proper permissions
sudo chown -R www:www /var/lib/nginx
sudo chown -R www:www /var/log/nginx

# Verify the configuration
sudo nginx -t

# Restart Nginx
sudo service nginx restart

Here's a properly configured nginx.conf snippet for Ubuntu:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # Additional configuration...
}

The error message getpwnam("www") failed occurs when Nginx attempts to switch to a specified user account during startup but cannot locate the user in the system's user database. This is a critical failure because Nginx requires proper user permissions to run securely.

The sample configuration you referenced defaults to using the www user, which doesn't exist in a standard Ubuntu 12.04 installation. Unlike some other Linux distributions, Ubuntu typically uses www-data as the default web server user.

Modify your /etc/nginx/nginx.conf to use the existing www-data user instead:


user www-data;
worker_processes 4;
pid /run/nginx.pid;

After making the change, test your configuration:


sudo nginx -t
sudo service nginx restart

If you specifically need the www user, you can create it:


sudo adduser --system --no-create-home --disabled-login --disabled-password --group www

For production environments, consider these additional security measures:


user www-data;
worker_processes auto;
worker_rlimit_nofile 100000;

events {
    worker_connections 4096;
    multi_accept on;
    use epoll;
}

To verify user existence and permissions:


# Check if user exists
id www-data

# Verify user permissions
namei -l /var/www/html

This issue stems from differences between Linux distributions. While RedHat-based systems often use nginx or www as the default user, Debian-based systems like Ubuntu use www-data. Always check your distribution's documentation when configuring services.