Uncategorized

How to Configure Apache Virtual Hosts: 7 Powerful & Easy Steps

0

How to Configure Apache Virtual Hosts is an important skill for beginners who want to host multiple websites on a single Apache web server. Instead of using a separate server for every website, Apache allows you to configure multiple domains on the same server and direct each domain to its own website directory.

For example, imagine that you have two websites:

  • example.com
  • mywebsite.com

Both domains can point to the same server IP address. Apache can then identify the domain requested by the visitor and display the correct website.

This is the main purpose of Apache Virtual Hosts.

If you are new to Linux servers, How to Configure Apache Virtual Hosts may initially look difficult because you need to understand Apache configuration files, DNS records, directories, permissions, ports, and domain names. However, the process is easier when you follow it step by step.

In this tutorial, you will learn How to Configure Apache Virtual Hosts on an Ubuntu or Debian-based Linux server. You will also learn how to configure multiple websites, test your configuration, troubleshoot common errors, and prepare your virtual host for HTTPS.

Apache officially supports both IP-based and name-based virtual hosting. For most modern setups, name-based virtual hosting is the simpler option because multiple domains can share the same IP address.


What Are Apache Virtual Hosts?

Before learning How to Configure Apache Virtual Hosts, you should understand what a virtual host means.

An Apache Virtual Host is a configuration that tells the Apache HTTP Server how to handle requests for a specific website or domain.

A virtual host can define:

  • Domain name
  • Website directory
  • Server aliases
  • Access permissions
  • Error logs
  • Access logs
  • HTTP or HTTPS settings
  • Redirects
  • Application-specific settings

A simple Apache virtual host can look like this:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
</VirtualHost>

In this example, Apache listens on port 80 and serves the website from:

/var/www/example.com

Apache’s official documentation explains that virtual hosting allows more than one website to run on a single machine. Name-based virtual hosting allows multiple hostnames to share the same IP address.


Why Use Apache Virtual Hosts?

Learning How to Configure Apache Virtual Hosts is useful because one server can host many websites.

For example, one server could host:

example.com
blog.example.com
store.example.com
mywebsite.com
portfolio.com

Each website can have its own:

  • Domain
  • Directory
  • Configuration
  • Logs
  • SSL certificate
  • Application
  • Permissions

This is especially useful for:

  • Web developers
  • System administrators
  • DevOps engineers
  • Hosting providers
  • Freelancers
  • WordPress developers
  • Backend developers
  • Students learning Linux
  • Website owners managing multiple projects

Without virtual hosts, managing several websites on one Apache server would be much more difficult.


How Apache Virtual Hosts Work

Understanding the request flow makes How to Configure Apache Virtual Hosts easier.

The process looks like this:

User enters domain
        ↓
DNS resolves domain
        ↓
Domain points to server IP
        ↓
Browser sends HTTP request
        ↓
Apache receives request
        ↓
Apache checks hostname
        ↓
Apache matches ServerName/ServerAlias
        ↓
Apache selects VirtualHost
        ↓
Apache uses DocumentRoot
        ↓
Website files are served

For example, when a visitor enters:

https://example.com

the browser first needs to find the server associated with that domain.

DNS provides the server IP address.

Apache then receives the HTTP request and checks the hostname. If the request matches:

ServerName example.com

Apache selects the corresponding virtual host.

Apache’s name-based virtual-host process first considers the IP address and port, then compares ServerName and ServerAlias among the matching virtual hosts.


How to Configure Apache Virtual Hosts Step by Step

Now let’s learn How to Configure Apache Virtual Hosts using a practical example.

We will use:

Domain: example.com
Website directory: /var/www/example.com
HTTP port: 80

The same process can be adapted for your own domain.


Step 1: Check Whether Apache Is Installed

The first step in How to Configure Apache Virtual Hosts is checking whether Apache is installed.

Run:

apache2 -v

If Apache is installed, you should see information similar to:

Server version: Apache/2.4.x

If Apache is not installed, update the package list:

sudo apt update

Then install Apache:

sudo apt install apache2

After installation, check the Apache service:

sudo systemctl status apache2

If Apache is running, you can continue with the next step.


Step 2: Create a Website Directory

The next step in How to Configure Apache Virtual Hosts is creating a separate directory for your website.

We will create:

