Knowledge Base Hub

Browse through our helpful how-to guides to get the fastest solutions to your technical issues.

Home  >  VPS FAQ  >  How to Configure Apache Virtual Hosts on Ubuntu 20.04?
Top Scroll

How to Configure Apache Virtual Hosts on Ubuntu 20.04?

 3 min

The Apache HTTP web server is one of the popular open-source web servers which offers the power, flexibility and support for developers.

Now, using the virtual hosts, one Apache instance can serve different website. Every domain or an individual website configured using the Apache will direct the visitor to a particular directory holding that website’s information. This is done without suggesting the user that the same server is also responsible for other websites.

This article describes the steps to configure Apache virtual host on Ubuntu 20.04 server.

Prerequisites:

Make sure that you have met the below requirements before continuing the guide.

  • One or more domain names that are pointing to your public server IP.
  • Apache installed on Ubuntu system.
  • You’re logged in as root or user with sudo privileges.

The first step is to create a directory structure that consists of the site data that you will show to the visitors.

Document root, the top-level directory that Apache looks at to to

Steps for Creating Virtual Hosts

On Ubuntu systems, the Apache Virtual Hosts configuration files are all located in the /etc/apache2/sites-available directory. They can be easily enabled by creating the symbolic links to the /etc/apache2/sites-enabled directory, which Apache generally reads during the initial phase.

Now, open the text editor of your choice and create the below Virtual Host configuration file.

/etc/apache2/sites-available/test1.com.conf

<VirtualHost *:80>

    ServerName test1.com

    ServerAlias www.test1.com

    ServerAdmin webmaster@test1.com

    DocumentRoot /var/www/test1.com/public_html

    <Directory /var/www/test1.com/public_html>

        Options -Indexes +FollowSymLinks

        AllowOverride All

    </Directory>

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

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

</VirtualHost>

Note: You can even create a virtual host for subdomain.

Knowing the components of Virtual Host:

ServerName: The domain that needs to match for this virtual host configuration. This also needs to be your domain name.

ServerAlias: All the other domains or subdomains that needs to be matched for this virtual host such as www subdomain.

DocumentRoot: It is basically the directory from which  the Apache will serve the domain files.

Options: This directive particularly controls the server features available in the specific directory.

  • FollowSymLinks: When you enable this option, Apache will follow the symbolic links.
  • Indexes: Prevents the directory listings.

AllowOverride: It specifies which directives declared in the .htaccess file can override the configuration directives.

ErrorLog, CustomLog: Basically, specifies the location for log files.

You can name the configuration files according to your choice, but the best way is to use the domain name of the virtual host configuration file.

After this, you have to enable the new virtual host file. To do so, a2ensite helper script that creates symbolic link from the virtual host file to the sites-enabled directory:

sudo a2ensite test1.com

One more option is, you can manually create a symlink as given below:

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

Once this is done, test the configuration for the syntax errors with:


$ sudo apachetl2 configtest

If no errors are found, you will then get to see the below output.

Output

Syntax OK

Restart the Apache service for the changes to appear:

$ sudo systemctl restart apache2

To verify that everything works as expected, open http://test1.com in your browser and you will get to see the content of the index.html page.

Here is the success message you get:

Success! test1.com home page!

Conclusion

You have a single server handling two different domain names. You can also expand the process by following the steps given in the above tutorial to add the virtual hosts. There is no restriction on the number of domain names Apache can handle, so you can easily make as many virtual hosts as your server is capable to handle.

Repeat the steps to create virtual hosts for all your domains.

For our Knowledge Base visitors only
Get 10% OFF on Hosting
Special Offer!
30
MINS
59
SECS
Claim the discount before it’s too late. Use the coupon code:
STORYSAVER
Note: Copy the coupon code and apply it on checkout.