How to Set Up an Apache Virtual Hosts on Ubuntu Linux
Apache virtual hosts let you run multiple websites from a single Ubuntu 22.04 LTS server by routing visitors to the correct files based on their domain name. This setup keeps independent sites like example.com and myblog.net separated on one machine so their files never mix.
You need a basic text editor and a standard server to set up your first configuration file. The process involves creating a website folder and pointing Apache to it so the right pages load when people visit.
Create a directory for your website content, like /var/www/example.com/public_html. Then, create a configuration file in /etc/apache2/sites-available/example.com.conf, specifying the ServerName and DocumentRoot. Finally, enable the site with `sudo a2ensite example.com.conf` and restart Apache.
How to create website directory structures on Ubuntu Linux
Website directory structures on Ubuntu Linux need a dedicated document root folder for each site to keep files separate and organized when you set up Apache Virtual Hosts Ubuntu. You create these folders inside the /var/www/ directory so Apache can find and serve the correct files for each unique domain name.
Below is an example of a directory structure for multiple websites with unique content and domains.
Each website domain, like example.com and blog.example.com, gets its own folder, referred to as a document root. This document root holds all the files needed for that specific website. Separating website files into different document roots keeps all website data organized and prevents files from getting mixed up.
Example: /var/www/domain/public_html
A new directory named the document root stores all website files for example.com. The command `mkdir -p /var/www/example.com/public_html` creates both the document root directory and its subfolder, public_html, in one go.
The `index.html` file for the `example.com` domain's document root displays content when users visit the domain. Next, create a basic HTML test file. Copy and paste the following content into the file and save it. Saving finishes the preparation before configuring the Apache Virtual Host to point toward this content.
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Welcome to example.com</title>
</head>
<body>
<h1>Success! example.com home page!</h1>
</body>
</html>To avoid any permission issues, change the ownership of the domain's document root directory and all files within it to the Apache user (www-data):
Apache Virtual Host configuration for the domain content now requires setting up the Virtual Host file. This file tells Apache how to handle requests for a specific website, such as example.com, by linking the domain name to the directory containing its files.
On Ubuntu Linux, Apache Virtual Host configuration files reside in the /etc/apache2/sites-available directory.
To create a virtual host file in the sites-available directory for the content, run the commands below to generate a site-specific Virtual Host configuration file.
The Apache virtual host configuration example functions across most systems. Copying and pasting the provided content into a newly created Apache virtual host configuration file establishes the setup. Saving this Apache virtual host configuration file finalizes the website setup on Ubuntu Linux.
Saving the file makes it active. This action allows the Apache web server to recognize and serve websites from that specific file. For instance, saving the file `/etc/apache2/sites-available/example.com.conf` activates it for the `example.com` website.
Run the following command to activate the new virtual host file. This command creates a symbolic link (a shortcut pointing to another file or directory) from the configuration file in sites-available to the sites-enabled directory.
Run the commands below to enable the configuration file for the domain.
sudo a2ensite example.com.confsudo systemctl restart apache2
Once Apache restarts, navigate to the server hostname or IP address in a web browser. The content file created earlier appears on screen.

Repeating this process applies to other domains and Virtual Host files requiring setup. Hardware resources dictate the total number of virtual host files the server handles.
With these configurations in place, your server now supports multiple independent websites. Proper directory structures and file permissions keep everything running securely.
Conclusion:
- Configuring Apache Virtual Hosts on Ubuntu Linux allows you to efficiently manage multiple websites from a single server.
- Each website operates independently with its own unique settings and configurations.
- Proper directory structures and document roots are essential for serving website content correctly.
- Remember to adjust ownership and permissions to avoid access issues for your web server.
- Use the
a2ensitecommand to easily enable new virtual hosts and manage your configurations. - Restarting Apache is crucial to apply the changes and ensure the new settings are active.
- With these steps, you can expand your server’s capabilities and support additional websites as needed.
Was this guide helpful?
About the Author
Richard
Tech Writer, IT Professional
Richard is a writer at Geek Rewind who turns complex IT tasks into clear, step-by-step guides. He draws on years of hands-on experience in system administration and enterprise IT operations, focusing on Windows, Linux and WordPress: the problems people actually run into, and the fixes that work. His server and WordPress guides come from systems he runs himself. Richard builds and maintains the platform behind Geek Rewind, from its Ubuntu servers and Nginx configuration to its custom WordPress plugins. Many new tutorials start with readers' questions, and he's always glad to answer them in the comments.
No comments yet — be the first to share your thoughts!