Optimal Web Server Root Directory Location in Linux: Apache/Nginx Best Practices vs FHS Standards


2 views

Most Linux distributions default to /var/www/html for Apache and /var/www for Nginx. This convention emerged organically despite not being officially specified in the Filesystem Hierarchy Standard (FHS). The typical structure looks like:


/var/www/
├── example.com
│   ├── public_html
│   └── logs
└── subdomain.example.com
    └── public_html

The /var directory exists specifically for variable data files. Web content qualifies because:

  • It changes frequently during deployments
  • Logs grow dynamically
  • Uploaded content appears at runtime

Example Apache VirtualHost configuration:


<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    DocumentRoot /var/www/example.com/public_html
    ServerName example.com
    ErrorLog /var/www/example.com/logs/error.log
    CustomLog /var/www/example.com/logs/access.log combined
</VirtualHost>

Many developers prefer /home/user/www for local development because:

  1. Simpler permissions (user-owned)
  2. Easier integration with development tools
  3. Separation from system packages

To configure this in Nginx:


server {
    listen 80;
    server_name local.dev;
    root /home/developer/projects/webapp/public;
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
}

Key permission settings for production:


# For /var/www
sudo chown -R www-data:www-data /var/www
sudo chmod -R 755 /var/www

# For /home directories
sudo usermod -a -G www-data $USER
chmod 711 /home/$USER

With Docker, the host directory becomes flexible:


version: '3'
services:
  webserver:
    image: nginx:alpine
    volumes:
      - ./project:/usr/share/nginx/html
    ports:
      - "8080:80"

While FHS doesn't explicitly define web root locations, section 5.12 in FHS 3.0 states:

"/var/www may be provided as a location for default web server content"

This makes /var/www a de facto standard despite not being mandatory.


Both Apache and Nginx conventionally use /var/www as their default web root directory in most Linux distributions. This has become a de facto standard despite not being formally specified in the Filesystem Hierarchy Standard (FHS).

The /var directory is designated for variable data files according to FHS. Web content that changes frequently fits this category perfectly. Server configuration examples:

# Apache virtual host example
<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example.com/public_html
</VirtualHost>

# Nginx server block example
server {
    listen 80;
    server_name example.com;
    root /var/www/example.com/html;
}

While /var/www is standard, other locations have valid use cases:

  • /srv/www - For servers where web content is the primary service
  • /home/user/www - Common in development environments or shared hosting
  • /opt/site - For self-contained web applications

Proper permissions are crucial for security. A typical setup:

sudo mkdir -p /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/example.com/public_html
sudo chmod -R 755 /var/www

Containerized deployments often use different approaches:

# Docker example using custom web root
FROM nginx:alpine
COPY ./site-content /usr/share/nginx/html/custom-path