How to Create Apache Virtual Hosts in Ubuntu: A Step-by-Step Guide

Post Date: March 07, 2019
Last Updated: October 10, 2026

Introduction

Create Apache Virtual Hosts in Ubuntu to host multiple websites on a single server using the Apache web server. Apache Virtual Hosts allow you to configure separate domain names, document roots, and log files for each website. In this tutorial, we will explain how to create Apache Virtual Hosts in Ubuntu step by step, enable the configuration, verify the syntax, and restart Apache to apply the changes.

Prerequisites

Before creating Apache Virtual Hosts in Ubuntu, ensure you have the following:

  • An Ubuntu server with sudo or root access.
  • Apache2 installed on the server, or permission to install it.
  • A registered domain name, such as example.com.
  • The domain’s DNS A record pointing to your server’s public IP address.
  • Basic knowledge of Linux commands and Apache configuration files.
  • Access to a terminal or SSH client to execute the required commands.

Steps to Create Apache Virtual Hosts in Ubuntu

In this post we will explain how to create the virtual hosts in ubuntu.

Apache is the most important used web server software in the world because it is an open source software. This web server developed and maintained by Apache Software Foundation.

1) Install the apache web server on your ubuntu server

$ sudo apt-get install apache2

2) Make the new directory for the new domain.

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

3) You can use the code for your new domain.

vi /var/www/html/example.com/index.html
<html>
 <head>
 <title>example.com</title>
 </head>
 <body>
 <h1>Hello, This is a test page for example.com website</h1>
 </body>
</html>

3) New configuration file for example.com domain.

sudo vi /etc/apache2/sites-available/example.com.conf

<VirtualHost *:80>
 # The ServerName directive sets the request scheme, hostname and port that
 # the server uses to identify itself. This is used when creating
 # redirection URLs. In the context of virtual hosts, the ServerName
 # specifies what hostname must appear in the request's Host: header to
 # match this virtual host. For the default virtual host (this file) this
 # value is not decisive as it is used as a last resort host regardless.
 # However, you must set it for any further virtual host explicitly.
 #ServerName www.example.com

   ServerAdmin webmaster@example.com
   ServerName example.com
   ServerAlias www.example.com
   DocumentRoot /var/www/html/example.com/public_html

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
 # error, crit, alert, emerg.
 # It is also possible to configure the loglevel for particular
 # modules, e.g.
 #LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
 # enabled or disabled at a global level, it is possible to
 # include a line for only one particular virtual host. For example the
 # following line enables the CGI configuration for this host only
 # after it has been globally disabled with "a2disconf".
 #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

4) Enable virtual host configuration files.

a2ensite example.com.conf

5) Run this command and check the apache files is syntax is correct or not.

apachectl configtest

6) If made any change in the apache related configuration you need to restart apache web server to take a look upon the changes.

systemctl restart apache2

Congratulations! You have completed the virtual host setup and can now be able to access your websites.

Conclusion

Creating Apache Virtual Hosts in Ubuntu allows you to host and manage multiple websites on a single server efficiently. By creating a separate document root and configuration file for each domain, you can organize website files and manage individual site settings. After enabling the virtual host, checking the Apache configuration syntax, and restarting the service, your website will be ready to serve requests once the domain’s DNS is correctly configured. Follow these steps to configure Apache Virtual Hosts in Ubuntu and simplify website management. For more information about Apache Virtual Hosts, refer to the official documentation Link.

For professional assistance with Apache configuration, web hosting, and server management, please reach out to here. Link

FAQs

1. What are Apache Virtual Hosts in Ubuntu?

Apache Virtual Hosts in Ubuntu allow you to host multiple websites on a single Apache web server. Each website can have its own domain name, document root, and configuration settings.

2. How do I create Apache Virtual Hosts in Ubuntu?

To create Apache Virtual Hosts in Ubuntu, install Apache2, create a document root for your website, configure a virtual host file in /etc/apache2/sites-available/, enable the configuration, and restart Apache.

3. Where are Apache Virtual Host configuration files stored in Ubuntu?

Apache Virtual Host configuration files are stored in /etc/apache2/sites-available/. Enabled virtual hosts are linked in the /etc/apache2/sites-enabled/ directory.

How to Modify the Apache VirtualHost Configuration for a Single Domain in cPanel

How to Configure a Custom VirtualHost in cPanel with Apache 2.4

Need Help with Apache Virtual Hosts in Ubuntu?

Need help with Apache Virtual Hosts in Ubuntu, domain configuration, website hosting, or Apache server management? Our experts can help with Apache configuration, Linux server administration, web server optimization, security hardening, and cloud hosting solutions. Get in touch with our team here.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Comment

Scroll to Top