πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English | πŸ‡ͺπŸ‡Έ EspaΓ±ol | πŸ‡΅πŸ‡Ή PortuguΓͺs | πŸ‡ΉπŸ‡­ ΰΉ„ΰΈ—ΰΈ’ | πŸ‡¨πŸ‡³ δΈ­ζ–‡

Introduction to httpd.conf: Understanding the Apache Configuration File (Ports, Directories, Logs)

In the last article, you took the first step of installing Apache on your computer and getting a web server running. Congratulations! Once your server is running, the next step is to "configure the server to your liking." The file that controls all of these settings is Apache's "brain," the configuration file known as httpd.conf.

In this article, we'll focus on some of the most important settings for controlling Apache and explain each one carefully so that even beginners can understand. Specifically, we'll cover the following three topics:

By editing this file just a little, your web server can become much more convenient and secure. Let's explore the world of Apache's configuration file together!

[EXTREMELY IMPORTANT!]: Before you edit the `httpd.conf` file, always make a backup of the file. If you copy the original file and save it with a name like `httpd.conf.bk`, you can quickly restore it if you make a mistake in the settings. This is a golden rule that even professional engineers always follow.


Where is httpd.conf Located?

First, let's find out where the crucial `httpd.conf` file is located. The location varies depending on how you installed it.

Once you've found the file for your environment, open it with a text editor. Lines starting with a `#` are comments and do not affect the configuration. Try to find the directives (settings) that we'll introduce below within the file.


1. Port Settings - The Listen Directive

A "port" is like a "window number" that the server uses to listen for communications. If an IP address is the address of a building, the port number is like the room number within that building. The standard port for web communication (HTTP) is "port 80."

The directive that specifies this port number is Listen.

# Apache listens for requests on port 80
Listen 80

Normally, this setting is fine as is. However, you might change this number if another application like Skype is already using port 80 and preventing Apache from starting, or if you want to run multiple web servers on a single PC. For example, let's try changing it to port `8080`.

# Change the listening port to 8080
Listen 8080

After changing this setting and restarting Apache, you will need to access your website by specifying the port number, like this: `http://localhost:8080`.


2. Directory Settings - DocumentRoot and Directory

Directory settings are a crucial part of deciding "which files in which location" to publish and "under what rules."

DocumentRoot: The Website's Front Door

DocumentRoot specifies the top-level folder where your website's files (HTML, CSS, images, etc.) are stored. When a request comes from the outside, this is the first place Apache looks. Think of it as the front door to your website.

# Example path for Unix/Linux/macOS
DocumentRoot "/usr/local/apache2/htdocs"

# Example path for Windows
# DocumentRoot "c:/Apache24/htdocs"

<Directory>: Setting the Rules for Each Room

The <Directory> block sets more detailed access rules and behaviors for the folder specified in `DocumentRoot`. For example, the role of this block is to grant permissions like, "Anyone can enter this room, and the resident is allowed to set their own rules inside."

<Directory "/usr/local/apache2/htdocs">
    # Whether to allow settings to be overridden by .htaccess files.
    # Setting this to "All" enables various settings in .htaccess.
    AllowOverride All

    # Access control method for this directory.
    # "Require all granted" allows access from all users.
    Require all granted
</Directory>

AllowOverride All is especially important, as it enables the use of .htaccess files, which can define redirects and access restrictions on a per-directory basis. Don't overthink it at first; just remember, "Once you set the location with `DocumentRoot`, you create a `<Directory>` block for the same path to set the rules."


3. Log Settings - ErrorLog and CustomLog

Logs are your best friend when something goes wrong with the server. They are essential for finding the cause of errors and for analyzing what kind of users are accessing your site.

ErrorLog: The Server's Error Record

As its name suggests, ErrorLog is the file that records errors occurring on the Apache server. Errors like "File not found (404 Not Found)," errors thrown by programs like PHP, or the reasons for a server failing to start are written here. Get into the habit of checking this file first when a problem occurs.

# The path to the file where error logs are saved
ErrorLog "logs/error_log"

CustomLog: The Complete Access Record

CustomLog is the file that records all access to the server, commonly known as the "access log." Information such as when, who (from which IP address), and which file was accessed is recorded line by line.

# The path to the access log file and the log format specification
CustomLog "logs/access_log" common

common is a nickname for a log recording format. With this setting, logs are recorded in a widely used format that includes the IP address, access time, request details, status code, and more.


The Most Important Step After Changing Settings: Syntax Check and Restart

After editing `httpd.conf`, you need to restart the server for the settings to take effect. However, if there's a typo (a syntax error) in the file, Apache won't be able to start. Therefore, you should always perform a "syntax check" before restarting.

Run the following command in your terminal or command prompt.

# For CentOS/RHEL or manual installations
httpd -t

# For Debian/Ubuntu
apache2ctl configtest

If the result is "Syntax OK," it means there are no grammatical errors in your configuration file. Once you get this confirmation, you can restart Apache with confidence.


Conclusion

This time, we learned about three fundamental yet very important elements of the Apache configuration file, `httpd.conf`: ports, directories, and logs.

Now that you can understand and change these settings yourself, you're just one step away from graduating from being a web server beginner! Tinkering with `httpd.conf` is directly linked to a deeper understanding of how websites are published to the world.

So, you've mastered the configuration for publishing a single website. But what if you want to run multiple websites on a single server? The answer to that is our next topic: "Virtual Hosts."

4. Introduction to Apache Virtual Hosts: Easily Manage Multiple Domains