← Back to Apache Course | Chapter 2: Configuration Basics | Lesson 1 of 4

httpd.conf

httpd.conf is Apache's main configuration file -- the place where you tell Apache which port to listen on, where your website's files live, and how it should behave. Almost everything else in this course is really about editing this file (or files it includes).

Where to Find It

The exact path depends on your OS: /etc/httpd/conf/httpd.conf on RHEL/CentOS, /etc/apache2/apache2.conf (plus files it includes) on Ubuntu/Debian, and C:\xampp\apache\conf\httpd.conf on a XAMPP Windows install. On Ubuntu specifically, apache2.conf mostly just Includes other files -- the interesting configuration is split across ports.conf, sites-enabled/, and mods-enabled/.

Core Directives

A handful of directives make up most of the file. ServerRoot sets the base directory Apache treats other relative paths from. Listen sets which port(s) Apache accepts connections on. DocumentRoot sets the directory that files are served from for a given site. ServerAdmin sets a contact email shown on some error pages. ServerName sets the hostname Apache identifies itself as.

Include and IncludeOptional

Rather than one giant file, Apache lets you split configuration across many files with Include path/to/file.conf or Include conf.d/*.conf for a whole directory. IncludeOptional does the same thing but doesn't error if no files match the pattern. This is exactly how distros structure Virtual Hosts and modules into their own separate files instead of one monolithic config.

Testing Config Before Reloading

Apache provides a syntax checker you should run before every reload: apachectl configtest (or apache2ctl configtest on Debian/Ubuntu, or httpd -t directly). It parses every included file and reports "Syntax OK" or the exact line of the problem, without actually restarting the server.

Note: Make apachectl configtest a habit before every systemctl reload apache2 -- catching a typo here takes two seconds; catching it after Apache refuses to restart on a live server takes a lot longer.

Example: Core directives in httpd.conf

apacheconf
ServerRoot "/etc/httpd"
Listen 80

ServerAdmin [email protected]
ServerName example.com

DocumentRoot "/var/www/html"

# Pull in additional config split across other files
IncludeOptional conf.d/*.conf
{# Flagged by hand after confirming a runner can't handle this example (a shell command / go.mod file stored as a TopicExample, a language feature the configured runner version doesn't support, or output that blows a runner's sandbox limit) -- see TopicExample.norun. Never render the run button for these, regardless of language, since it would just fail at execute_code (or worse, hang the Judge0 queue on a submission that can never finish cleanly). #}

⚠️ This example can't run in the browser editor. Try it in your own local environment instead.

{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Editing the wrong file for the distro -- e.g. expecting one single httpd.conf on Ubuntu when the real per-site config lives in sites-available/, or vice versa on RHEL.
  2. Reloading Apache after a config change without running configtest first, so a typo takes the whole server down instead of just failing a syntax check.
  3. Not realizing directives are inherited and can be overridden by more specific contexts later in the file (or in an included file) -- order and scope both matter.
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.