SELinux file contexts and booleans on web servers
selinuxapachealma-linuxrestoreconsemanagehttpd
SELinux is enforcing on the Alma 9 server. Apache (and php-fpm running as httpd_t) can
only read files labeled with web content types and can only make network connections the
booleans allow. Most "permissions look right but I still get 403/500" problems on this server
are SELinux, not Unix permissions.
File types that matter
| Type | Meaning |
|---|---|
httpd_sys_content_t |
read-only web content (the default under /var/www) |
httpd_sys_rw_content_t |
web content PHP may write (uploads, SQLite, session dirs) |
user_tmp_t |
files created in /tmp; httpd cannot read them |
The /tmp copy trap
mv keeps a file's label. A file uploaded to /tmp and then moved into the docroot keeps
user_tmp_t and Apache returns 403 for it. After any deploy that stages through /tmp (or
mvs from a home directory), relabel:
restorecon -R /var/www/html/site
Deploy scripts on this server run restorecon as their last step for this reason. cp and
rsync into the directory create new files that inherit the right label, so they are safer.
Inspect labels with ls -Z (or ls -lZd dir).
Writable data directories
For a directory PHP writes to (SQLite DB, sessions, uploaded photos), set the rule persistently
so a later restorecon does not undo it:
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/plumb-data(/.*)?'
restorecon -Rv /var/www/plumb-data
chcon -R -t httpd_sys_rw_content_t dir works immediately but is temporary; a relabel resets
it. Keep runtime data outside the code tree (e.g. /var/www/<app>-data/, owned
apache:apache, mode 750) so rsync --delete deploys never touch it.
Booleans
setsebool -P httpd_can_network_connect_db 1 # PHP to the database
setsebool -P httpd_can_sendmail 1 # PHP mail()
setsebool -P httpd_can_network_connect 1 # outbound HTTP(S): FCM, object storage, LLM APIs
getsebool -a | grep httpd # check
Without httpd_can_network_connect, any outbound API call from PHP fails with a generic
connection error.
Ports
A service bound to a non-standard port that Apache or nginx must reach needs a port label:
semanage port -a -t http_port_t -p tcp 9100. Port 8008 already carries http_port_t.
Debugging
ausearch -m avc -ts recentorjournalctl -t setroubleshootshows denials.- A 403 on a file that
ls -lsays is world-readable: checkls -Zfirst. - Directory permissions still matter too:
mktemp -dcreates mode 700 directories, and rsyncing one onto a docroot gives a 403 for everything (see Deploy scripts with rsync (patterns and traps)).
See also
- Setting up an Alma Linux 9 LAMP server
- nginx in front of Apache (TLS proxy on :8008)
- Apache vhost gotchas (icons alias, manifests, SPA fallback, redirects)
Linked from
- A JSON file as the database (flat-file catalogs)
- Apache vhost gotchas (icons alias, manifests, SPA fallback, redirects)
- Deploy scripts with rsync (patterns and traps)
- Heredoc, stdin and scripted-edit pitfalls
- Hosting one static site per subdomain with a wildcard vhost
- Moving a website to a new server without downtime
- nginx in front of Apache (TLS proxy on :8008)
- Personal finance dashboards: bank aggregation and subscription detection
- PHP's uppercase-S TLS downgrade (CVE-2026-128064) and proving it on the wire
- Release-directory deploys with an atomic symlink swap
- Rotating API keys without locking yourself out
- Sending mail from PHP with Postfix, SPF, DKIM and DMARC
- Setting up an Alma Linux 9 LAMP server