How to configure Apache Virtual Host.png

How to Configure Apache Virtual Hosts

Apache webserver is one of the dominating web content serving in the overall internet. As we know, web servers are serving web content like HTML, CSS. JavaScript, Images, and more using the Hypertext Transfer Protocol (HTTP). When an end-user requests the content of a web server via HTTP using a domain name, Domain Name Servers (DNS) will resolve the Domain name to the destination IP address of the Web server. In order to properly receive the request of the end-user and provide the content, Web servers need to be configured the Domains called Virtual Hosts. In this article, we will discuss How to configure Apache Virtual Hosts.

Installing the Apache Web Server.

Before getting into the Apache Virtual Hosts, lets see how to install the Apache Web Server into your Machine.

On Ubuntu/Debian:

We are going to install Apache on Ubuntu using apt-get. And just run the following commands.

$ sudo apt-get update
$ sudo apt-get install apache2

On CentOS/RHEL:

In CentOS/RHEL, we are going to use YUM to install Apache Web Servers.

sudo yum install httpd

Remember, in Ubuntu/Debian, Apache web server is called in the name of apache2 and in CentOS/RHEL, it is called as httpd.

Site Contents and Directory Structures.

As part of this tutorial, we are going to see the static web content. As part of this, we will see the Site contents and the Directory structure of the Apache Web server in this section.

Apache webserver will use /var/www as the Document root of the static content. Within this directory, we can create a folder named as the exact site or Domain name. For instance, here we will create demodigitalvarys.com

Create Directories for the site in www as mentioned below.

$ sudo mkdir /var/www/demodigitalvarys.com

then inside the site directory, create home directory called home for the website where the website will land.

$ sudo mkdir /var/www/demodigitalvarys.com/home

Permissions to the Directory Structure.

So, we know that the Apache Web server will use the above directory as the landing page of the request. In order to make the Directories and its contents accessible for the Apache Web Server, we need to give the proper permission to the directory structure. In this section, we will see permissions to provide care and how to give the same.

Ownership of the Directory

As the landing content directory is going to be created by any of the normal user, we need to provide ownership of the above folder to the current user by providing the following command.

$ sudo chown -R $USER:$USER /var/www/demodigitalvarys.com/home

 here, $USER variable is the current username.

Read, Write and Execute Permission.

Ownership of the directory is not enough. We need to provide Permission to the assigned user with proper Read, Write, and Execute permissions. To do the same, execute the following command.

$ sudo chmod -R 755 /var/www

To know more about the chmod command, read here.

Sample Content

We have the Directory structure and the permissions for the same. Now let’s create some sample content that can be served when the end-user requests the webserver. For the same, let’s create an HTML file called index.html and move it to the landing directory.

For the same, Add the following HTML content into the index.html

$ cat <<EOT >> /var/www/demodigitalvarys.com/home/index.html
<!DOCTYPE html>
<html>
<head>
<title>Sample Page</title>
</head>
<body>

<h1>This is a Sample Page for Digital Varys Tutorial</h1>
<p>This is a paragraph for Digital Varys Tutorial</p>

</body>
</html>
EOT

Now, this will create HTML file called index.html with the above content.

Configuring Virtual Host files

Now to the Important part of configuring the virtual host in the Apache Webserver. So far, we have created the Content Directory, Permissions, and Sample page. Now let’s open the configuration file of the Apache webserver and edit it using your favorite editor. Here I am using nano.

So, When you install Apache web server, we will have the default configuration file in the following directory.

$ /etc/apache2/sites-available/000-default.conf

Now, we need to make a copy as we have the separate site for the content.

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/demodigitalvarys.com.conf

Then, we are going to edit it using nano editor.

$ nano /etc/apache2/sites-available/demodigitalvarys.com.conf

The content will look like the below code.

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Note: Here I removed all the commented lines to understand the content clearly.

In this, we will be creating the following directive and we will discuss one by one in detail.

  • ServerAdmin – this directive will have the email ID of the Admin to receive the email notifications. Here we use [email protected]
  • ServerName – This is the directive for the domain name, and it should match the domain we created. Here, demodigitalvarys.com
  • ServerAlias – ServerAlias directive which will have alias address too. Like www.demodigitalvarys.com.
  • DocumentRoot – this directive will point to the landing content directory. Here it is, /var/www/demodigitalvarys.com/home/

So, the over all content after the modification will looks like the below code

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName demodigitalvarys.com
    ServerAlias www.demodigitalvarys.com
    DocumentRoot /var/www/demodigitalvarys.com/home
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the created configurations

So, we have the content directory, Permissions, and configuration files ready. Now we need to let Apache Web serve to know that the new configurations are made. In order to do that, we must enable the configurations by doing the following commands

$ sudo a2ensite demodigitalvarys.com.conf

Remember, we must disable the previous default site too. So, to disable, follow the command.

$ sudo a2dissite 000-default.conf

And finally, we need to restart the Apache service to make the changes effective. To do the same, enter the following service command

$ sudo service apache2 restart

That’s all the configuration of the Virtual Host in the Apache Web Servers. Now, we can test the by entering the address http://demodigitalvarys.com. Then the result page will look like the below screenshot. If you see that, your configurations are working.

configure Apache Virtual Hosts
configure Apache Virtual Hosts

Note: the domain name should be registered in any of the domain name providers. Then in the Management console of the Domain name provider, you have to configure the Public IP or CNAME of the Web server.

Conclusion

In this article, we have discussed I How to configure Apache Virtual Hosts. This article explains about serving the Static Content with the Apache Web Servers. In our upcoming article, we will discuss dynamic content with Application Servers with platforms like Spring, Django, Ruby on rails, and more. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOpsAgileDevSecOps and App Development.

Leave a Reply