How to Set up an Apache Virtual Hosts on Ubuntu Linux
Apache Virtual Hosts on Ubuntu Linux lets you run many different websites from one computer.
These are special configuration files that tell your web server, Apache, how to show specific websites when someone types a domain name, like yourwebsite.com. They act like separate instruction sets for each site.
Setting up virtual hosts on Ubuntu 22.04 LTS means you can host sites such as example.com and myblog.net on the same server. This makes managing your server much more efficient.
Each virtual host setup points the server to your website’s files, called the ‘document root,’ and can include other custom settings.
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 your website directory structures tidy on Ubuntu Linux is key. For each site, create a main folder called the document root. This folder will hold all the website’s files, making it much simpler to manage multiple sites by giving each one its own clear space. This helps organize up to 10 distinct websites.
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. Let’s create this file now. A basic HTML file for testing can be made by copying and pasting content into a new file and saving it. Once saved, we’ll configure Apache Virtual Host to point to 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 your 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 our content, run the commands below to create a site-specific Virtual Host configuration file.
The Apache virtual host configuration example works for most systems. You can copy and paste the provided content into a newly created Apache virtual host configuration file. Once you save this Apache virtual host configuration file, the setup for your website on Ubuntu Linux is complete.
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 your new virtual host file, you’ll use the a2ensite command. This handy script creates a symbolic link, essentially a shortcut, from your configuration file in sites-available to the sites-enabled directory.
Run the commands below to enable the configuration file for our domain.
sudo a2ensite example.com.confsudo systemctl restart apache2
Once Apache restarts, you can navigate to your server’s hostname or IP address in a web browser. You should see the content file we created earlier.

You can repeat this process for other domains and Virtual Host files you want to set up. Your server can handle as many virtual host files as it has the resources for.
That should do it!
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!