Knowledge Base
MilesWeb / VPS FAQ

How to Configure Apache Virtual Hosts on Ubuntu 20.04?

Approx. read time : 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.

Nehal is an ardent content writer. She's passionate about crafting content that's simple but adds value. Her insatiable interest in writing has allowed her to explore her skills. She is adept and can write for different types of content formats.

Trusted By Thousands of Clients & Big Businesses

We highly appreciate the kind and stellar feedback we receive from our customers. Delivering the best is our goal! MilesWeb is rated Excellent out of 5 based on reviews. Read more reviews.

Excellent
Based on reviews
2 hours ago
Perfect and Valuable Server + ...
I am using MilesWeb Servers, The main thing which I getting are continuous support over everything w...
Gunjan Makwana
4 hours ago
Milesweb is superb Hosting pro...
Milesweb is superb Hosting provider ever, their Support team is amazing!!!...
Abhishek Singh
15 hours ago
Great support in great timing...
We need urgent assistance on changes in a primary domain on our client's Cpanel accounts and reached...
Riyaju Deen
21 hours ago
Best Website Hosting platform ...
I was new on MilesWeb. And needed help on multiple areas from setting up to getting started with cre...
Harshada
1 days ago
Very quick and helpful assista...
Very quick and helpful assistance. Support person listened properly and provided a nice solution....
Narendra
1 days ago
the team is very supportive th...
the team is very supportive though at times effort needs to be made to make understand the problem s...
Suree Sharma
1 days ago
I am using miles web for 3plus...
I am using miles web for 3plus years, very quick and perfect support by the team, they helped me man...
Sri Raghav
2 days ago
The service is good...
The service is good. They are answering with patience and doing the needful as soon as possible....
MR
2 days ago
Perfect and Valuable Server + ...
I am using MilesWeb Servers, The main thing which I getting are continuous support over everything w...
Gunjan Makwana
3 days ago
Very quick and helpful assista...
Very quick and helpful assistance. Support person listened properly and provided a nice solution....
Narendra
4 days ago
positively helped me with find...
positively helped me with finding insecure content on my website causing SSL to not work properly on...
Thaviraj Junglee
4 days ago
Exceptional support, Truly Pra...
I had opted for the basic wordpress hosting plan as I intended to experiment with various plug-ins. ...
Aseem Chandna