Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Know it before you install
One VirtualHost = one website (by name and port). Modules add features (mod_ssl, mod_proxy, mod_rewrite, mod_headers). The MPM decides how Apache handles connections — use event with PHP-FPM.
| Item | RHEL / Rocky / Alma / Fedora | Ubuntu / Debian |
|---|---|---|
| Package | httpd (+ mod_ssl) | apache2 |
| Service | httpd.service | apache2.service |
| Main config | /etc/httpd/conf/httpd.conf | /etc/apache2/apache2.conf |
| Your sites | /etc/httpd/conf.d/*.conf | /etc/apache2/sites-available/ + a2ensite |
| Modules | /etc/httpd/conf.modules.d/ | a2enmod / a2dismod |
| Web root | /var/www/html | /var/www/html |
| Logs | /var/log/httpd/ | /var/log/apache2/ |
| Test config | apachectl configtest | apache2ctl configtest |
| Runs as | user apache | user www-data |
Install on RHEL, Rocky, Alma and Fedora
dnf -y install httpd mod_ssl
systemctl enable --now httpd
firewall-cmd --permanent --add-service={http,https}
firewall-cmd --reload
curl -I http://localhostInstall on Ubuntu and Debian
apt-get update
apt-get install -y apache2
a2enmod ssl headers rewrite
ufw allow 'Apache Full'
systemctl reload apache2First configuration: global hardening
Hide the version, turn off old features, and keep directory listings off:
ServerTokens Prod
ServerSignature Off
TraceEnable Off
FileETag None
Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
<Directory /var/www>
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<FilesMatch "^\.(git|env|ht)">
Require all denied
</FilesMatch>(On Ubuntu put it in /etc/apache2/conf-available/security-extra.conf and run a2enconf security-extra.) Also set ServerName once in the main config to stop the AH00558 warning: ServerName web1.example.com.
A first website (VirtualHost)
<VirtualHost *:80>
ServerName shop.example.com
ServerAlias www.shop.example.com
DocumentRoot /var/www/shop
<Directory /var/www/shop>
AllowOverride FileInfo
Require all granted
</Directory>
ErrorLog /var/log/httpd/shop-error.log
CustomLog /var/log/httpd/shop-access.log combined
</VirtualHost>mkdir -p /var/www/shop
echo '<h1>It works</h1>' > /var/www/shop/index.html
restorecon -Rv /var/www/shop
apachectl configtest
Syntax OK
systemctl reload httpdOn Ubuntu: save it in sites-available/shop.example.com.conf, use /var/log/apache2/…, then a2ensite shop.example.com && a2dissite 000-default && apache2ctl configtest && systemctl reload apache2.
Onthoud
AllowOverride None is faster and safer. Allow .htaccess (AllowOverride FileInfo or All) only for apps that need it, like WordPress.
PHP with PHP-FPM
PHP-FPM runs PHP in its own service; Apache passes .php files to it.
dnf -y install php-fpm php-mysqlnd php-gd php-mbstring php-xml php-opcache
systemctl enable --now php-fpm
systemctl reload httpd
apt-get install -y php-fpm php-mysql php-gd php-mbstring php-xml
a2enmod proxy_fcgi setenvif
a2enconf php8.3-fpm
systemctl reload apache2(The first 3 lines are for RHEL — the php.conf in conf.d/ connects Apache to PHP-FPM by itself. The last 4 lines are for Ubuntu — write your PHP version in php8.3-fpm.) Test with a <?php phpinfo(); file and delete it right after.
Reverse proxy to an app
<VirtualHost *:80>
ServerName app.example.com
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
RequestHeader set X-Forwarded-Proto "http"
</VirtualHost>setsebool -P httpd_can_network_connect 1
a2enmod proxy proxy_http(setsebool on RHEL; a2enmod on Ubuntu.)
Add HTTPS
dnf -y install certbot python3-certbot-apache
apt-get install -y certbot python3-certbot-apache
certbot --apache -d shop.example.com -d www.shop.example.com
certbot renew --dry-runCertbot adds a :443 VirtualHost with the certificate and a redirect from HTTP. Add Header always set Strict-Transport-Security "max-age=31536000" to the HTTPS VirtualHost and allow only SSLProtocol -all +TLSv1.2 +TLSv1.3.
Daily use: the commands and the real uses
apachectl configtest
systemctl reload httpd
apachectl -S
apachectl -M
tail -f /var/log/httpd/error_log
tail -f /var/log/httpd/shop-access.log
curl -I https://shop.example.comapachectl -S shows all virtual hosts and which one is the default; -M lists the loaded modules.
Real uses at work:
- PHP hosting (WordPress, WooCommerce, Laravel) with PHP-FPM.
- Apps that ship
.htaccessrules; intranet sites with LDAP login (mod_authnz_ldap). - The web server tasks of the RHCSA exam (SELinux contexts and ports).
Make it safe
Security checklist:
ServerTokens Prod,ServerSignature Off,TraceEnable Off, noIndexes.- HTTPS only with HSTS; TLS 1.2+.
AllowOverride Nonewhere possible; deny.git,.env,.ht*.- Use
eventMPM + PHP-FPM, notmod_php. - Keep SELinux on:
restorecon,semanage fcontext -a -t httpd_sys_rw_content_tfor upload folders,setseboolfor proxy and mail. - Remove test files (
phpinfo), keep the server and PHP updated.
Update and remove
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
| 403 Forbidden | Require missing, no index file, file rights or SELinux label. | Require all granted; namei -l FILE; restorecon -Rv /var/www/shop; ausearch -m avc -ts recent. |
AH00558: Could not reliably determine the server's fully qualified domain name | No global ServerName. | Add ServerName host.example.com to the main config. |
| 500 Internal Server Error | A bad .htaccess line or the app failed. | Read the site's error log; check the module of the .htaccess line is loaded. |
| PHP code shows as text or downloads | PHP-FPM is not connected. | Install and start php-fpm; Ubuntu: a2enmod proxy_fcgi + a2enconf php*-fpm. |
| 502/503 on proxy (RHEL) | SELinux blocks the connection or the app is down. | setsebool -P httpd_can_network_connect 1; curl 127.0.0.1:3000. |
(98)Address already in use: AH00072: make_sock: could not bind to address [::]:80 | Nginx or another server uses port 80. | ss -ltnp 'sport = :80'; stop the other one. |
| Wrong site answers | Name does not match, so the first VirtualHost wins. | apachectl -S; fix ServerName / ServerAlias. |
| Custom port (for example 8081) will not start on RHEL | SELinux port label. | semanage port -a -t http_port_t -p tcp 8081. |
Next steps
- Command pages: apachectl, certbot, semanage, restorecon.
- The other web server: Install and set up Nginx. Certificates: Install and set up Certbot.
- The database for PHP apps: Install and set up MySQL or MariaDB.
1A new site gives 403, the files exist and rights are fine (RHEL). What do you check next?
The SELinux label: ls -Z, then restorecon -Rv /var/www/shop.
2Which two commands do you run before every reload?
apachectl configtest, and on bigger changes apachectl -S to see the virtual hosts.