Oct 17, 2023 5 min read

How To Set Up Apache Virtual Hosts on Ubuntu 20.04

Set Up Apache Virtual Hosts on Ubuntu 20.04 with our step-by-step tutorial. They allow you to host multiple websites on one Apache web server.

Set Up Apache Virtual Hosts on Ubuntu 20.04
Table of Contents

Introduction

Before, we begin talking about the steps to set up Apache Virtual Hosts on Ubuntu 20.04. First, let's understand - What is Apache Virtual Hosts?

Apache Virtual Hosts allow you to host multiple websites on a single Apache web server. It's like having multiple properties with different addresses on one plot of land. Each Virtual Host can have its domain name, content, and settings, making it appear as distinct servers. This efficient and cost-effective solution enables better website management and enhanced user experience.

In this tutorial, you will learn how to set up Apache Virtual Hosts on Ubuntu 20.04.

Advantages of Apache Virtual Hosts

  1. Multi-Website Hosting: Host several websites on one server, each with its domain, content, and settings.
  2. Cost-Efficiency: Save resources by consolidating multiple sites on a single server, reducing hardware and maintenance expenses.
  3. Flexible Configuration: Easily customize settings for each virtual host to meet specific site requirements.
  4. Improved Security: Isolate websites from one another, preventing cross-site vulnerabilities and enhancing overall security.
  5. Enhanced Performance: Optimize server resources, leading to faster loading times and a better user experience.

Prerequisites to Set Up Apache Virtual Hosts on Ubuntu 20.04

Before proceeding with the guide, ensure that you have met the following requirements:

Creating the Directory Structure

The document root is the directory in which a domain name's website files are kept and served in response to requests. In this example, we will use the directory structure shown below, but you can specify the document root to any location you like:

/var/www/
├── domain1.com
│   └── public_html
├── domain2.com
│   └── public_html

Every domain hosted on the server will have /var/www/<domain_name>/public_html as its document root.

As a first step, create the domain's root directory:

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

We will also build an index.html file in the domain's document root directory, which will be displayed when you visit the domain in your browser:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to domain1.com</title>
  </head>
  <body>
    <h1>Success! domain1.com home page!</h1>
  </body>
</html>

Due to the fact that the above commands were executed as the sudo user, root now has ownership of the newly created files and directories. Change the ownership of the domain document root directory and any files inside of it to the apache user (www-data) in order to avoid any permission issues:

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

Creating Virtual Hosts

The Apache Virtual Hosts configuration files for Ubuntu systems can be found in the /etc/apache2/sites-available directory. They can be enabled by creating symbolic links to the /etc/apache2/sites-enabled directory, which Apache reads at startup.

Create the following basic Virtual Host configuration file using your preferred text editor /etc/apache2/sites-available/domain1.com.conf:

<VirtualHost *:80>
    ServerName domain1.com
    ServerAlias www.domain1.com
    ServerAdmin [email protected]
    DocumentRoot /var/www/domain1.com/public_html

    <Directory /var/www/domain1.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/domain1.com-error.log
    CustomLog ${APACHE_LOG_DIR}/domain1.com-access.log combined
</VirtualHost>

  • ServerName: The domain that this virtual host configuration should match. This should be the name of your domain.
  • ServerAlias: All other domains or subdomains, such as the www subdomain, that should correspond with this virtual host.
  • DocumentRoot: It refers to the directory from which Apache will serve the domain files.
  • Options: This directive regulates the features of the server that are available in a specific directory.
  1. -Indexes: Restricts directory listings.
  2. FollowSymLinks: When this option is enabled, Apache will adhere to the symbolic links.
  • AllowOverride: Determines which directives stated in the .htaccess file have the authority to overrule configuration directives.
  • ErrorLog, CustomLog: Indicates where log files should be stored.

The virtual host configuration file can be given whatever name you like, however, it is recommended that you use the domain name.

Using the a2ensite helper script, which creates a symbolic link from the virtual host file to the sites-enabled directory, you can enable the new virtual host file and also disable apache's default page:

sudo a2ensite domain1.com
sudo a2dissite 000-default

A symlink can also be manually created, as demonstrated below:

sudo ln -s /etc/apache2/sites-available/domain1.com.conf /etc/apache2/sites-enabled/

Once complete, check the configuration for syntax errors using:

sudo apachectl configtest

If there are no errors, the output will look like this:

Output

Syntax OK

For the changes to take effect, restart the Apache service:

sudo systemctl restart apache2

Finally, access http://domain1.com in your browser to view the contents of the index.html page and make sure everything is functioning as it should:

FAQs to Set Up Apache Virtual Hosts on Ubuntu 20.04

How can I enable Apache Virtual Hosts on Ubuntu 20.04?

Use sudo a2enmod vhost_alias to enable the required module, then restart Apache with sudo systemctl restart apache2.

How do I create a new Virtual Host on Apache?

Navigate to /etc/apache2/sites-available/ directory, duplicate 000-default.conf, configure the new site settings, and create a symbolic link in /etc/apache2/sites-enabled/.

What's the proper way to disable a Virtual Host?

Run sudo a2dissite your_site.conf to disable the Virtual Host and then sudo systemctl reload apache2 to apply changes.

Can I have multiple domains pointing to one Virtual Host on Ubuntu 20.04?

Yes, you can set up multiple domain names in a single Virtual Host configuration using the ServerAlias directive.

How do I check for syntax errors in my Virtual Host configuration?

Use sudo apache2ctl configtest to verify your Virtual Host configuration for syntax errors before reloading Apache.

Is it possible to have SSL/HTTPS on Apache Virtual Hosts?

Yes, you can enable SSL for your Virtual Hosts using sudo a2enmod ssl, configuring SSL settings, and obtaining SSL certificates.

Can I have both IPv4 and IPv6 Virtual Hosts on Ubuntu 20.04?

Yes, you can set up Virtual Hosts for both IPv4 and IPv6 addresses by defining separate configurations for each IP version.

Conclusion

You now know how to create an Apache virtual host configuration so that one Ubuntu server may host several domains.

Repeat the previous steps for creating additional virtual hosts for all of your domains.

If you have any suggestions or queries, kindly leave them in the comments section.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.