📚 Encyclopedia

Servers & Hosting · updated 2026-10-13

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

See also

Linked from

Sources: global CLAUDE.md server notes, infra memory (server-setup-gotchas), plumb memory (deployment), tldr post #1555