How To Set Up Apache Virtual Hosts on Ubuntu Server

How To Set Up Apache Virtual Hosts on Ubuntu Server

Setting up Apache on Ubuntu server properly is one of the most important skills for anyone managing a Linux based hosting environment. Apache remains one of the most widely used web servers in the world, and its Virtual Host system allows you to host multiple websites on a single Ubuntu server cleanly and efficiently.

In this guide, we will walk through an Apache Virtual Hosts tutorial on an Ubuntu server, with a strong focus on real world usage. This article is written for users who actually want to deploy websites, not just read theory. By the end, you will understand how Apache Virtual Hosts work, how to configure them correctly, and how to troubleshoot common problems before they turn into downtime. This tutorial is especially suitable if you are working with a vps ubuntu hosting setup, where Apache Virtual Host setup is often the default and most reliable choice. Let’s delve in.

What Are Apache Virtual Hosts and Why They Matter

Apache Virtual Hosts allow a single Apache installation to serve multiple domains or websites. Each site can have its own document root, configuration rules, and log files, even though they all run on the same server and IP address.

Without Apache Virtual Host setup, Apache can only serve one website cleanly. With them, you can:

  • Host multiple domains on one server
  • Isolate configurations per project
  • Manage logs and permissions more effectively
  • Scale without adding unnecessary infrastructure

Understanding this concept is critical before moving into the actual apache virtual host setup steps and you need to know your needs.

9 Steps you need to do to Set Up Apache on Ubuntu

Before you begin the process to Set Up Apache on Ubuntu server, a few prerequisites must be in place. Ignoring these basics is one of the most common reasons Apache setups fail later.

You need an Ubuntu server (20.04, 22.04, or newer) with sudo or root access. Most production environments use a vps ubuntu hosting provider, which works perfectly as long as the server has a public IP address.

Make sure port 80 is open on your firewall. If UFW is enabled, Apache traffic must be allowed. You should also either own a domain name or be prepared to test using the local hosts file. Apache Virtual Hosts depend on domain based routing, so DNS awareness is essential.

Once these requirements are met, you can safely move forward.

Step 1: Install Apache on Ubuntu Server

In the first step you need to install packages. So start by updating your already system packages and could be done by this:

sudo apt update
sudo apt upgrade -y

Install Apache:

sudo apt install apache2 -y

After installation, Apache starts automatically. Verify its status:

sudo systemctl status apache2

If Apache is running, open your server’s IP address in a browser. Seeing the default Apache page confirms the installation was successful. And now you can move to the next step.

Step 2: Understand Apache’s Directory Structure

Knowing where Apache stores its files prevents configuration mistakes. You can easily find most important directories in the Ubuntu environment.

Important directories:

/etc/apache2/sites-available/ → Virtual Host configuration files

/etc/apache2/sites-enabled/ → Enabled Virtual Hosts (symbolic links)

/var/www/ → Website files

/var/log/apache2/ → Logs

All apache set up virtual host work starts in sites available, never directly in sites enabled and it’s mostly important for the next steps.

Step 3: Create a Document Root for Your Website

In this step you need to create a document root for your website and we assume your domain is example.com. Create the directory structure:

sudo mkdir -p /var/www/example.com/public_html

Set ownership:

sudo chown -R $USER:$USER /var/www/example.com

Then set permissions:

sudo chmod -R 755 /var/www/example.com

Simply create a test page:

nano /var/www/example.com/public_html/index.html

And you are done for this step. This file will later confirm that your apache virtual host setup works correctly.

Step 4: Create an Apache Virtual Host Configuration File

You must create a configuration file for your virtual host so navigate to the configuration directory with this code:

cd /etc/apache2/sites-available

Create a new file:

sudo nano example.com.conf

Add this apache virtual host example:

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.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>

This configuration tells Apache exactly which files to serve when a request for example.com arrives.

Step 5: Enable the Virtual Host

The next step is for virtual hosts. Enable the virtual host on your site:

sudo a2ensite example.com.conf

Disable the default site to avoid conflicts:

sudo a2dissite 000-default.conf

Test the configuration:

sudo apache2ctl configtest

If the output says Syntax OK, reload Apache:

sudo systemctl reload apache2

At this point, your apache virtual host tutorial setup is active.

Step 6: Configure DNS or Local Hosts File

For live domains, point the domain’s A record to your server IP. For local testing, edit /etc/hosts:

sudo nano /etc/hosts

Add:

SERVER_IP example.com

Now visiting example.com should load your test page.

Step 7: Hosting Multiple Websites with Apache Virtual Hosts

To host another domain, repeat the process:

  • Create a new directory in /var/www
  • Create a new Virtual Host file
  • Enable it with a2ensite
  • Reload Apache

Apache handles multiple Virtual Hosts efficiently, making it ideal for vps ubuntu hosting environments.

Step 8: Enable Common Apache Modules

Most applications require additional modules:

sudo a2enmod rewrite
sudo a2enmod headers
sudo systemctl reload apache2

These are essential for frameworks, CMS platforms, and advanced routing.

Step 9: File Permissions and Security Considerations

Avoid using 777 permissions. A secure and functional setup looks like this:

Directories: 755

Files: 644

Owner: your user

Incorrect permissions are a silent killer in Apache setups.

Troubleshooting Apache Virtual Host Issues

Even with a correct setup, issues can happen. Here are the most common problems and how to fix them. If the default Apache page still shows, your Virtual Host may not be enabled. Run a2ensite again and reload Apache. If you get a 403 Forbidden error, check directory permissions and ownership. If Apache fails to restart, always run:

sudo apache2ctl configtest

For runtime errors, monitor logs:

sudo tail -f /var/log/apache2/error.log

If the domain doesn’t load at all, verify DNS settings and ensure the domain points to the correct server IP. Most Apache issues are configuration related, not software bugs.

When Apache Virtual Hosts Are the Right Choice

When you are considering setting up a host you have a lot of different options. You can choose between several carefully or with just the recommendation of others. Maybe some actual data Apache Virtual Hosts help you to choose better:

  • Hosting multiple domains
  • Running small to medium traffic websites
  • Managing traditional PHP or CMS based projects

They remain a stable and proven solution even in 2026.

Conclusion

Learning how to Set Up Apache on Ubuntu server using Virtual Hosts is a foundational server skill. Once configured correctly, Apache becomes predictable, scalable, and easy to maintain. This guide focused on clarity and real world execution. Follow it carefully, and you’ll have a production ready Apache setup that won’t surprise you later.

FAQ

  1. What is the best way to set up Apache Virtual Hosts on Ubuntu server?

The best approach is to create separate Virtual Host configuration files inside sites available, enable them using a2ensite, and reload Apache. This structure keeps configurations clean and easy to manage on an Ubuntu server.

  1. Can I host multiple domains using Apache on one Ubuntu VPS?

Yes. Apache Virtual Hosts are specifically designed for this purpose and work very well in a vps ubuntu hosting environment, allowing multiple domains to run on a single server without conflicts.

  1. Why does Apache still show the default page after virtual host setup?

This usually happens when the default Virtual Host is still enabled or the new Virtual Host file is not activated. Disabling 000 default.conf and reloading Apache resolves this issue.

  1. What permissions should I use for Apache virtual host directories?

Directories should typically use 755 and files 644, with ownership set to the deploying user. Incorrect permissions are a common cause of 403 Forbidden errors.

  1. Is Apache Virtual Host setup still recommended in 2026?

Yes. Apache remains a stable and reliable web server in 2026, especially for traditional web applications and multi-domain setups on Ubuntu servers.