Skip to content
Follow
Ubuntu Linux 🟡 Intermediate

Configure Apache Virtual Hosts on Ubuntu 26.04

Richard
Written by
Richard
Oct 19, 2026 4 min read
Laptop with a dark Linux terminal on a wooden desk, with a server rack behind it, for an Apache virtual hosts guide.
Laptop with a dark Linux terminal on a wooden desk, with a server rack behind it, for an Apache virtual hosts guide.

To host several websites on one Ubuntu 26.04 server, create one Apache2 configuration file per site in /etc/apache2/sites-available/, then enable each one with a2ensite and reload Apache. This guide assumes Apache2 is already installed and uses two example domains. You can test from a Windows 11 PC by editing its hosts file.

Advertisement
⚡ Quick Answer

To host several websites on one Ubuntu 26.04 server, create one Apache2 configuration file per site in /etc/apache2/sites-available/, set a DocumentRoot folder for each, then run sudo a2ensite for each file and sudo systemctl reload apache2. Check the files first with sudo apache2ctl configtest, which should report Syntax OK.

Advertisement

What you need before you start

A virtual host (a configuration block that tells Apache which site to serve for which domain name) lets one server answer for many names. You need these in place first:

  • An Ubuntu 26.04 server with Apache2 installed and running.
  • A user account that can run sudo.
  • Two domain names. The examples below use example.com and example.org. Replace them with your own.
  • The server’s IP address. The examples use 192.0.2.10.

Create the site folders

Each site gets its own folder, so the content stays separate. These steps need sudo.

  1. Open a terminal on the server, or connect to it over SSH (Secure Shell, a tool for running commands on a server remotely).
  2. Create a folder for each site:
    🐧Bash / Shell
    sudo mkdir -p /var/www/example.com/public_html
    sudo mkdir -p /var/www/example.org/public_html
  3. Make your user the owner of both folders. Owners can edit files in a folder without sudo:
    🐧Bash / Shell
    sudo chown -R $USER:$USER /var/www/example.com /var/www/example.org
  4. Create a test page for the first site:
    ⬛Command Prompt
    echo "This is example.com" > /var/www/example.com/public_html/index.html
  5. Create a test page for the second site:
    ⬛Command Prompt
    echo "This is example.org" > /var/www/example.org/public_html/index.html

Each page shows a different message, so you can tell which site Apache served.

Write the virtual host files

Apache reads one file per site from /etc/apache2/sites-available/. The file only takes effect after you enable it in the next section.

Create the first site

  1. Open a new configuration file:
    🐧Bash / Shell
    sudo nano /etc/apache2/sites-available/example.com.conf
  2. Paste this block:
    💻Code
    <VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
    </VirtualHost>
  3. Press Ctrl+O, then Enter, to save.
  4. Press Ctrl+X to close nano (a basic text editor that runs in the terminal).

ServerName is the main name for the site. ServerAlias adds another name that points to the same site. DocumentRoot is the folder Apache serves.

Advertisement

Create the second site

  1. Open a second file:
    🐧Bash / Shell
    sudo nano /etc/apache2/sites-available/example.org.conf
  2. Paste this block:
    💻Code
    <VirtualHost *:80>
    ServerName www.example.org
    ServerAlias example.org
    DocumentRoot /var/www/example.org/public_html
    ErrorLog ${APACHE_LOG_DIR}/example.org-error.log
    CustomLog ${APACHE_LOG_DIR}/example.org-access.log combined
    </VirtualHost>
  3. Save the file and close nano the same way as before.

Enable the sites and reload Apache

Enabling a site links its file into Apache's active configuration. Run the a2ensite command for each site below. These steps need sudo.

  1. Enable both sites:
    🐧Bash / Shell
    sudo a2ensite example.com.conf
    sudo a2ensite example.org.conf
  2. Check the configuration for mistakes:
    🐧Bash / Shell
    sudo apache2ctl configtest

    You should see Syntax OK. A warning about ServerName for the whole server does not stop Apache from working.
  3. Reload Apache so it picks up the change:
    🐧Bash / Shell
    sudo systemctl reload apache2

If the test reports an error, fix the file it names before you reload.

Open the firewall if needed

📝Good to KnowIf UFW (Ubuntu's firewall tool) is active, allow web traffic.

Skip this if you do not use UFW.

  1. Run:
    🐧Bash / Shell
    sudo ufw allow "Apache"
  2. Confirm the rule with:
    🐧Bash / Shell
    sudo ufw status

Test the sites from 🪟 Windows 11

📝Good to KnowIf your domains do not point at the server yet through DNS (the system that turns domain names into IP addresses), tell your Windows PC where to find them by editing its hosts file.

This file overrides DNS on that PC only.

Advertisement

Add entries to the hosts file

This step needs administrator rights. To undo it later, delete the lines you add.

  1. Click Start, type Notepad, right-click Notepad, and choose Run as administrator.
  2. Choose File Open.
  3. Paste C:\Windows\System32\drivers\etc into the address bar and press Enter.
  4. Change the file type filter from Text Documents (*.txt) to All files.
  5. Select hosts and click Open.
  6. Add these lines at the bottom:
    💻Code
    192.0.2.10 example.com www.example.com
    192.0.2.10 example.org www.example.org
  7. Choose File Save.
  8. Open a Terminal window and run ipconfig /flushdns to clear the saved name lookups that Windows keeps for recent domain names.

Check each site

  1. Open a browser and go to http://www.example.com. You should see This is example.com.
  2. Go to http://www.example.org. You should see This is example.org.
  3. For a quick check without a browser, run curl.exe http://www.example.com in Terminal. The command prints the page text, so you can check the content without opening a browser.

Use http://, not https://. These virtual hosts listen on port 80 only (the standard port for unencrypted web traffic), and your browser may try HTTPS first.

Handle requests for unknown names

Apache serves the first enabled virtual host when a request matches none of your names. On Ubuntu, the default site file 000-default.conf sorts first, so it answers those requests. To stop that, disable it:

  1. Run:
    🐧Bash / Shell
    sudo a2dissite 000-default.conf
  2. Reload Apache:
    🐧Bash / Shell
    sudo systemctl reload apache2

Apache now falls back to the first site in alphabetical order. On this server, that site is example.com.conf. To bring the default site back, run sudo a2ensite 000-default.conf and reload.

What to do next

Once both pages load as expected, remove the lines you added to the Windows hosts file and point your real DNS records at the server. Then add HTTPS (encrypted web traffic) to each virtual host with a certificate, because the setup above sends traffic unencrypted. If a site shows the wrong page, check the ServerName spelling and look at the logs in /var/log/apache2/.

Have a question about this guide?

Answered by AI from this guide. Good questions may be added to this page — please don't include personal details.

Did this guide fix your problem?

Richard

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.

Advertisement

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

Leave a Comment

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