How to Setup Apache2 Virtual Host in Ubuntu 24.04 LTS

On Ubuntu 24.04, Apache2 uses virtual hosts to manage multiple websites on a single server. Each virtual host configuration defines how Apache should handle requests for different domains or subdomains. Understanding how to configure these virtual hosts is crucial for effectively managing multiple sites. Here’s a comprehensive guide to Apache2 virtual host configurations.

Virtual Host Basics

Virtual hosts allow you to run multiple websites on a single Apache server by defining separate configuration files for each site. Apache uses the NameVirtualHost directive (which is now implicit) to route requests based on the domain name or IP address.

My Development Environment

I am a PHP programmer. I believe every PHP Developer should know the fundamentals of Linux and specifically know how to build a virtualization system and should have the skills to build a LAMP stack (Linux, Apache, MySQL, PHP) and have the skills to build and maintain their own development and testing environment.

I am using an old Windows 10 laptop that has an i3 CPU with 2 cores and 4 threads and 16GB of RAM. I’ve installed VirtualBox on it. VirtualBox is a type 2 hypervisor, which means it is installed on top of an operating system.

I use VirtualBox to create virtual machines (guests) for PHP development and testing. These guests consist of Ubuntu 24.04LTS (Linux), Apache, MySQL, and PHP (LAMP stack).

Directory Structure

On Ubuntu, virtual host configurations are usually placed in /etc/apache2/sites-available/, and enabled using symbolic links in /etc/apache2/sites-enabled/.

Configuration File Structure

Each virtual host configuration file typically includes:

  • ServerName: The domain name to which the virtual host responds to.
  • DocumentRoot: The directory where the website’s files are located.
  • ErrorLog: The file where error messages for the virtual host are logged.
  • CustomLog: The file where access logs for the virtual host are recorded.

Example Configuration

Here’s a basic example of a virtual host configuration file located at /etc/apache2/sites-available/example.com.conf:

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>

Configuration Directives Explained

<VirtualHost *:80>: This specifies that the virtual host should respond to requests on port 80 (HTTP). You can also use port 443 for HTTPS.

  • ServerAdmin webmaster@example.com: The email address of the server administrator.
  • ServerName example.com: The primary domain name for this virtual host.
  • ServerAlias www.example.com: Additional domain names or subdomains that should also be handled by this virtual host.
  • DocumentRoot /var/www/example.com/public_html: The directory containing the files for this website.
  • ErrorLog ${APACHE_LOG_DIR}/example.com_error.log: Path to the error log file specific to this virtual host.
  • CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined: Path to the access log file and log format.

Enabling a Virtual Host

After creating or modifying a virtual host configuration file, you need to enable it with the following command:

sudo a2ensite example.com.conf

Then, reload Apache to apply the changes:

sudo systemctl reload apache2

Disabling a Virtual Host

If you need to disable a virtual host, use:

sudo a2dissite example.com.conf

And reload Apache:

sudo systemctl reload apache2

Best Practices

  • Keep Configuration Files Organized: Use clear and descriptive names for your virtual host configuration files.
  • Regularly Check Logs: Monitor error and access logs for troubleshooting.

By following these guidelines and understanding each directive, you can effectively manage multiple websites on your Apache server using virtual hosts.

Conclusion

In this article, we learned how to create and configure a virtual host on a LAMP Ubuntu 24.04LTS server. This consists of creating an Apache2 virtual host configuration file. We also learned what commands are required to complete this task.

How to Setup Apache2 Virtual Host in Ubuntu 24.04 LTS