Sub topic 14
Service Users, File Permissions, and Blocking Hidden Files
Assume you know basic Linux ownership, permissions, HTTP, and how Nginx forwards requests to PHP-FPM or Gunicorn. This chapter focuses on reducing the damage caused by a compromised application or a configuration mistake.
Run services as unprivileged users
A service user is a Linux account used only by a daemon or application. It should not be allowed to log in interactively or modify the whole server.
Common service users include:
| Service | Typical user | Main access needed |
|---|---|---|
| Nginx | `www-data` | Read public files |
| PHP-FPM | `www-data` or a pool-specific user | Read PHP code and write selected data |
| Gunicorn | `www-data` or `myapp` | Read application code and write selected data |
Set ownership deliberately. The application user should own files that it must update, such as an upload directory. It should not own the entire operating system or private configuration files.
sudo chown -R myapp:myapp /srv/myapp
sudo find /srv/myapp -type d -exec chmod 755 {} \;
sudo find /srv/myapp -type f -exec chmod 644 {} \;
sudo install -d -o myapp -g myapp -m 750 /srv/myapp/var/uploads
sudo chmod 640 /srv/myapp/.envThe 755 mode lets the owner write directories while others can read and enter them. The 644 mode lets only the owner modify files. The .env file contains application settings or secrets, so it should be readable only by the service that needs it.
If Nginx reads files owned by myapp, Nginx needs permission to traverse every parent directory and read the files. Directory traversal means having the execute bit, represented by x, on each directory in the path.
Protect sockets and sensitive paths
A Unix socket is a special filesystem entry used for local communication. Its owner and group determine which processes can connect to PHP-FPM or Gunicorn.
For a PHP-FPM pool, a restrictive configuration can look like this:
listen = /run/php/myapp.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660Mode 0660 allows the owner and group to use the socket, but denies access to unrelated users. Avoid mode 0666, which permits every local user to connect.
Nginx should also reject hidden files, except for the ACME path commonly used to prove domain ownership during certificate issuance.
location ~ /\.(?!well-known) {
deny all;
access_log off;
log_not_found off;
}This blocks requests such as /.env, /.git/config, and /.ssh/id_rsa. Blocking requests is a backup. Keep sensitive files outside the document root whenever possible.
Check the complete request path
Use namei to inspect permissions on every directory component:
namei -l /srv/myapp/public/index.php
sudo -u www-data test -r /srv/myapp/public/index.php
echo $?A result of 0 means the test user can read the file. A 403 Forbidden response often means Nginx cannot traverse a directory or read a file. A 502 Bad Gateway response can mean Nginx cannot connect to the application socket, often because the socket ownership or path is wrong.
Exercise
Why should an application not own every file under the server's document root?
Show answer
A compromised application could modify its code, expose secrets, or replace files served by Nginx. Give it write access only to directories that require it.