AlmaLinuxRHEL Based

How To Setup Virtual Host Apache on AlmaLinux 9

Setup Virtual Host Apache on AlmaLinux 9

Apache Virtual Hosts are a crucial feature for anyone looking to host multiple websites on a single server. By using virtual hosts, you can efficiently manage various domains and subdomains, all while utilizing the same server resources. This guide provides a comprehensive walkthrough on how to set up Apache Virtual Host on AlmaLinux 9, ensuring you can maximize your server’s potential.

AlmaLinux 9 is a robust and reliable Linux distribution, making it an excellent choice for web servers. Its stability and compatibility with Red Hat Enterprise Linux (RHEL) make it a preferred option for many system administrators. Setting up virtual hosts on AlmaLinux 9 involves several steps, but with this guide, you’ll find the process straightforward and manageable.

Prerequisites

Before diving into the setup process, ensure you have the following prerequisites in place:

  • System Requirements: A server running AlmaLinux 9 with root or sudo privileges.
  • Domain Names: One or more domain names pointed to your server’s IP address. You’ll need to configure DNS records for each domain to point to your server.
  • Basic Linux Knowledge: Familiarity with basic Linux commands and text editors like nano or vim.

Initial Server Setup

System Preparation

First, update your system packages to ensure all software is up to date. This step is crucial for maintaining system security and stability.

sudo dnf update -y

Next, install the necessary packages for web server operations.

sudo dnf install -y httpd

Firewall Configuration for HTTP/HTTPS

AlmaLinux 9 uses Firewalld as its default firewall. Configure the firewall to allow HTTP (port 80) and HTTPS (port 443) traffic.

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

SELinux Considerations

SELinux (Security-Enhanced Linux) is a security feature that provides an additional layer of protection. Ensure that SELinux is configured correctly to avoid permission issues.

 sudo setsebool -P httpd_can_network_connect on

Apache Installation and Basic Configuration

Installation Steps

Install Apache using the DNF package manager. This is the standard method for installing software on AlmaLinux.

sudo dnf install httpd -y

After installation, start and enable the Apache service to ensure it runs automatically on boot.

sudo systemctl start httpd
sudo systemctl enable httpd

Verifying Installation

Verify that Apache is running by checking its status.

sudo systemctl status httpd

You can also access your server’s IP address in a web browser to see the default Apache test page. If you see the Apache test page, the installation was successful.

Basic Security Configurations

It’s a good practice to secure your Apache installation. Disable directory listing to prevent unauthorized access to your server’s files. Edit the Apache configuration file:


 sudo nano /etc/httpd/conf/httpd.conf
 

Find the <Directory "/var/www/html"> section and modify the Options directive:

<Directory "/var/www/html">
  Options -Indexes +FollowSymLinks
  AllowOverride None
  Require all granted
</Directory>

Restart Apache to apply the changes:

sudo systemctl restart httpd

Understanding Virtual Hosts

Core Concepts

Virtual hosts allow you to host multiple domains on a single server. There are two primary types of virtual hosting: name-based and IP-based.

  • Name-Based Virtual Hosting: Uses the domain name to determine which website to serve. This is the most common and efficient method.
  • IP-Based Virtual Hosting: Uses different IP addresses for each website. This method requires multiple IP addresses assigned to your server.

This guide focuses on name-based virtual hosting, as it’s more practical for most scenarios.

Apache’s Virtual Host Processing

When a client makes a request, Apache examines the HTTP Host header to determine which virtual host configuration to use. If the requested domain matches the ServerName or ServerAlias in a virtual host configuration, Apache serves the content from the specified DocumentRoot.

Directory Structure Overview

A well-organized directory structure is essential for managing multiple websites. A common setup involves creating separate directories for each domain within the /var/www/ directory.

Creating the Directory Structure

File Organization

Create a document root directory for each domain you intend to host. For example, if you have two domains, example.com and example.org, create the following directories:

sudo mkdir -p /var/www/example.com/html
sudo mkdir -p /var/www/example.org/html

Also, create log directories for each domain to store Apache logs.

sudo mkdir -p /var/www/example.com/log
sudo mkdir -p /var/www/example.org/log

File Permissions and Ownership

Set the correct file permissions and ownership to ensure Apache can read and write files in these directories. Change the ownership to the Apache user (apache).

sudo chown -R apache:apache /var/www/example.com/html
sudo chown -R apache:apache /var/www/example.com/log
sudo chown -R apache:apache /var/www/example.org/html
sudo chown -R apache:apache /var/www/example.org/log

SELinux Context Settings

Apply the appropriate SELinux context to the directories to allow Apache to serve files from these locations.

