Skip to content
Follow
Ubuntu Linux

How to Set up an Apache Virtual Hosts on Ubuntu Linux

Richard
Written by
Richard
Dec 6, 2021 Updated Jun 20, 2026 4 min read
Enable Automatic Suspension in Ubuntu Linux Easily
Enable Automatic Suspension in Ubuntu Linux Easily

Setting up Apache Virtual Hosts on Ubuntu Linux allows you to host multiple websites on a single server.

Apache Virtual Hosts are configuration files that tell your web server how to handle requests for different domain names, enabling you to run distinct websites from one IP address.

This is crucial for efficient server management, especially when you need to host sites like example.com and blog.example.com from the same Ubuntu 22.04 LTS machine.

Each virtual host configuration defines specific settings for a website, such as its document root (where its files live) and any custom directives.

⚡ Quick Answer

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

Setting up a good folder structure for your websites on Ubuntu helps you keep everything organised when you host multiple sites on one machine.

Below is an example of a directory structure for multiple websites with unique content and domains.

💻Code
/var/www/
├── example.com
│   └── public_html
├── example.net
│   └── public_html

As you can see above, each domain gets its own folder containing a document root.

Example: /var/www/domain/public_html

Run the commands below to create a directory for the example.com domain, including its document root.

🐧Bash / Shell
sudo mkdir -p /var/www/example.com/public_html

Each document root needs an index.html file so clients see something when they visit. Run the commands below to create an index.html file for the example.com domain.

🐧Bash / Shell
sudo nano /var/www/example.com/public_html/index.html

Then, copy and paste the content below into the file and save it. This is a basic HTML file for testing purposes.

💻Code
<!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>

Once you save the file, we can configure Apache Virtual Host to point to this content. 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):

🐧Bash / Shell
sudo chown -R www-data: /var/www/example.com

How to create an Apache Virtual Host file on Ubuntu Linux

Now that you’ve created the domain content in the directory above, let’s configure the Apache Virtual Host file for it.

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.

🐧Bash / Shell
sudo nano /etc/apache2/sites-available/example.com.conf

An example configuration that should work with most environments is shown below. Copy and paste this content into the file you just created and save it.

💻Code
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin webmaster@example.com
    DocumentRoot /var/www/example.com/public_html

    <Directory /var/www/example.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>

Once the file is saved, you can enable it to become an active virtual host.

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.

🐧Bash / Shell
sudo a2ensite example.com.conf

After that, run the commands below to restart the Apache service.

🐧Bash / Shell
sudo 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.

Terminal window showing Apache virtual host configuration file settings on Ubuntu
apache virtual host file test

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 a2ensite command 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?

Was this helpful?
Richard

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.

📚 Related Tutorials

How to Install Windows Subsystem for Linux (WSL) on Windows 11
Ubuntu Linux How to Install Windows Subsystem for Linux (WSL) on Windows 11
How to Allow Remote Connections to MySQL Database Server
Ubuntu Linux How to Allow Remote Connections to MySQL Database Server
How to Add a User to Sudoers in Ubuntu
Ubuntu Linux How to Add a User to Sudoers in Ubuntu
How to Install KDE Desktop on Ubuntu 24.04
Ubuntu Linux How to Install KDE Desktop on Ubuntu 24.04

No comments yet — be the first to share your thoughts!

Leave a Comment

Your email address will not be published. Required fields are marked *