/var/www/example.com

Run:

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

This directory will contain the website files.

Now create a simple HTML page:

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

Add:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Example.com</title>
</head>
<body>
    <h1>Welcome to Example.com</h1>
    <p>Apache Virtual Host is working successfully.</p>
</body>
</html>

Save the file.

Your website structure should now look like:

/var/www/example.com/
└── index.html

Step 3: Configure Website Permissions

Permissions are another important part of How to Configure Apache Virtual Hosts.

On Ubuntu and Debian, Apache commonly runs using the www-data user.

You can assign ownership using:

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

Then set appropriate permissions:

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

Check the directory:

ls -la /var/www/example.com

You should see the index.html file.

Why Are Permissions Important?

Apache needs permission to read your website files.

If Apache cannot access the directory, visitors may receive:

403 Forbidden

Do not use permissions such as 777 just to make a website work. Excessive permissions can create unnecessary security risks.


Step 4: Create the Apache Virtual Host Configuration

This is the most important step in How to Configure Apache Virtual Hosts.

On Ubuntu and Debian, Apache site configurations are commonly stored in:

/etc/apache2/sites-available/

Create a new configuration file:

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

Add:

<VirtualHost *:80>

    ServerName example.com
    ServerAlias www.example.com

    DocumentRoot /var/www/example.com

    <Directory /var/www/example.com>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

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

</VirtualHost>

Save the file.

Now let’s understand the configuration.


Understanding the VirtualHost Configuration

<VirtualHost *:80>

<VirtualHost *:80>

This defines a virtual host that handles HTTP traffic on port 80.

The * means Apache can use the virtual host for the relevant addresses on that port.


ServerName

ServerName example.com

ServerName defines the primary domain associated with the virtual host.

For example:

ServerName example.com

Apache’s documentation recommends explicitly specifying ServerName in name-based virtual hosts because it makes hostname matching more predictable.


ServerAlias

ServerAlias www.example.com

ServerAlias allows another hostname to use the same virtual host.

Therefore, both:

example.com

and:

www.example.com

can point to the same website.

You can also define multiple aliases:

ServerAlias www.example.com example.net www.example.net

However, those hostnames must also resolve to the appropriate server through DNS.


DocumentRoot

DocumentRoot /var/www/example.com

DocumentRoot tells Apache where the website files are located.

In our example:

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

is the main page that Apache can serve when a visitor requests the website root.


Directory Configuration

<Directory /var/www/example.com>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

This section controls access to the website directory.

The following directive:

Require all granted

allows Apache to serve content from this directory.

AllowOverride All allows supported .htaccess directives to be used. This is commonly useful for applications such as WordPress, although you should enable only the configuration features you actually need.


ErrorLog

ErrorLog ${APACHE_LOG_DIR}/example.com-error.log

This tells Apache where to store error messages for this website.

These logs are extremely useful when troubleshooting.


CustomLog

CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined

This records website access requests.

Having separate log files for each virtual host makes troubleshooting easier when a server hosts multiple websites.


Step 5: Enable the Virtual Host

After creating the configuration file, enable it.

Run:

sudo a2ensite example.com.conf

The a2ensite command is commonly used on Ubuntu and Debian systems to enable an Apache site configuration.

You can disable the site later with:

sudo a2dissite example.com.conf

At this point, you have completed the main configuration part of How to Configure Apache Virtual Hosts.

However, you should not reload Apache yet without testing the configuration.


Step 6: Test the Apache Configuration

Testing is an important step in How to Configure Apache Virtual Hosts.

Run:

sudo apache2ctl configtest

If everything is correct, you should see:

Syntax OK

If Apache reports an error, do not reload the service until you fix the problem.

You can also use:

sudo apache2ctl -S

This command is especially useful when learning How to Configure Apache Virtual Hosts because it shows how Apache has interpreted the virtual host configuration.

Apache specifically recommends the -S option for debugging virtual host configuration.


Step 7: Reload Apache

Once you receive:

Syntax OK

reload Apache:

sudo systemctl reload apache2

You can check the service:

sudo systemctl status apache2

If there are no errors, Apache should now use your new virtual host.

You have now completed the basic process of How to Configure Apache Virtual Hosts.


Configure DNS for the Apache Virtual Host

Apache configuration alone is not enough.

