Configure Apache Virtual Hosts on Ubuntu 26.04
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.
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.
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.comandexample.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.
- Open a terminal on the server, or connect to it over SSH (Secure Shell, a tool for running commands on a server remotely).
- 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 - Make your user the owner of both folders. Owners can edit files in a folder without
sudo:Bash / Shellsudo chown -R $USER:$USER /var/www/example.com /var/www/example.org
- Create a test page for the first site:Command Prompt
echo "This is example.com" > /var/www/example.com/public_html/index.html
- 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
- Open a new configuration file:Bash / Shell
sudo nano /etc/apache2/sites-available/example.com.conf
- 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> - Press Ctrl+O, then Enter, to save.
- 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.
Create the second site
- Open a second file:Bash / Shell
sudo nano /etc/apache2/sites-available/example.org.conf
- 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> - 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.
- Enable both sites:Bash / Shell
sudo a2ensite example.com.conf
sudo a2ensite example.org.conf - Check the configuration for mistakes:Bash / Shell
sudo apache2ctl configtest
You should seeSyntax OK. A warning aboutServerNamefor the whole server does not stop Apache from working. - 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
Skip this if you do not use UFW.
- Run:Bash / Shell
sudo ufw allow "Apache"
- Confirm the rule with:Bash / Shell
sudo ufw status
Test the sites from 🪟 Windows 11
This file overrides DNS on that PC only.
Add entries to the hosts file
This step needs administrator rights. To undo it later, delete the lines you add.
- Click Start, type
Notepad, right-click Notepad, and choose Run as administrator. - Choose File Open.
- Paste
C:\Windows\System32\drivers\etcinto the address bar and press Enter. - Change the file type filter from Text Documents (*.txt) to All files.
- Select hosts and click Open.
- 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 - Choose File Save.
- Open a Terminal window and run
ipconfig /flushdnsto clear the saved name lookups that Windows keeps for recent domain names.
Check each site
- Open a browser and go to
http://www.example.com. You should see This is example.com. - Go to
http://www.example.org. You should see This is example.org. - For a quick check without a browser, run
curl.exe http://www.example.comin 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:
- Run:Bash / Shell
sudo a2dissite 000-default.conf
- 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?
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!