For any regular hosting for to setup any laravel or wordpress website I do this type of permission
sudo chown -R apache:apache /var/www/html
sudo find /var/www/html -type f -exec chmod 644 {} \;
sudo find /var/www/html -type d -exec chmod 755 {} \;
But in SELinux any directory (and its contents) should have a read-write context for Apache. For for public dir or any dir that needs file uploads should have something like below:
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/public(/.*)?"
restorecon -Rv "/var/www/html/public"
setfacl -R -m u:apache:rwX /var/www/html/public
These three lines are all about making a directory writable by Apache under SELinux, but each line plays a slightly different role. Letâs break them down carefully.
1ī¸âŖ semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/public(/.*)?"
-
Purpose: Tells SELinux that this directory (and its contents) should have a read-write context for Apache.
-
Components:
-
semanage fcontextâ manage file contexts (SELinux labels). -
-aâ add a new rule. -
-t httpd_sys_rw_content_tâ type: read-write content for web servers. -
"/var/www/html/public(/.*)?"â regex pattern:/var/www/html/publicâ the directory itself(/.*)?â all files and subdirectories under it
-
-
Effect: SELinux knows that files here are allowed to be written by Apache/PHP.
Think of it as marking the folder for write permissions in SELinuxâs eyes.
2ī¸âŖ restorecon -Rv "/var/www/html/public"
-
Purpose: Applies the SELinux context physically to the filesystem.
-
Components:
restoreconâ restore the SELinux context on a file/directory to whatsemanagedefined.-Râ recursively apply to all subdirectories/files.-vâ verbose, so you see what changed.
-
Effect: Actually sets
httpd_sys_rw_content_ton/var/www/html/publicand everything inside.
Without this, semanage just records the rule, it doesnât change existing files.
3ī¸âŖ setfacl -R -m u:apache:rwX /var/www/html/public
-
Purpose: Grants actual Linux permissions (Access Control List) for the Apache user.
-
Components:
-
setfaclâ set file ACLs (extra permissions beyond standardchmod). -
-Râ recursively. -
-m u:apache:rwXâ modify the ACL for userapache:râ readwâ writeXâ execute only if itâs a directory or already executable
-
-
Effect: Ensures Apache can read/write/create files regardless of regular ownership/permissions.
â TL;DR
| Command | Role |
|---|---|
semanage fcontext |
Tells SELinux this directory is writable for Apache. |
restorecon |
Applies the SELinux label to existing files/folders. |
setfacl |
Gives Apache Linux-level write permission (ACL), independent of ownership. |
Together, these three lines guarantee that both SELinux and Linux permissions allow Apache/PHP to create or modify files in /var/www/html/public.
Self Promotion
Since 2011, Codeboxr has been transforming client visions into powerful, user-friendly web experiences. We specialize in building bespoke web applications that drive growth and engagement. Our deep expertise in modern technologies like Laravel and Flutter allows us to create robust, scalable solutions from the ground up. As WordPress veterans, we also excel at crafting high-performance websites and developing advanced custom plugins that extend functionality perfectly to your needs. Letâs build the advanced web solution your business demands.