Your domain must point to the server’s IP address.

For example, your DNS provider may require records similar to:

Type: A
Name: @
Value: YOUR_SERVER_IP

For www, you might use:

Type: A
Name: www
Value: YOUR_SERVER_IP

Alternatively, you can use a CNAME for www depending on your DNS setup.

Important DNS Concept

DNS and Apache perform different jobs.

DNS tells the browser:

“This domain belongs to this server.”

Apache then tells the request:

“This hostname should use this virtual host.”

Apache’s official virtual-host examples explicitly note that creating a virtual host does not automatically create DNS records. The hostname must resolve to the server’s IP address.


Check DNS Resolution

You can check DNS using:

dig example.com

or:

nslookup example.com

The result should point to your server’s public IP address.

If DNS points somewhere else, Apache may be configured correctly while the domain still displays the wrong website.


Test the Apache Virtual Host

After configuring DNS, open:

http://example.com

You should see:

Welcome to Example.com
Apache Virtual Host is working successfully.

You can also test from the terminal:

curl -I http://example.com

A successful response may look like:

HTTP/1.1 200 OK

If you receive a different status code, check your Apache logs and configuration.


How to Configure Apache Virtual Hosts for Multiple Websites

One of the biggest advantages of How to Configure Apache Virtual Hosts is hosting multiple websites on one server.

Suppose you want to host:

example.com
mywebsite.com

Create two directories:

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

Now create separate virtual host configurations.

example.com

<VirtualHost *:80>

    ServerName example.com
    ServerAlias www.example.com

    DocumentRoot /var/www/example.com

    <Directory /var/www/example.com>
        AllowOverride All
        Require all granted
    </Directory>

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

</VirtualHost>

mywebsite.com

<VirtualHost *:80>

    ServerName mywebsite.com
    ServerAlias www.mywebsite.com

    DocumentRoot /var/www/mywebsite.com

    <Directory /var/www/mywebsite.com>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/mywebsite.com-error.log
    CustomLog ${APACHE_LOG_DIR}/mywebsite.com-access.log combined

</VirtualHost>

Enable both:

sudo a2ensite example.com.conf
sudo a2ensite mywebsite.com.conf

Test:

sudo apache2ctl configtest

Then reload:

sudo systemctl reload apache2

Now the same server can serve two different websites.

Apache’s official examples show the same basic approach for running multiple name-based websites on a single IP address.


How to Configure Apache Virtual Hosts for a Subdomain

You can also use How to Configure Apache Virtual Hosts for subdomains.

For example:

blog.example.com

Create a directory:

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

Create the virtual host:

sudo nano /etc/apache2/sites-available/blog.example.com.conf

Add:

<VirtualHost *:80>

    ServerName blog.example.com

    DocumentRoot /var/www/blog.example.com

    <Directory /var/www/blog.example.com>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/blog.example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/blog.example.com-access.log combined

</VirtualHost>

Enable it:

sudo a2ensite blog.example.com.conf

Test:

sudo apache2ctl configtest

Reload:

sudo systemctl reload apache2

You must also create the appropriate DNS record for blog.example.com.


How to Configure Apache Virtual Hosts with HTTPS

For production websites, HTTPS should be used instead of plain HTTP.

The HTTP virtual host normally uses:

Port 80

HTTPS normally uses:

Port 443

A simplified HTTPS virtual host can look like:

<VirtualHost *:443>

    ServerName example.com
    ServerAlias www.example.com

    DocumentRoot /var/www/example.com

    SSLEngine on
    SSLCertificateFile /path/to/certificate.crt
    SSLCertificateKeyFile /path/to/private.key

</VirtualHost>

The exact certificate paths depend on your certificate provider and installation method.

Apache supports name-based HTTPS virtual hosting using SNI, which allows multiple HTTPS virtual hosts to share an IP address when the required software and client support are available.

For beginners, the simplest production approach is usually to first make the HTTP virtual host work and then configure TLS.


Common Apache Virtual Host Errors

Even after following How to Configure Apache Virtual Hosts, you may encounter errors.

Here are the most common problems.

403 Forbidden

A 403 Forbidden error usually means Apache cannot access the requested resource.

Possible causes include:

  • Incorrect permissions
  • Incorrect directory ownership
  • Missing Require all granted
  • Filesystem restrictions
  • Security configuration

