Introduction
Apache configuration combines global settings, loaded modules, and virtual hosts. Before changing a live service, validate syntax and understand which configuration file owns the active directive.
What you should be able to do after this lesson:
- Locate and test Apache configuration.
- Configure name-based virtual hosts.
- Control access and basic authentication.
- Read access and error logs.
- Configure redirects and dynamic-language integration.
- Inspect process-model and resource settings.
Big Idea: Apache Resolves a Request in Stages
When a request reaches Apache, reason through this order:
listener -> virtual host -> URL-to-resource mapping -> directory/location policy
-> authentication and authorization -> content handler -> response and logs
A 403, 404, or wrong site can originate at different stages. apachectl -S, the effective configuration, and the matching virtual-host logs help identify which stage handled the request.
Configuration Layout
Depending on the distribution, Apache may be named httpd or apache2. Files commonly live under /etc/httpd/ or /etc/apache2/.
apachectl configtest
apachectl -S
apachectl -M
configtest checks syntax, -S explains virtual-host selection, and -M lists loaded modules.
Virtual Hosts
<VirtualHost *:80>
ServerName www.example.test
ServerAlias example.test
DocumentRoot /srv/www/example
ErrorLog logs/example-error.log
CustomLog logs/example-access.log combined
</VirtualHost>
DNS must point clients to the server, but DNS does not choose the virtual host. Apache selects it using the local address, port, and HTTP host name.
Directory Access
<Directory "/srv/www/example">
Options -Indexes
AllowOverride None
Require all granted
</Directory>
Apache 2.4 uses Require directives through authorization modules. Legacy Order, Allow, and Deny behavior may be available through compatibility modules.
.htaccess permits directory-level overrides only when AllowOverride allows them. Central configuration is usually faster and easier to audit.
Basic Authentication
htpasswd -c /etc/apache2/users admin
AuthType Basic
AuthName "Restricted"
AuthUserFile /etc/apache2/users
Require valid-user
AuthGroupFile can define groups for authorization rules. Relevant modules include mod_auth_basic and mod_authz_host; mod_access_compat provides older access-control syntax for migration, not a preferred new policy.
Basic authentication encodes rather than encrypts credentials, so use it over HTTPS.
Logs and Redirects
Access logs record requests; error logs explain server and module failures. Use distinct virtual-host logs when it helps ownership and retention.
Redirect permanent /old https://www.example.test/new
For complex rewriting, understand request matching and test loops carefully.
Dynamic Content and Capacity
PHP may run through a module or through FastCGI/PHP-FPM. mod_perl embeds a Perl interpreter. Each model changes process ownership, memory, and isolation.
Apache Multi-Processing Modules control worker behavior. Settings differ among prefork, worker, and event; inspect the loaded MPM before tuning server and client limits.
Common capacity directives describe maximum requests or worker processes and, for some MPMs, minimum and maximum spare servers or threads. Names and valid combinations depend on the active MPM. Measure concurrency and memory per worker before raising limits, or a traffic spike may exhaust RAM.
Safe Reload
apachectl configtest
systemctl reload apache2
journalctl -u apache2
Use the actual service name for the distribution. Verify locally with the intended Host header before changing DNS or a load balancer.
Guided Practice: Add a Name-Based Virtual Host
On a lab server, create a small document root and virtual host for training.example.test. Keep AllowOverride None initially and grant access explicitly in the central configuration.
Before reload:
apachectl configtest
apachectl -S
apachectl -M
Verify locally without public DNS:
curl -I -H 'Host: training.example.test' http://127.0.0.1/
Add Basic authentication to one location, create a test user with htpasswd, and test both a denied anonymous request and an allowed authenticated request. Find both requests in the access log and the authentication result in the error log.
Troubleshooting Scenario
Apache serves the default site instead of a newly configured host. DNS points to the correct server, but apachectl -S shows that ServerName is misspelled and the file is loaded under a different listener.
Correct virtual-host selection, validate, reload, and retest with an explicit Host header. Changing filesystem permissions would not influence which virtual host Apache selects.
Exam Focus
- Know
httpd.conf, distribution include layouts, access/error logs, and.htaccessbehavior. - Understand name- and address-based virtual-host selection.
- Recognize authentication files, authorization modules, redirects, PHP, and
mod_perlintegration. - Tune limits only for the active MPM and measured workload.
Recap
- Validate syntax and virtual-host mapping before reload.
- Access controls belong in explicit directory or location scopes.
- Basic authentication requires HTTPS for credential protection.
- Tune worker limits for the active MPM and workload.
Test Your Knowledge
Complete the quiz to assess your understanding of this course's concepts.
