How to Configure Apache Virtual Hosts on Ubuntu 20.04?
One of the well-known open-source web servers that provides developers with strength, flexibility, and support is the Apache HTTP web server.
Currently, one Apache instance may serve several websites by utilizing virtual hosts. Any domain or specific website set up with Apache will point the visitor to a specific directory containing the contents of that website. This is accomplished without implying to the user that other websites are under the control of the same server.
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
One server is in charge of two distinct domain names. Additionally, you can add more hosts to the process by following the instructions in the tutorial mentioned above. You can easily create as many virtual hosts as your server can support, as Apache has no limit on the amount of domain names it can manage.
Repeat the steps to create virtual hosts for all your domains.