Check:

ls -la /var/www/example.com

Also check the Apache error log:

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

404 Not Found

A 404 Not Found error usually means Apache cannot find the requested resource.

Check your DocumentRoot:

DocumentRoot /var/www/example.com

Then verify that the requested file exists:

ls -la /var/www/example.com

If your website uses a framework or rewrite rules, also check whether the required Apache modules and .htaccess configuration are present.


Apache Shows the Wrong Website

This is a common beginner problem when learning How to Configure Apache Virtual Hosts.

Possible causes include:

  • Incorrect ServerName
  • Incorrect ServerAlias
  • DNS pointing to another server
  • Virtual host not enabled
  • Apache configuration not reloaded
  • Multiple virtual hosts using the same hostname
  • Request hostname does not match the configuration

Run:

sudo apache2ctl -S

This helps show which virtual host Apache considers active.

Apache documents apachectl -S as a useful way to inspect how the server parsed virtual host configuration.


Domain Does Not Open

If the website works using the server IP but not using the domain, check DNS.

Run:

dig example.com

If the domain resolves to the wrong IP address, update the DNS record.

Remember:

DNS → Finds the server
Apache → Finds the website

Apache Configuration Syntax Error

If this command:

sudo apache2ctl configtest

returns an error, check the configuration file carefully.

Common mistakes include:

Missing </VirtualHost>
Missing quotation marks
Incorrect directive names
Incorrect directory paths
Typing mistakes

For example, this is incorrect:

<VirtualHost *:80>
    ServerName example.com

because the closing tag is missing.

Correct it:

<VirtualHost *:80>
    ServerName example.com
</VirtualHost>

Useful Apache Commands for Beginners

When learning How to Configure Apache Virtual Hosts, remember these commands.

Check Apache Version

apache2 -v

Check Apache Status

sudo systemctl status apache2

Start Apache

sudo systemctl start apache2

Stop Apache

sudo systemctl stop apache2

Restart Apache

sudo systemctl restart apache2

Reload Apache

sudo systemctl reload apache2

Test Configuration

sudo apache2ctl configtest

Display Virtual Hosts

sudo apache2ctl -S

Enable a Site

sudo a2ensite example.com.conf

Disable a Site

sudo a2dissite example.com.conf

View Apache Error Logs

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

View Access Logs

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

Apache Virtual Hosts Best Practices

Following best practices makes How to Configure Apache Virtual Hosts safer and easier to maintain.

1. Use a Separate Directory for Every Website

Use:

/var/www/example.com
/var/www/mywebsite.com
/var/www/blog.example.com

This keeps websites isolated and organized.

2. Always Define ServerName

Use:

ServerName example.com

Do not rely on Apache to automatically determine the server name.

Apache recommends explicitly listing ServerName in each name-based virtual host.

3. Use Separate Log Files

For every important website, use separate logs:

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

This makes debugging easier.

4. Test Before Reloading

Always run:

sudo apache2ctl configtest

before applying a configuration change.

5. Use HTTPS

Use HTTPS for production websites.

It protects traffic between visitors and the server and is essential for modern websites.

6. Keep Apache Updated

Install operating system and Apache security updates regularly.

7. Avoid 777 Permissions

Do not solve permission problems by using:

chmod -R 777 /var/www/example.com

Instead, identify the actual permission problem and apply the minimum required permissions.

8. Back Up Configuration Files

Before making major changes, create a backup:

sudo cp /etc/apache2/sites-available/example.com.conf \
/etc/apache2/sites-available/example.com.conf.backup

Apache Virtual Host Configuration Example

Here is a complete beginner-friendly example that you can use as a starting point:

<VirtualHost *:80>

    ServerName example.com
    ServerAlias www.example.com

    DocumentRoot /var/www/example.com

    <Directory /var/www/example.com>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

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

</VirtualHost>

After saving the file:

sudo a2ensite example.com.conf

Test it:

sudo apache2ctl configtest

If you see:

Syntax OK

reload Apache:

sudo systemctl reload apache2

Then make sure your DNS points to the correct server.

This is the basic workflow for How to Configure Apache Virtual Hosts.


How to Check Which Virtual Host Apache Is Using

If you host multiple websites, you may want to know how Apache is interpreting your configuration.

