Web Server Management¶
Prerequisites¶
- Apache HTTPD installed
- Firewall port 80/443 open
Quick Reference¶
| Action | Command |
|---|---|
| List Modules | httpd -M |
| List VHosts | httpd -S |
| Test Config | apachectl configtest |
| Check Logs | tail -f /var/log/httpd/error_log |
| Start | systemctl start httpd |
| Reload | systemctl reload httpd |
| SELinux Bool | setsebool -P httpd_can_network_connect 1 |
Procedure: Create a Virtual Host¶
When to use: Hosting multiple websites on a single server (e.g., site1.com and site2.com).
Steps:
-
Create a config file in
/etc/httpd/conf.d/(e.g.,mysite.conf):<VirtualHost *:80> ServerName mysite.example.com DocumentRoot /var/www/html/mysite ErrorLog /var/log/httpd/mysite-error.log CustomLog /var/log/httpd/mysite-access.log combined </VirtualHost> -
Create the document root:
mkdir -p /var/www/html/mysite echo "<h1>Hello World</h1>" > /var/www/html/mysite/index.html -
Reload Apache:
systemctl reload httpd
Troubleshooting:
- "403 Forbidden": Check directory permissions (
chmod 755) and SELinux context (httpd_sys_content_t).
Procedure: Test Apache Configuration¶
When to use: Before restarting Apache, to ensure syntax errors don't bring down the service.
Steps:
- Run the config test: Expected output:
apachectl configtestSyntax OK
Troubleshooting:
- "Could not reliably determine the server's fully qualified domain name": Add
ServerName localhostto/etc/httpd/conf/httpd.conf(warning only).
Procedure: Enable/Disable Modules¶
When to use: Adding functionality like SSL, proxying, or PHP.
Steps:
-
List loaded modules:
httpd -M -
Enable a module (CentOS/RHEL):
- Most modules are enabled by installing their package (e.g.,
mod_ssl). - Edit files in
/etc/httpd/conf.modules.d/.
- Most modules are enabled by installing their package (e.g.,
-
Enable a module (Debian/Ubuntu style - just for reference):
a2enmod ssl systemctl restart apache2
Troubleshooting:
- "Module not found": Install the RPM package (e.g.,
dnf install mod_ssl).
Procedure: Set Up a Reverse Proxy¶
When to use: Forwarding traffic from Apache to a backend app (like Node.js, Python, or a container) running on a local port (e.g., 8080).
Steps:
-
Edit your VirtualHost config:
<VirtualHost *:80> ServerName app.example.com ProxyPreserveHost On ProxyPass / http://localhost:8080/ ProxyPassReverse / http://localhost:8080/ </VirtualHost> -
Allow Apache to connect to network ports (SELinux):
setsebool -P httpd_can_network_connect 1 -
Reload Apache.
Troubleshooting:
- "503 Service Unavailable": The backend app on port 8080 is not running.
- "Permission denied": SELinux blocked the connection. Run the
setseboolcommand.
Procedure: Configure Forensic Logging¶
When to use: Detailed debugging of HTTP headers for security analysis.
Steps:
-
Load the module (if not loaded):
- On CentOS/RHEL, the module is already installed with
httpdbut commented out. UncommentLoadModule log_forensic_module modules/mod_log_forensic.soin/etc/httpd/conf.modules.d/00-optional.conf. - On other distributions, ensure the
LoadModuleline is present in the Apache configuration.
- On CentOS/RHEL, the module is already installed with
-
Add the directive to your VirtualHost or global config:
<IfModule log_forensic_module> ForensicLog /var/log/httpd/forensic_log </IfModule> -
Reload Apache.
Troubleshooting:
- Logs are empty: Ensure traffic is hitting the server.
- Logs are hard to read: They use
+for start of request and-for end, with full headers.
Procedure: Configure Bearer Token Authorization¶
When to use: Restricting access to a virtual host so that only clients providing a valid Authorization: Bearer <token> header can reach the backend.
Steps:
-
In the virtual host configuration, use
mod_rewriteto check theAuthorizationheader and deny requests that don't carry the correct token:Replace<VirtualHost *:80> ServerName app.example.sysadm.ee RewriteEngine On RewriteCond %{HTTP:Authorization} !^Bearer\ YOUR_TOKEN_HERE$ RewriteRule .* - [F,L] ProxyPreserveHost On ProxyPass / http://localhost:5000/ ProxyPassReverse / http://localhost:5000/ </VirtualHost>YOUR_TOKEN_HEREwith the actual token value. -
Reload Apache:
systemctl reload httpd
How it works:
RewriteCondchecks whether theAuthorizationheader matches the expectedBearer <token>value.- If it does not match,
RewriteRule .* - [F,L]returns HTTP 403 Forbidden and stops processing. - If it matches, the request passes through to the
ProxyPassbackend normally.
Troubleshooting:
- "403 on every request": Double-check the token value and make sure there is a space between
Bearerand the token in theRewriteCondpattern (escaped as\in the regex). - Apache may strip the
Authorizationheader before it reachesmod_rewriteifmod_proxyis involved. If needed, addRequestHeader set Authorization "%{HTTP_AUTHORIZATION}e" env=HTTP_AUTHORIZATIONor useCGIPassAuth On.
Procedure: Configure ModSecurity Rules¶
When to use: Protecting web apps from attacks like SQL injection and XSS.
Steps:
-
Install ModSecurity:
dnf install mod_security -
Enable the rule engine in
/etc/httpd/conf.d/mod_security.conf:SecRuleEngine On -
Add custom rules in
/etc/httpd/modsecurity.d/local_rules/modsecurity_localrules.conf:# Default action: deny and log with HTTP 406 SecDefaultAction "phase:2,deny,log,status:406" # Block requests containing 'etc/passwd' in the URI SecRule REQUEST_URI "etc/passwd" "id:'500001'" # Block directory traversal attempts SecRule REQUEST_URI "\.\./" "id:'500002'" # Block script injection in arguments SecRule ARGS "<[Ss][Cc][Rr][Ii][Pp][Tt]" "id:'500003'" -
Restart Apache.
Troubleshooting:
- Valid requests blocked (False Positives): Check
/var/log/httpd/modsec_audit.logor the Apache error log. You may need to tune rules or setSecRuleEngine DetectionOnlytemporarily. - See Concepts: Web Application Security for background on WAF rules and the OWASP Core Rule Set.
Related Documentation¶
- Technologies: Apache HTTPD
- Concepts: HTTP, Virtual Hosting, Reverse Proxy, Web Application Security