The web server: httpd, virtual hosts, with SELinux and firewalld
This lesson is open to everyone. The other lessons open with a package: sign in or sign up, then choose a package.
Key points
- httpd configuration: the main
/etc/httpd/conf/httpd.conf, our own files in/etc/httpd/conf.d/*.conf; check withhttpd -t(apachectl configtest). - A virtual host: several sites on one IP, told apart by
ServerName:<VirtualHost *:80> ServerName shop.itnuvo.local DocumentRoot /srv/shop </VirtualHost>. - For a new DocumentRoot: directory permissions + a
<Directory>block + the SELinux context (semanage fcontext+restorecon). - The sequence:
dnf install→ configuration →httpd -t→enable --now→firewall-cmd→ SELinux →curl. - Logs:
/var/log/httpd/error_log,access_log; write separate logs for each vhost.
The target architecture
In the simulator's lab world, srv-web01 is the web server, srv-db01 the database and client01 the user. In this unit we fill it with real services:
client01 (10.20.0.50) │ http://www.itnuvo.local, http://shop.itnuvo.local ▼ srv-web01 (10.20.0.15) — httpd: two virtual hosts │ 3306/tcp (8.2), NFS (8.3) ▼ srv-db01 (10.20.0.20) — MariaDB, NFS
Steps 1–3: install, start, open
Familiar from the previous units:
[ali@srv-web01 ~]$ sudo dnf install -y httpd [ali@srv-web01 ~]$ sudo systemctl enable --now httpd [ali@srv-web01 ~]$ sudo firewall-cmd --permanent --add-service=http && sudo firewall-cmd --reload [ali@srv-web01 ~]$ curl -s -o /dev/null -w "%{http_code}\n" http://localhost 403
403 is not surprising: /var/www/html is empty and RHEL's "welcome" page is returned with a 403 code. A browser shows the "Test Page".
Configuration files
| File / directory | What it is |
|---|---|
/etc/httpd/conf/httpd.conf | The main configuration — Listen 80, DocumentRoot "/var/www/html", User apache |
/etc/httpd/conf.d/*.conf | Extra configuration — put your own sites here |
/etc/httpd/conf.modules.d/ | Module loading |
/var/www/html | Default content |
/var/log/httpd/ | access_log, error_log |
On RHEL httpd processes run as the apache user (www-data on Ubuntu).
The first site: in the standard directory
[ali@srv-web01 ~]$ sudo mkdir -p /var/www/www.itnuvo.local [ali@srv-web01 ~]$ echo "<h1>ITNuvo - ana sayt</h1>" | sudo tee /var/www/www.itnuvo.local/index.html [ali@srv-web01 ~]$ sudo tee /etc/httpd/conf.d/www.conf > /dev/null <<'EOF' <VirtualHost *:80> ServerName www.itnuvo.local DocumentRoot /var/www/www.itnuvo.local ErrorLog /var/log/httpd/www-error.log CustomLog /var/log/httpd/www-access.log combined </VirtualHost> EOF [ali@srv-web01 ~]$ sudo httpd -t Syntax OK [ali@srv-web01 ~]$ sudo systemctl reload httpd
A directory created under /var/www gets the SELinux context (httpd_sys_content_t) automatically — no extra work needed.
The second site: /srv/shop — four layers
[ali@srv-web01 ~]$ sudo mkdir -p /srv/shop [ali@srv-web01 ~]$ echo "<h1>ITNuvo Shop</h1>" | sudo tee /srv/shop/index.html [ali@srv-web01 ~]$ sudo tee /etc/httpd/conf.d/shop.conf > /dev/null <<'EOF' <VirtualHost *:80> ServerName shop.itnuvo.local DocumentRoot /srv/shop <Directory /srv/shop> Require all granted </Directory> ErrorLog /var/log/httpd/shop-error.log CustomLog /var/log/httpd/shop-access.log combined </VirtualHost> EOF [ali@srv-web01 ~]$ sudo httpd -t && sudo systemctl reload httpd [ali@srv-web01 ~]$ curl -s -o /dev/null -w "%{http_code}\n" -H "Host: shop.itnuvo.local" http://localhost 403
The configuration is right, but 403. Let us check the four layers:
| Layer | Check | Result |
|---|---|---|
| 1. httpd configuration | The <Directory> block, Require all granted | ✓ (httpd sets Require all denied for / by default — without the block, 403) |
| 2. File permissions | ls -ld /srv/shop, ls -l /srv/shop | ✓ 755 / 644 — apache can read |
| 3. SELinux | ls -Zd /srv/shop | ✗ var_t |
| 4. Firewall | A local request, not affected | — |
[ali@srv-web01 ~]$ ls -Zd /srv/shop unconfined_u:object_r:var_t:s0 /srv/shop [ali@srv-web01 ~]$ sudo semanage fcontext -a -t httpd_sys_content_t "/srv/shop(/.*)?" [ali@srv-web01 ~]$ sudo restorecon -Rv /srv/shop Relabeled /srv/shop from unconfined_u:object_r:var_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0 Relabeled /srv/shop/index.html from unconfined_u:object_r:var_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0 [ali@srv-web01 ~]$ curl -s -H "Host: shop.itnuvo.local" http://localhost <h1>ITNuvo Shop</h1>
This is the most classic RHCSA task — and it is solved without turning SELinux off.
Checking from the client
On client01 we write the names into /etc/hosts (on a real network they would be in DNS):
ali@client01:~$ echo "10.20.0.15 www.itnuvo.local shop.itnuvo.local" | sudo tee -a /etc/hosts ali@client01:~$ curl -s http://www.itnuvo.local <h1>ITNuvo - ana sayt</h1> ali@client01:~$ curl -s http://shop.itnuvo.local <h1>ITNuvo Shop</h1>
The same IP, two different sites — httpd looks at the Host header of the request and chooses the right VirtualHost.
HTTPS (briefly)
[ali@srv-web01 ~]$ sudo dnf install -y mod_ssl [ali@srv-web01 ~]$ sudo firewall-cmd --permanent --add-service=https && sudo firewall-cmd --reload [ali@srv-web01 ~]$ sudo systemctl restart httpd [ali@srv-web01 ~]$ curl -sk -o /dev/null -w "%{http_code}\n" https://localhost 403
mod_ssl opens 443 with a self-signed certificate. In production a real certificate is configured (SSLCertificateFile, SSLCertificateKeyFile); the key file must have mode 600 and the cert_t context.
When there is a problem
[ali@srv-web01 ~]$ sudo tail -n 3 /var/log/httpd/shop-error.log [ali@srv-web01 ~]$ sudo ausearch -m AVC -ts recent -c httpd [ali@srv-web01 ~]$ sudo httpd -S VirtualHost configuration: *:80 is a NameVirtualHost default server shop.itnuvo.local (/etc/httpd/conf.d/shop.conf:1) port 80 namevhost shop.itnuvo.local (/etc/httpd/conf.d/shop.conf:1) port 80 namevhost www.itnuvo.local (/etc/httpd/conf.d/www.conf:1)
httpd -S shows all virtual hosts and which one is the default — when the Host header matches none of them, the first file in alphabetical order (here shop.conf) answers.
Lab: two sites
In the Red Hat simulator, the "Start httpd", "firewalld: open the site to the network" and "SELinux: 403 Forbidden" tasks are the main steps of this lesson — do all three in order. Then on your own VM:
- Build the
www.itnuvo.localsite in/var/www/www.itnuvo.local. - Build the
shop.itnuvo.localsite in/srv/shop:<Directory>, permissions,semanage fcontext. httpd -t,reload,httpd -S.- Open both sites by name from
client01.
Check: two curl commands return two different pages; getenforce → Enforcing.
Check yourself
5 questions · answers are shown instantly · 3 correct answers complete the lesson
1Where should you create your own virtual host file on RHEL?
2Which command checks the syntax of the httpd configuration?
3The site is in /srv/shop, the permissions are 755/644, the Directory block exists, but it returns 403. What is the next step?
4How does httpd tell several sites on one IP apart?
5Which user do httpd processes run as on RHEL?
Comments
Sign in to comment.