Ravindra BagaleCourses & study guides

Chapter 6: Web Servers — Nginx and Apache

6.6 CentOS Stream 9

Nginx on CentOS Stream 9

sudo yum install -y nginx
sudo service nginx start
sudo systemctl enable nginx
sudo service nginx status
sudo nginx -t
curl -I http://localhost

Apache on CentOS Stream 9

sudo yum install -y httpd
sudo service httpd start
sudo systemctl enable httpd
sudo service httpd status
sudo apachectl configtest
curl -I http://localhost          # shows the CentOS test page until you add index.html

firewalld (only if it is running)

CentOS cloud images may or may not have firewalld running. Check, and if active, open HTTP/HTTPS:

sudo systemctl is-active firewalld
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-all

If firewalld is inactive or not installed, the AWS security group alone controls access — that is fine.

SELinux basics

Dhyan do — SELinux is the number one reason why something that works on Ubuntu gives "403 Forbidden" or "502 Bad Gateway" on CentOS. It is not your enemy; it is an extra security guard. Let's learn how to talk to it.

CentOS Stream runs SELinux in enforcing mode. SELinux labels every file and process; a web server may only read files labelled httpd_sys_content_t (both Nginx and Apache run in the httpd_t domain).

getenforce                                  # Enforcing / Permissive / Disabled
ls -Z /usr/share/nginx/html /var/www/html   # view SELinux labels
sudo restorecon -Rv /var/www/html           # fix labels after copying/moving files
sudo setsebool -P httpd_can_network_connect 1      # allow reverse proxy to apps (Node/Flask)
sudo setsebool -P httpd_can_network_connect_db 1   # allow web server/PHP to reach a remote DB
sudo ausearch -m avc -ts recent             # see recent SELinux denials (package audit)

Do not simply disable SELinux

Turning SELinux off (setenforce 0) "fixes" 403/502 errors but removes an important security layer. Fix the label or boolean instead. Temporarily using sudo setenforce 0 only to confirm that SELinux is the cause is fine — then set it back with sudo setenforce 1.