Hardening an Apache Web Server with SELinux & firewalld
Served a site on a non-standard port, resolved SELinux AVC denials without disabling enforcement, and locked the host down with firewalld zones and rich rules.
Environment
- ›RHEL 9.4 (minimal install)
- ›VirtualBox — 2 vCPU / 2 GB RAM
- ›Packages: httpd, firewalld, setroubleshoot, policycoreutils-python-utils
Objectives
- 01Serve a static site on TCP 8080 instead of the default port 80
- 02Diagnose and fix SELinux denials caused by the non-standard port
- 03Restrict inbound access using firewalld zones and a rich rule scoped to one subnet
Walkthrough
Step 1
Install and enable Apache
Installed httpd, started and enabled it so it survives a reboot, and confirmed the default page loads on port 80 first.
$ dnf install -y httpd$ systemctl enable --now httpd$ curl -I http://localhost
Step 2
Move the listener to port 8080
Changed the Listen directive and restarted the service. The restart succeeded, but the socket never came up — journalctl showed httpd exiting with a permission error even though the file permissions were correct.
$ sed -i 's/^Listen 80/Listen 8080/' /etc/httpd/conf/httpd.conf$ systemctl restart httpd$ journalctl -u httpd -n 20 --no-pager
Step 3
Diagnose and fix the SELinux denial
ausearch confirmed an AVC denial for httpd_t trying to bind to a port not labelled http_port_t. Rather than setenforce 0, I added 8080 to the correct SELinux port type and verified the policy with semanage.
$ ausearch -m avc -ts recent$ semanage port -a -t http_port_t -p tcp 8080$ semanage port -l | grep http_port_t$ systemctl restart httpd
Step 4
Lock down access with firewalld
Opened 8080/tcp permanently, then added a rich rule so only the lab's management subnet can reach it — everything else is dropped by the default zone.
$ firewall-cmd --permanent --add-port=8080/tcp$ firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.2.0/24" port port="8080" protocol="tcp" accept'$ firewall-cmd --reload$ firewall-cmd --list-all
Outcomes
- ✓Site reachable on :8080 with SELinux still in enforcing mode
- ✓Confirmed the difference between a firewall block and an SELinux denial when troubleshooting a 'service won't start' issue
- ✓Wrote a one-page runbook for re-applying this config on a fresh VM