Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Fix Nginx Virtual Host Directory Permissions

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide walks you through correcting file and directory permissions for Nginx virtual hosts (server blocks) so your sites load without 403 Forbidden errors or permission warnings. You’ll learn how to set the right ownership, directory permissions, and file permissions, and how to verify the configuration. You’ll need SSH access to your server and sudo privileges.

What You'll Need

  • SSH access to your Linux server (Ubuntu, Debian, CentOS, etc.)
  • A user account with sudo privileges
  • Knowledge of your web root path (e.g., /var/www/example.com)
  • The username that Nginx runs as (usually www-data on Debian/Ubuntu, nginx on RHEL/CentOS)

Step-by-Step

  1. Identify the Nginx user. Run ps aux | grep nginx or check the Nginx config with grep -R "^user" /etc/nginx/nginx.conf. The output will show the user (e.g., www-data).
  2. Locate your virtual host document root. Check your site’s server block config in /etc/nginx/sites-available/ (or /etc/nginx/conf.d/). Look for the root directive, e.g., root /var/www/example.com;.
  3. Set ownership to the Nginx user. Run sudo chown -R www-data:www-data /var/www/example.com (replace with your actual path and user). This ensures Nginx can read and write files if needed.
  4. Set directory permissions to 755. Run sudo find /var/www/example.com -type d -exec chmod 755 {} \;. This gives the owner (www-data) read/write/execute, and others read/execute.
  5. Set file permissions to 644. Run sudo find /var/www/example.com -type f -exec chmod 644 {} \;. Files are readable by everyone, writable only by the owner.
  6. Verify permissions. Use ls -la /var/www/example.com to confirm directories show drwxr-xr-x and files show -rw-r--r--.
  7. Test the configuration. Run sudo nginx -t to check for syntax errors. If OK, reload Nginx with sudo systemctl reload nginx (or sudo service nginx reload).
  8. Check the site in a browser. Visit your domain. If you still get a 403, check the Nginx error log: sudo tail -f /var/log/nginx/error.log.

Common Mistakes

  • Using the wrong user. Setting ownership to your own user (e.g., ubuntu) instead of the Nginx user can cause permission denials.
  • Setting permissions too restrictive. 700 on directories or 600 on files will block Nginx from reading them.
  • Forgetting to apply changes recursively. If you only change the top-level directory, subdirectories and files may still have incorrect permissions.
  • Ignoring parent directory permissions. The directories leading up to your web root (e.g., /var/www) must also be traversable (at least 711).
  • Using SELinux on CentOS/RHEL without adjusting contexts. Even with correct Unix permissions, SELinux can block Nginx. Check with ls -Z and restore context if needed.

Tips

  • Use a dedicated user for each site. For multi-site servers, consider creating a unique user per site and adding Nginx to that user’s group.
  • Set umask for new files. If you create files via FTP or Git, ensure your server’s umask is set to 022 so new files get 644 and directories 755.
  • Use ACLs for more granular control. For complex setups, setfacl can give specific users access without changing ownership.
  • Document your permissions. Keep a note of the desired permissions in your deployment script or README to avoid future mistakes.
  • Test with a simple file. Create a test index.html to confirm the web root is accessible before troubleshooting PHP or other scripts.

Troubleshooting

If you still get a 403 Forbidden:

  • Check the error log: sudo tail -f /var/log/nginx/error.log. It will often say “Permission denied” and point to the exact file.
  • Verify that the index directive in your server block includes a file that exists (e.g., index index.html index.php;).
  • Ensure the web root path is correct and that there are no symlinks pointing to inaccessible locations.
  • If using SELinux, run sudo restorecon -Rv /var/www/example.com or set the correct context: sudo chcon -R -t httpd_sys_content_t /var/www/example.com.

If you get a 500 Internal Server Error:

  • This is often a PHP-FPM permission issue. Check that the PHP-FPM socket or port is accessible and that the user running PHP-FPM can read the files. You may need to set ownership to the PHP-FPM user (often www-data or nginx).

If Nginx fails to start after changing permissions:

  • Run sudo nginx -t to see any syntax errors. If it’s a permission error on a log file, adjust ownership of the log directory.

Frequently Asked Questions

What are the correct permissions for Nginx web directories?

Directories should be 755, files 644, and ownership should be the Nginx user (e.g., www-data). This allows Nginx to read files and traverse directories while keeping write access limited to the owner.

Why do I get a 403 Forbidden even with 755/644 permissions?

Common causes include: the web root path is wrong, the index file is missing, or SELinux is blocking access. Check the error log and SELinux settings.

Should I set ownership to www-data or my own user?

For security, the Nginx process should own the files. If you need to edit files as your own user, add your user to the www-data group and set group ownership, or use ACLs.

How do I fix permission issues for PHP files?

Ensure the PHP-FPM user (often the same as Nginx) has read access to the PHP files. If you use a Unix socket, check that the socket file is writable by both users.

What is the difference between 755 and 775 for directories?

755 gives read/execute to all, but write only to the owner. 775 gives write to the owner and group. Use 775 if you have a group of users who need to write to the directory.

How do I apply permissions to a new site quickly?

Use a one-liner: sudo chown -R www-data:www-data /var/www/example.com && sudo find /var/www/example.com -type d -exec chmod 755 {} \; && sudo find /var/www/example.com -type f -exec chmod 644 {} \;