How to Create a Virtual Host in Apache 2.4 on Ubuntu Server: A Comprehensive Guide

Welcome to our step-by-step guide on setting up a Virtual Host in Apache 2.4 on an Ubuntu server. This tutorial is aimed at beginners and intermediates who want to host multiple websites on a single server. Let’s dive right in!

Getting Started: Install Apache

First things first, ensure Apache is installed on your Ubuntu server. If not, you can easily install it using the following commands:

sudo apt update sudo apt install apache2

Enable Necessary Apache Modules

Some Apache modules like mod_rewrite are essential for running websites efficiently. Enable it by executing:

sudo a2enmod rewrite

Structuring Your Directory

Each virtual host should have its own directory. For a website named, create a directory like so:

sudo mkdir -p /var/www/

Assigning Permissions

Setting the right permissions is crucial for security and accessibility:

sudo chown -R $USER:$USER /var/www/ sudo chmod -R 755 /var/www

Create a Test HTML Page (Optional)

A simple HTML page can help you test your setup:

echo "<html><head><title>Welcome to!</title></head><body><h1>Success! The virtual host is working!</h1></body></html>" | sudo tee /var/www/

6. Crafting the Virtual Host File

Apache’s default virtual host file, 000-default.conf, can be duplicated and modified for your new website:

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/

7. Configuring Your Virtual Host

Edit the new virtual host file:

sudo nano /etc/apache2/sites-available/

Customize the <VirtualHost> block as per your domain’s requirements.

8. Activating Your Website

Enable the new site and, if necessary, disable the default one:

sudo a2ensite sudo a2dissite 000-default.conf

9. Configuration Validation

Before restarting Apache, always check for syntax errors:

sudo apache2ctl configtest

10. Restarting Apache

Apply your changes by restarting Apache:

sudo systemctl restart apache2

Local Testing (Optional)

For local testing, update your hosts file to direct the domain name to your server:

sudo nano /etc/hosts

Add a line like:


Congratulations! Your virtual host should now be up and running. Remember, this is a basic setup. Depending on your needs, you might want to explore additional configurations like SSL setup, PHP processing, and more.


Q1: What is a Virtual Host in Apache?

A: A Virtual Host allows Apache to serve different content based on the domain name or IP address used to access the server. This means you can host multiple websites on a single Apache server.

Q2: Do I Need a Unique IP Address for Each Virtual Host?

A: No, you don’t. Apache allows you to host multiple websites on a single IP address using name-based virtual hosts.

Q3: Can I Use This Setup for Both HTTP and HTTPS?

A: Yes, you can configure virtual hosts for both HTTP and HTTPS. However, for HTTPS, you’ll also need to configure SSL certificates.

Q4: How Do I Access My Virtual Host from Another Machine?

A: To access the virtual host from another machine, the domain name of your virtual host must point to your server’s IP address. This is usually configured in your DNS settings.

Q5: Why Can’t I Access My Virtual Host After Configuration?

A: Common reasons include Apache not restarted after configuration, syntax errors in the configuration files, or incorrect permissions. Always run sudo apache2ctl configtest to check for syntax errors.

Q6: How Can I Secure My Virtual Host?

A: You can secure your virtual host by implementing HTTPS with SSL/TLS certificates, ensuring proper file permissions, and using security modules like ModSecurity.

Q7: Is It Possible to Host a WordPress Site in a Virtual Host?

A: Absolutely! You can host a WordPress site by setting up a virtual host and installing WordPress in the designated directory for that virtual host.

Q8: How Do I Monitor the Performance of My Virtual Host?

A: You can monitor performance through Apache access and error logs, and tools like top, htop, or more advanced monitoring solutions like Nagios or Prometheus.

Q9: Can I Host Applications in Different Programming Languages in Separate Virtual Hosts?

A: Yes, each virtual host can be configured to serve applications in different programming languages, though additional configuration may be required for the language-specific runtime environment.

Q10: How Do I Redirect Traffic from HTTP to HTTPS in a Virtual Host?

A: This can be achieved by adding a redirect rule in your virtual host configuration file or using the .htaccess file to enforce HTTPS.

Atiqur Rahman

I am MD. Atiqur Rahman graduated from BUET and is an AWS-certified solutions architect. I have successfully achieved 6 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. I have more than 8 years of working experience as a DevOps engineer designing complex SAAS applications.

Leave a Reply