Run:

sudo apache2ctl -S

The output can show:

  • Virtual host addresses
  • Ports
  • Server names
  • Configuration files
  • Default virtual hosts

This is one of the most useful troubleshooting commands when working with Apache Virtual Hosts.

Apache’s official documentation specifically recommends the -S option for examining how Apache parsed the configuration.


Difference Between Name-Based and IP-Based Virtual Hosts

There are two main types of Apache Virtual Hosts.

Name-Based Virtual Hosting

Name-based virtual hosting allows several domains to share one IP address.

For example:

example.com       → 203.0.113.10
mywebsite.com     → 203.0.113.10
blog.example.com  → 203.0.113.10

Apache uses the requested hostname to select the correct virtual host.

This is the most common approach for modern websites. Apache’s documentation states that name-based hosting is generally simpler and should normally be preferred unless a specific requirement calls for IP-based hosting.

IP-Based Virtual Hosting

With IP-based virtual hosting, different websites can use different IP addresses.

For example:

example.com    → 203.0.113.10
mywebsite.com  → 203.0.113.20

IP-based virtual hosting can be useful when applications or infrastructure require separate IP addresses.

However, for most beginner and general website deployments, name-based virtual hosting is easier.


How to Configure Apache Virtual Hosts Locally Without a Public Domain

You can also learn How to Configure Apache Virtual Hosts without buying a domain.

For local development, you can edit the hosts file.

On Linux:

/etc/hosts

Add:

127.0.0.1 example.local

Then configure Apache:

<VirtualHost *:80>

    ServerName example.local

    DocumentRoot /var/www/example.local

    <Directory /var/www/example.local>
        Require all granted
    </Directory>

</VirtualHost>

Enable the configuration and reload Apache.

Now you can visit:

http://example.local

This approach is useful for testing websites before deploying them to a production server.

Apache’s official virtual-host examples also mention using the hosts file for local testing, while noting that it only affects the machine where the entry exists.


Internal Resources

If you are learning server administration and web development, you can continue with related tutorials on CodexJunction.

For example, explore the CodexJunction Tutorials section for additional development guides. The site currently includes tutorials covering web development, Python, AI/LLM development, PHP, WordPress, and other programming topics.

You can also connect this tutorial with related CodexJunction content such as:

  • How to Build a Simple RAG Application With Python
  • How to Build a Document Q&A App With Python
  • How to Create a Semantic Search Feature With Python
  • How to Build a Website Q&A Bot With RAG

These topics are currently listed on CodexJunction and can be useful follow-up resources for developers working with modern web and AI applications.

WordPress internal-link placement suggestion: Add internal links to the actual published URLs of those articles after confirming their URLs in your WordPress dashboard. This prevents broken internal links if your site’s permalink structure differs.


External Resources

For authoritative information about Apache Virtual Hosts, beginners should use the official Apache documentation.

Apache Virtual Host Documentation

The official Apache Virtual Host documentation explains name-based hosting, IP-based hosting, virtual host configuration, and virtual host matching.

Apache Name-Based Virtual Hosts

Apache’s name-based virtual host documentation explains how Apache selects a virtual host using the request hostname and why ServerName and ServerAlias are important.

Apache Virtual Host Examples

The official examples cover multiple websites on one IP address, multiple IP addresses, different ports, and other common virtual-host configurations.

Apache SSL/TLS Documentation

Apache’s SSL/TLS documentation explains HTTPS virtual hosting and SNI support.

When adding these resources to WordPress, use normal external links without adding rel="nofollow" if you want them to remain DoFollow links.


Frequently Asked Questions

What is an Apache Virtual Host?

An Apache Virtual Host is a configuration that allows Apache to serve one or more websites from a server. Each virtual host can have its own domain name, website directory, logs, and other settings.

Why do I need Apache Virtual Hosts?

Apache Virtual Hosts allow multiple websites to run on the same server. This can reduce infrastructure costs and make server management easier.

Can multiple domains share one IP address?

Yes. Name-based virtual hosting allows multiple domains to share the same IP address. Apache uses the hostname from the request to select the appropriate virtual host.

What is ServerName in Apache?

ServerName specifies the primary hostname associated with a virtual host.

Example:

ServerName example.com

What is ServerAlias?

