Getting file permissions right is one of the most common stumbling blocks when deploying a PHP application on CentOS or RHEL. Apache runs as theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/gavafue/registroComponentesMultimedia/llms.txt
Use this file to discover all available pages before exploring further.
apache user and group — it must be able to read every PHP and static file in the document root, and it must be able to write session files to api/sesiones/. On top of standard Unix permissions, CentOS 9 enforces SELinux security policies that can silently block writes even when the Unix permissions look correct. This guide explains how to handle both layers.
Why Permissions Matter
Two things can go wrong independently:- Unix ownership / mode — If files are owned by another user or have restrictive modes, Apache returns HTTP 403 Forbidden.
- SELinux context — Even with correct Unix permissions, SELinux can deny Apache write access to directories it has not been explicitly told to trust, causing authentication failures (HTTP 401) or session errors that are only visible in
/var/log/httpd/error_log.
api/fix_auth.sh to automate all four repair steps in one go.
Using fix_auth.sh (Recommended for CentOS 9 / RHEL)
What the Script Does
The script performs four numbered steps:Set owner and base permissions
Recursively sets ownership of the entire
/var/www/html tree to apache:apache, then applies safe base permissions: 755 on all directories and 644 on all files. This ensures Apache can traverse directories and read files without exposing write access to the world.Secure critical files
Two paths receive special treatment after the base pass:
api/sesiones/is set to775so theapachegroup can write session files into it.api/config.phpis set to600(owner-read/write only) so the database credentials are not readable by other system users.
Repair SELinux contexts
If SELinux is enabled (i.e.,
getenforce does not return Disabled), the script:- Runs
restorecon -Rto reset the entire document root to its default SELinux context (httpd_sys_content_t). - Applies the
httpd_sys_rw_content_ttype toapi/sesiones/so Apache is explicitly allowed to create and modify files there. - Enables two SELinux booleans:
httpd_can_network_connect— allows Apache to open outbound network connections (needed if the app ever calls external services).httpd_graceful_shutdown— allows Apache to perform graceful restarts under SELinux enforcement.
Running the Script
After copying the project files to/var/www/html, run the script with sudo:
[OK] line after each phase means it succeeded. A red [ERROR] line means something went wrong — check the message and correct it before re-running.
Full Script Listing
Manual Steps (XAMPP on Linux or Windows)
fix_auth.sh is designed for a CentOS / RHEL system with Apache running as the apache user. For XAMPP environments, apply permissions manually.
Create the sesiones directory if it does not exist
api/config.php automatically calls mkdir() on startup if api/sesiones/ is missing, but the directory must be writable by the web server user for that call to succeed. Pre-creating it is the safest approach.Checking SELinux Status
To see whether SELinux is active on your server:| Output | Meaning |
|---|---|
Enforcing | SELinux is active and blocking unauthorised access. Run fix_auth.sh or the manual semanage/chcon commands. |
Permissive | SELinux logs violations but does not block them. Useful for debugging. |
Disabled | SELinux is off. The script skips all SELinux steps automatically. |
Viewing Apache Error Logs
If the application returns HTTP 401, 403, or 500 errors after deployment, inspect the Apache error log in real time:AVC entries in this log (or in /var/log/audit/audit.log) and look like:
api/sesiones, re-run fix_auth.sh or apply the chcon command from Step 3 manually.
If
api/sesiones/ does not exist at all when fix_auth.sh runs, the script will warn you and skip the SELinux context step for that directory. Create the directory first, then re-run the script: