Linux Web Hosting: Best Practices for /var/www vs /usr/web Directory Placement


3 views

When setting up web servers on Linux systems, one of the first decisions administrators face is where to place website files. The traditional /var/www/ location competes with the more application-oriented /usr/web/ approach. Let's examine the technical merits of each.

The Linux FHS provides clear guidelines:

/var - Variable data files (logs, spools, temporary files)
/usr - Read-only user data and applications

This distinction suggests:

  • /var/www: Best for static content or when files change frequently
  • /usr/web: Better for application code that remains relatively static

For /var/www/example.com:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example.com/public_html
    <Directory /var/www/example.com/public_html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

For /usr/web/example.com:

server {
    listen 80;
    server_name example.com;
    root /usr/web/example.com/public;
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    }
}

Key factors in your decision:

  1. Deployment method: Containerized apps often use /usr while traditional hosting favors /var
  2. Update frequency: Code that changes rarely belongs in /usr
  3. Backup strategy: /var typically requires more frequent backups

Contemporary practices suggest:

  • Containerized apps: /app or /srv directories
  • Cloud deployments: Often use application-specific paths like /opt/company/app
  • CI/CD pipelines: May deploy to temporary directories before final placement

Sample commands for proper permissions:

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

# For /usr/web
sudo chown -R deploy:www-data /usr/web/example.com
sudo chmod -R 750 /usr/web/example.com
sudo chmod g+s /usr/web/example.com

For most web applications today, I recommend:

/srv/www/example.com  # For traditional web hosting
/opt/company/webapp   # For self-contained applications

These locations avoid the /usr vs /var debate while following modern conventions.


The Linux Filesystem Hierarchy Standard (FHS) defines specific purposes for different directories:

  • /usr/: Contains read-only application files and static data
  • /var/: Stores variable data files that change during normal operation

The conventional location is /var/www/, and there are good reasons for this:

# Typical Apache configuration
DocumentRoot /var/www/html
<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

However, for web applications (like those built with Node.js, Django, or Ruby on Rails), /usr/local/webapps/ might be more appropriate:

# Example for a Node.js app
/usr/local/webapps/myapp/
├── package.json
├── node_modules/
└── src/

When deciding between locations, consider these factors:

  • Static vs Dynamic Content: Static sites fit well in /var/www/, while applications may belong in /usr/local/
  • Package Management: System-packaged web apps often install to /usr/share/
  • Security: /var/ typically has more restrictive permissions

For a PHP application using /var/:

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

For a Python web app in /usr/local/:

sudo mkdir -p /usr/local/webapps/myapp
sudo chown -R deploy:deploy /usr/local/webapps/myapp
sudo chmod -R 775 /usr/local/webapps

Some contemporary approaches include:

  • Using containerized deployments that abstract filesystem concerns
  • Cloud-native patterns with ephemeral storage
  • Service-specific directories like /srv/

The /srv/ directory is worth special mention as it's specifically for site-specific data:

# Example structure
/srv/
├── http/
│   └── example.com/
└── https/
    └── api.example.com/