sudo chcon -t httpd_sys_content_t /var/www/example.com/html -R
sudo chcon -t httpd_log_t /var/www/example.com/log -R
sudo chcon -t httpd_sys_content_t /var/www/example.org/html -R
sudo chcon -t httpd_log_t /var/www/example.org/log -R

Virtual Host Configuration

Configuration Files

Create virtual host configuration files for each domain in the /etc/httpd/conf.d/ directory. Use a naming convention like example.com.conf.

sudo nano /etc/httpd/conf.d/example.com.conf

Add the following configuration, adjusting the directives as needed:

<VirtualHost *:80>
  ServerName example.com
  ServerAlias www.example.com
  DocumentRoot /var/www/example.com/html
  ErrorLog /var/www/example.com/log/error.log
  CustomLog /var/www/example.com/log/access.log combined
</VirtualHost>

Repeat this process for example.org:

sudo nano /etc/httpd/conf.d/example.org.conf

Add the corresponding configuration:

<VirtualHost *:80>
  ServerName example.org
  ServerAlias www.example.org
  DocumentRoot /var/www/example.org/html
  ErrorLog /var/www/example.org/log/error.log
  CustomLog /var/www/example.org/log/access.log combined
</VirtualHost> 

Basic Virtual Host Directives

  • <VirtualHost *:80>: Defines the virtual host block, listening on port 80 (HTTP). Use <VirtualHost *:443> for HTTPS.
  • ServerName: Specifies the primary domain name for the virtual host.
  • ServerAlias: Specifies any additional domain names or subdomains that should be served by this virtual host.
  • DocumentRoot: Defines the directory from which Apache will serve the website’s files.
  • ErrorLog: Specifies the path to the error log file for this virtual host.
  • CustomLog: Specifies the path to the access log file for this virtual host.

Enable the New Virtual Hosts

Restart or reload Apache to apply the new virtual host configurations.

sudo systemctl restart httpd

Testing and Troubleshooting

Verification Steps

Create simple index.html files in each document root directory to test the virtual host configurations:

echo "<h1>Example.com</h1>" | sudo tee /var/www/example.com/html/index.html
echo "<h1>Example.org</h1>" | sudo tee /var/www/example.org/html/index.html

Access your domains in a web browser (e.g., http://example.com and http://example.org). If the virtual hosts are configured correctly, you should see the corresponding content for each domain.

Common Configuration Errors

  • Incorrect DocumentRoot: Ensure the DocumentRoot directive points to the correct director.
  • Syntax Errors: Check the Apache configuration files for syntax errors using apachectl configtest.
  • Firewall Issues: Verify that your firewall allows traffic on ports 80 and 443.
  • SELinux Issues: Ensure SELinux is not blocking Apache from accessing the files.

Log File Analysis

Check the Apache error logs for any issues. The error logs are located in the directories specified by the ErrorLog directive in your virtual host configurations.

sudo tail -f /var/www/example.com/log/error.log
sudo tail -f /var/www/example.org/log/error.log

Browser Testing Procedures

Clear your browser cache or use a private browsing window to ensure you’re not seeing cached content. Use online tools to check if your DNS records are correctly pointing to your server’s IP address.

Advanced Configuration Options

SSL/TLS Configuration

To secure your websites with HTTPS, you’ll need to configure SSL/TLS. Install the mod_ssl module and obtain SSL certificates from a Certificate Authority (CA) like Let’s Encrypt.


 sudo dnf install mod_ssl -y
 

Create a new virtual host configuration file for HTTPS (port 443) and include the necessary SSL directives:

<VirtualHost *:443>
  ServerName example.com
  ServerAlias www.example.com
  DocumentRoot /var/www/example.com/html
  ErrorLog /var/www/example.com/log/error.log
  CustomLog /var/www/example.com/log/access.log combined

  SSLEngine on
  SSLCertificateFile /etc/pki/tls/certs/example.com.crt
  SSLCertificateKeyFile /etc/pki/tls/private/example.com.key
</VirtualHost>

Custom Error Pages

Create custom error pages to provide a better user experience when errors occur. Create HTML files for different error codes (e.g., 404.html, 500.html) and add the following directives to your virtual host configuration:

ErrorDocument 404 /404.html
ErrorDocument 500 /500.html

Directory-Specific Configurations

Use <Directory> blocks within your virtual host configurations to apply specific settings to certain directories. For example, you can restrict access to certain directories or enable directory listing for specific directories.

<Directory /var/www/example.com/html/admin>
  Require all denied
 </Directory>

Congratulations! You have successfully installed Apache. Thanks for using this tutorial to set up virtual hosts Apache web server on AlmaLinux 9 system. For additional help or useful information, we recommend you check the official Apache website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button