Article

Table of Contents
Theme:
Was this article helpful?
Try Vultr Today with

$50 Free on Us!

Want to contribute?

You could earn up to $300 by adding new articles!

Host Multiple Websites With Apache Virtual Hosts On FreeBSD 12 Server

Author: Francis Ndungu

Last Updated: Tue, Jun 15, 2021
BSD Web Servers

Introduction

Apache allows you to host multiple websites on a single server using named virtual hosts. Each virtual domain shares your server's public IP address, and the clients' HTTP headers instruct the webserver to serve the right content for each website. This technology is excellent for reducing administration overheads. Provided your server can handle the traffic and has the necessary CPU, memory, and disk, there is no limit to the number of websites you can host. A practical example is using a single server to host a website, employee portal, and an API endpoint for your business on a single server.

This guide will create and host three websites with Apache web server on a FreeBSD 12 server.

Prerequisites

Before you proceed, ensure you have the following:

1. Create Directory Structure for Websites

Begin by setting up a directory structure for your websites. In FreeBSD 12, Apache serves web content from the location below.

/usr/local/www/apache24/data/

Create the following subdirectories in that directory for each website that you intend to host on your server. Replace example.com, example.net, and example.org with the correct domain names.

$ sudo mkdir -p /usr/local/www/apache24/data/example.com/public_html
$ sudo mkdir -p /usr/local/www/apache24/data/example.net/public_html
$ sudo mkdir -p /usr/local/www/apache24/data/example.org/public_html

Then, set the appropriate permissions for each directory so that web visitors can read content and execute the files.

$ sudo chmod -R 755 /usr/local/www/apache24/data/example.com/public_html
$ sudo chmod -R 755 /usr/local/www/apache24/data/example.net/public_html
$ sudo chmod -R 755 /usr/local/www/apache24/data/example.org/public_html

Ensure that all newly created directories inherit the group permissions by executing the commands below.

$ sudo find /usr/local/www/apache24/data/example.com/public_html  -type d -exec chmod g+s {} \;
$ sudo find /usr/local/www/apache24/data/example.net/public_html  -type d -exec chmod g+s {} \;
$ sudo find /usr/local/www/apache24/data/example.org/public_html  -type d -exec chmod g+s {} \;

2. Create Virtual Hosts

In FreeBSD 12, Apache has a default configuration file in the following location.

/usr/local/etc/apache24/httpd.conf

However, the webserver also scans the directory below for additional configuration files.

/usr/local/etc/apache24/Includes

Create separate configuration files for each website and enter the respective content as shown below.

Site 1: example.com

$ sudo nano /usr/local/etc/apache24/Includes/example.com.conf

Enter the information for the example.com website.

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName example.com
    DocumentRoot /usr/local/www/apache24/data/example.com/public_html
    ErrorLog "/var/log/example.com.log"
    CustomLog "/var/log/example.com.log" common
</VirtualHost>

Save and close the file.

Site 2: example.net

$ sudo nano /usr/local/etc/apache24/Includes/example.net.conf

Enter the information below for the example.net website.

<VirtualHost *:80>
    ServerAdmin webmaster@example.net
    ServerName example.net
    DocumentRoot /usr/local/www/apache24/data/example.net/public_html
    ErrorLog "/var/log/example.net.log"
    CustomLog "/var/log/example.net.log" common
</VirtualHost>

Save and close the file.

Site 3: example.org

$ sudo nano /usr/local/etc/apache24/Includes/example.org.conf

Enter the information below for the example.org website.

<VirtualHost *:80>
    ServerAdmin webmaster@example.org
    ServerName example.org
    DocumentRoot /usr/local/www/apache24/data/example.org/public_html
    ErrorLog "/var/log/example.org.log"
    CustomLog "/var/log/example.org.log" common
</VirtualHost>

Save and close the file.

Restart your Apache webserver to load the new configuration files.

$ sudo service apache24 restart

3. Create Content for Each Website

In this step, you'll create content for each website under the directories that you created earlier.

Site 1: example.com

$ sudo nano /usr/local/www/apache24/data/example.com/public_html/index.html

Add the content below into the file.

<html>
  <head>
    <title>The example.com website</title>
  </head>
  <body>
    <h1>Welcome to <b>example.com</b> website.</h1>
    <p>This content confirms that the <b>example.com</b> website is working correctly.</p>
  </body>
</html>

Save and close the file.

Site 2: example.net

$ sudo nano /usr/local/www/apache24/data/example.net/public_html/index.html

Add the content below into the file.

<html>
  <head>
    <title>The example.net website</title>
  </head>
  <body>
    <h1>Welcome to <b>example.net</b> website.</h1>
    <p>This content confirms that the <b>example.net</b> website is working correctly.</p>
  </body>
</html>

Save and close the file.

Site 3: example.org

$ sudo nano /usr/local/www/apache24/data/example.org/public_html/index.html

Add the content below into the file.

<html>
  <head>
    <title>The example.org website</title>
  </head>
  <body>
    <h1>Welcome to <b>example.org</b> website.</h1>
    <p>This content confirms that the <b>example.org</b> website is working correctly.</p>
  </body>
</html>

Save and close the file.

Your webserver is now ready to serve the content from each website.

4. Test the Virtual Domains

Open your web browser and visit your server's URLs to test the new websites. You should now see the web content that you've created for each site.

Conclusion

In this tutorial, you've set up three Apache virtual domains on a FreeBSD 12 server and created site content for them. In the end, you were able to browse content from each website. If you have more websites, you can follow the same procedure to add them to your server.

Want to contribute?

You could earn up to $300 by adding new articles