ServerAlias specifies additional hostnames that should use the same virtual host.

Example:

ServerAlias www.example.com

What is DocumentRoot?

DocumentRoot tells Apache where the website’s files are stored.

Example:

DocumentRoot /var/www/example.com

What command tests Apache configuration?

Use:

sudo apache2ctl configtest

If the configuration is valid, Apache normally reports:

Syntax OK

How can I see all Apache Virtual Hosts?

Use:

sudo apache2ctl -S

This helps identify virtual host configuration and matching problems.

Can I host WordPress with Apache Virtual Hosts?

Yes. WordPress can be installed inside a virtual host’s DocumentRoot. Depending on your WordPress configuration, you may also need PHP, a database server, Apache rewrite support, appropriate permissions, and HTTPS.

Can I use HTTPS with Apache Virtual Hosts?

Yes. Apache supports HTTPS virtual hosts on port 443. Modern browsers and Apache installations can use SNI to support multiple HTTPS websites on the same IP address.

Do Apache Virtual Hosts automatically create DNS records?

No. Apache Virtual Hosts and DNS are separate systems. You must configure DNS so that your domain resolves to the appropriate server IP address.


Apache Virtual Hosts Troubleshooting Checklist

If your website is not working after following How to Configure Apache Virtual Hosts, check these items in order:

  1. Confirm Apache is installed.
  2. Confirm Apache is running.
  3. Check your domain’s DNS record.
  4. Confirm the domain resolves to the correct server IP.
  5. Check the DocumentRoot.
  6. Check website file permissions.
  7. Confirm the virtual host file exists.
  8. Confirm the virtual host is enabled.
  9. Run apache2ctl configtest.
  10. Run apache2ctl -S.
  11. Reload Apache.
  12. Check Apache error logs.
  13. Check your browser’s response.
  14. Verify HTTPS configuration if using SSL.

This simple checklist can solve many common Apache configuration problems.


Summary of How to Configure Apache Virtual Hosts

Let’s summarize How to Configure Apache Virtual Hosts.

The overall process is:

1. Install Apache
       ↓
2. Create website directory
       ↓
3. Add website files
       ↓
4. Set permissions
       ↓
5. Create VirtualHost configuration
       ↓
6. Set ServerName
       ↓
7. Set ServerAlias
       ↓
8. Set DocumentRoot
       ↓
9. Enable the website
       ↓
10. Test Apache configuration
       ↓
11. Configure DNS
       ↓
12. Reload Apache
       ↓
13. Test the website
       ↓
14. Configure HTTPS

The most important Apache directives to remember are:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
</VirtualHost>

Once you understand these directives, How to Configure Apache Virtual Hosts becomes much easier.


Conclusion

How to Configure Apache Virtual Hosts is an essential skill for anyone who wants to manage websites on an Apache server.

The basic idea is simple: DNS sends a domain to your server, and Apache uses the requested hostname to select the appropriate virtual host. Each virtual host can then point to a different website directory.

In this tutorial, you learned How to Configure Apache Virtual Hosts step by step, including how to:

  • Install Apache
  • Create a website directory
  • Add website files
  • Configure permissions
  • Create a virtual host
  • Configure ServerName
  • Configure ServerAlias
  • Set DocumentRoot
  • Enable a website
  • Test Apache configuration
  • Configure DNS
  • Host multiple websites
  • Configure subdomains
  • Prepare HTTPS
  • Troubleshoot common errors
  • Inspect Apache virtual hosts
  • Apply Apache security and maintenance best practices

The most important commands to remember are:

sudo apache2ctl configtest
sudo apache2ctl -S
sudo systemctl reload apache2

The first command checks your configuration. The second helps you understand how Apache is interpreting your virtual hosts. The third applies configuration changes without requiring a full service restart.

If you are a beginner, practice How to Configure Apache Virtual Hosts first with a simple HTML website. Once that works, you can move on to WordPress, PHP applications, Laravel projects, reverse proxies, and HTTPS.

With regular practice, How to Configure Apache Virtual Hosts becomes a straightforward and valuable Linux server administration skill.


 

How to Configure Nginx for a Website: Complete Beginner’s Guide 2026

Previous article

How to Migrate a WordPress Website Manually: 9 Easy Steps

Next article

Comments

Leave a reply

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