How to set up vhosts in the Apache2 webserver?

Apache2 is a free and open-source web server that allows users to deploy their websites on the internet. It was first released in 1995 and is maintained by the Apache Software Foundation. Apache is fast, reliable, and secure, and it runs on a large percentage of web servers, making it one of the most popular choices for website owners, developers, and hosting providers.

Join my Discord: https://discord.gg/YbSYGsQYES

Prerequisites

  • A Debian Bookworm (or other Debian based) server
  • Root access
  • Internet connection
  • Website code (or test html)
  • Coffee

Installing the Apache2 webserver

Follow the instructions from my video to install the webserver:

Install Apache2 on Debian

In nutshell:

apt install -y apache2

Creating the directory structure for the sites

We are going to host two sites in our imaginary setup:

  1. prodsite.com
  2. testsite.org

On Debian based systems the web directory is the /var/www.

mkdir -p /var/www/prodsite.com/public_html
mkdir -p /var/www/testsite.org/public_html

Give read access to the web directory.

chmod -R 755 /var/www

Testing with small HTML files

I just create two small “websites” to test the settings.

/var/www/prodsite.com/public_html/index.html

<html>
  <head>
    <title>Welcome to prodsite.com!</title>
  </head>
  <body>
    <h1>This is the prodsite.com.</h1>
    <p>If you see this then the vhost configuration was successful!</p>
  </body>
</html>

/var/www/testsite.org/public_html/index.html

<html>
  <head>
    <title>Welcome to testsite.org!</title>
  </head>
  <body>
    <h1>This is the testsite.org.</h1>
    <p>If you see this then the vhost configuration was successful!</p>
  </body>
</html>

Configuring name-based virtual hosts on our single IP

Name-based web sites in Apache2, also known as name-based virtual hosts, refer to the practice of running multiple websites on a single machine where each website is identified by its domain name.

With name-based virtual hosting, the server relies on the client to report the hostname as part of the HTTP headers, allowing many different hosts to share the same IP address.

Create a virtual host configuration file in /etc/apache2/sites-available/prodsite.com.conf.

<VirtualHost *:80>
    DocumentRoot "/var/www/prodsite.com/public_html/"
    ServerName prodsite.com

    # Other configuration...
</VirtualHost>

Then create a virtual host configuration file in /etc/apache2/sites-available/testsite.org.conf.

<VirtualHost *:80>
    DocumentRoot "/var/www/testsite.org/public_html/"
    ServerName testsite.org

    # Other configuration...
</VirtualHost>

Enable the new vhosts

a2ensite prodsite.com.conf
a2ensite testsite.org.conf
a2dissite 000-default.conf

Validating the configuration and restarting the HTTP server

apache2ctl configtest
systemctl restart apache2

We can reload the config instead of restarting the Apache2, thus the active connections will not break.

systemctl reload apache2

Checking the sites with Curl

If our DNS configuration is right we can navigate to the URLs in a browser or we can use Curl to verify our settings.

curl http://prodsite.com
curl http://testsite.org

Join my Discord: https://discord.gg/YbSYGsQYES

Leave a comment