Skip to main content
In this guide, we explore the Apache web server—an open-source HTTP server developed and maintained by the Apache Software Foundation. Apache is predominantly used to serve web content such as HTML, CSS, and JavaScript files. Often, it operates in concert with an application server to process backend business logic. This article, however, focuses exclusively on the Apache web server.
The image features the Apache Web Server logo, highlighting it as an open-source web server.

Installation on CentOS

On CentOS, Apache is available from the default software repository, meaning no additional repository configuration is needed. Simply install Apache using the package manager, start the HTTP service, and verify its status. If your system has a firewall enabled, make sure to add a rule that permits HTTP traffic.
Use the package manager to install, start, and check the Apache service with the commands shown below.
A sample output from the service httpd status command might look like this:

Log Files and Configuration

Apache places its log files within the /var/log/httpd directory. There are two primary logs:
  • Access Logs: Updated whenever a user accesses the website.
  • Error Logs: Updated whenever an error occurs.
The main configuration file is located at /etc/httpd/conf/httpd.conf. This file manages various operational parameters, including the port number, location of static content, SSL/HTTPS settings, and log configurations. A simplified example of the default settings is:
With this configuration, Apache listens on port 80 for all IP addresses on the host. The DocumentRoot specifies the directory containing your static files. To launch your custom website, simply move your static files into this directory. Once you refresh your browser, the default Apache test page will be supplanted by your website’s content.

Server Name Configuration

Setting a server name in the configuration allows Apache to correctly recognize requests for your website. For instance, consider the following configuration for a website about houses:
This configuration lets you access your website via www.houses.com. For this to work seamlessly, the domain name must resolve to your server’s IP address. In a local testing environment, add the following entry to your /etc/hosts file:
Mapping your domain in the /etc/hosts file can help simulate DNS resolution for local server development.

Virtual Hosts

Apache supports hosting multiple websites on a single server using virtual hosts. Each virtual host is defined by its own configuration, including a separate server name and document root. This is useful for hosting multiple domains (e.g., www.houses.com and www.oranges.com) on one machine. Below is an example configuration for hosting two websites:
When a user navigates to either domain, Apache selects the appropriate document root based on the domain in the request. Ensure that the corresponding files (HTML, CSS, JavaScript, images, etc.) for each website are properly placed in their designated directories. Any modifications to the configuration require a restart of the Apache service:

Splitting Virtual Host Configurations

For better organization, you can split virtual host configurations into separate files instead of maintaining everything in the primary configuration file. An example include directive in the main configuration might be:
The contents of /etc/httpd/conf/houses.conf could be:
Similarly, /etc/httpd/conf/oranges.conf might contain:
For effective local testing, add these entries to your /etc/hosts file:
Splitting configuration files can streamline managing multiple virtual hosts, making maintenance and troubleshooting easier.
These configurations ensure that requests to www.houses.com or www.oranges.com are directed to your local Apache server on port 80.

Conclusion

By following the steps outlined in this guide, you can efficiently deploy and manage the Apache web server on CentOS. Experiment with the configurations, set up virtual hosts, and monitor the server logs to deepen your understanding of Apache’s functionality. For additional resources, visit the Apache Documentation. Happy configuring!

Watch Video

Practice Lab