How to Set up an Apache Virtual Hosts on Ubuntu Linux
Apache virtual hosts let you run multiple websites from a single Ubuntu server by pointing visitors to the correct website files based on the domain name they type. This setup means you can host independent sites like example.com and myblog.net on one machine without them mixing up their files.
You need a standard Ubuntu 22.04 LTS server and a basic text editor to set up your first virtual host configuration file. The process involves creating a folder for your website files and pointing Apache to that folder so the right pages load when visitors arrive.
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
Keeping website directory structures tidy on Ubuntu Linux matters. Creating a main folder called the document root for each site holds all website files, giving each project its own isolated space. Organizing up to 10 distinct websites follows this pattern.
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. Creating this file happens next. A basic HTML file for testing takes shape by copying and pasting content into a new file and saving 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.
To activate the new virtual host file, invoke the a2ensite command. This script creates a symbolic link, functioning as a shortcut 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, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.
No comments yet — be the first to share your thoughts!