Web Server Virtual Host



Hellow there! How’s it going? Today I thought of concentrating my blog on Apache Virtual Host. There you have the URL to the documentation page. It’s for the Apache HTTP Server Version 2.2. Have you got time to read all that? I mean who’s got time to read all those docs? That’s why I’m here for. I’ll make this blog post short and sweet for you(happy-face).

What’s Apache Virtual Host?

Apache Virtual Hosts A.K.A Virtual Host(Vhost) are used to run more than one web site(domain) using a single IP address. In other words you can have multiple web sites(domains) but a single server. Different sites will be shown depending on the user’s requested URL. Best part is you can have any number of virtual hosts in a single server. It simply means you can have any number of web sites(domains) in a single server.

To delete a virtual directory, follow these steps: Click Start, point to Programs Administrative Tools, and then click Internet Services Manager. In the Internet Information Services window, click to expand server name (where server name is the name of the server). Expand the Web site that contains the virtual directory that you want to delete. A Virtual Private Server (VPS) is the next step up from shared web hosting, offering the power and configurability of a dedicated server, but without any hardware issues to worry about. We have powerful physical servers partitioned into isolated virtual servers - each of them looks and behaves like a real networked server system. Virtual host files are the files that specify the actual configuration of our virtual hosts and dictate how the Apache web server will respond to various domain requests. Apache comes with a default virtual host file called 000-default.conf that we can use as a jumping off point. Due to the fact that the virtual host with ServerName www.example.com is first in the configuration file, it has the highest priority and can be seen as the default or primary server. That means that if a request is received that does not match one of the specified ServerName directives, it.

As you see in the above picture, requests from each domain will be mapped into respective document root. If you don’t know what document root means it is where all the files of the website are located(could be public_html). I know you know what document root means, I’m just messing around(tongue-out-face). Let’s get serious again. There are

  1. Name-Based Virtual Host
  2. IP-Based Virtual Host

In most scenarios Name-based virtual host is used. So today we are going to talk about Name-Based Virtual Host and it’s configurations. I don’t want to leave you in the dark. So I’ll go through basics of IP-Based Virtual Host as well.

1. IP-Based Virtual Host

In this scenario the physical server should have two ip addresses, in other words the physical server should have two ethernet cards, each one of them are configured to the particular ip-address of the corresponding website. Don’t get confused. There is only one physical server running Apache but two IPs.

  • www.abc.com has the 192.168.100.1 IP address and a virtual host in Apache which points to www.abc.com’s document root.
  • www.xyz.com has the 192.168.100.2 IP address and a virtual host in Apache which points to www.xyz.com’s document root.

2. Name-Based Virtual Host

Most of the time you will be using name-based virtual host configuration. Let me explain what happens then we’ll move on to how to get to this done. When a request is made to the Apache web server, it looks for the hostname in the HTTP header in the given request. Depending on the hostname, requested will be served. Compared to previous scenario here the server has got only one ip-address but multiple web sites(domains) will be pointing to the server. Here abc.com and xyz.com both sites points to the same Apache web server which has got the ip address of 192.168.100.1. In this scenario we need to have two virtual hosts, one for xyz.com and one for abc.com. Are you worried about how to setup your virtual hosst? That’s what we are going to talk about next.

How To Use Apache Virtual Host?

Here we are going to focus on the name-based virtual hosting. To set up a virtual host you should have the following. What we call “prerequisites”.

  • Apache web server
  • Directory which keeps the website’s files(/var/www/dasunhegoda.com/public_html)
  • Correct permission, 755 for folders and 644 for files

If you have all the above mentioned prerequisites you are good to proceed. Let’s get techie(happy-face).

In the below code snippets dasunhegoda.com should be replaced with your site name.

1. New Virtual Host File

Here we get a copy of the default file.

2. Open The New Virtual Host File & Edit

Above command will open up the newly created file. If you don’t have vim editor, you can use vi editor or nano editor for this. Paste the code below.

Let me explain. Virtual hosts, is defined by <VirtualHost> sections. Keep in mind that having logs in your vhost is not mandatory but makes the life easier to figure out what’s going on.

  • VirtualHost *:80 – Virtual hosts will be listening on the default port 80(could 443 if you are using https)
  • ServerAdmin – Server Admin’s email
  • DocumentRoot – Path where web site files are located
  • ServerName – Server name
  • ServerAlias – Alternate names
  • ErrorLog – File contains any errors that it encounters in processing requests
  • CustomLog – All requests processed by the server. Access log file

Note that you don’t have to have the all the directive in your virtual host. Even having DocumentRoot and ServerName would do the job. See more example here.

3. Activate the host

Activate the virtual host using above command. a2ensite is a built in apache command.

4. Restart Apache

Above command will restart the Apache server. Remember whenever you make configuration changes you should restart Apache server.

*. Disable the virtual host

At any point if you want to disable the virtual host you can use above command to disable. a2dissite is a built in apache command.

Tips & Tricks

Here I’m going to tell you a dirty way to get the job done. But below method is not recommend.

Rather than going through all above steps you can right way edit the apache2.conf file which contains all the Apache server configurations. Just copy and paste the VirtualHost code snippet and restart the server. Disadvantage is you can n’t use a2ensite or a2dissite to enable and disable the sites.

That’s all about Apache virtual hosts. If you have any questions let me know in the comments below. Your feedback is highly appreciated.

Table of Contents

Introduction

Virtual hosting is a method for hosting multiple websites on a single machine. There are two types of virtual hosting: Name-based virtual hosting and IP-based virtual hosting.IP-based virtual hosting is a technique to apply different directives based on the IP address and port a request is received on. You can assign a separate IP for each website on a single server using IP-based virtual hosting. This is mainly used to host different websites on different ports or IP addresses.

In this article we will be creating:

Requirements

  • A server running CentOS v. 7 with Apache installed
  • A desktop machine running Linux
  • A static IP address for each site you want to host.

Set up multiple IP addresses on a single network interface

To set up IP-based virtual hosting, you need to have more than one IP address assigned to your server. Setting up multiple IP addresses on a single network interface is called 'IP aliasing.' It is very useful, particularly if your server only has one network interface card (NIC).

To set up multiple IPs, you need to edit the ifcfg-eth0 file:

Add/edit the following lines:

Web

Save and close the file when you are finished. Then restart the network service to make these changes take effect.

Set up multiple instances of Apache

By default Apache listens for incoming connections on port 80. For port-based virtual hosting, you need to tell Apache to listen for IP address 192.168.1.42 on port 80 and for IP address 192.168.1.43 on port 8080.

To set up multiple ports, you need to edit the httpd.conf file:

Add/edit the following lines:

Host web server at home

Save and close the file, then restart Apache to make these changes take effect.

Create the directory structure

Web Server Virtual Host

First, you need to make a directory structure which will hold the web pages. This directory is known as 'document root' for the domain.

In CentOS 7 the default Apache document root directory is /var/www/html/.

Now, create two directory for websites www.ip-vhost.com and www.port-vhost.com in the default Apache document root directory:

Create test web pages for each virtual host

Now, you need to create an index.html file for each website which will identify that specific domain.

Let's create an index.html file for the www.ip-vhost.com ip virtual host.

Add the following content.

Save and close the file when you are finished.

Similarly, create an index.html file for the www.port-vhost.com virtual host.

Add the following content.

Save and close this file as well. Now, you have the pages to test the virtual host configuration.

Set up ownership and permissions

In CentOS 7 by default the Apache service runs as the user apache. You must change the ownership of these two virtual directories to apache, so that Apache can read and write data.

You can change the ownership with chown command.

You should also make the Apache document root /var/www/html directory world readable, so that everyone can read files from that directory.

Now your web server has the permissions it needs to serve content.

Create virtual host files

The next step is to create a virtual host configuration file for each website. The name of each configuration file must end with .conf.

Apache Web Server Virtual Host

Let's create a virtual host file for website www.ip-vhost.com.

Add the following content.

Web Server Virtual Hosting

Save and close the file when you are finished.

Similarly, create a virtual host file for website www.port-vhost.com.

Mac Web Server Virtual Host

Add the following content.

When you are finished, it is a good idea to check the syntax of the configuration. You can check the syntax of files with the following command:

After the syntax check is done, restart Apache to make these changes take effect.

Allow Apache through the firewall

Now, you need to allow the Apache port 80 and 8080 using FirewallD.

You can do this by running following commands:

Now, reload the firewall service for the changes to take effect.

Test the virtual hosts

Now on the desktop Linux computer, open your web browser and go to the URLs http://192.168.1.42:80 and http://192.168.1.43:8080.You should see sample pages that look like this:

www.ip-vhost.com sample page:

www.port-vhost.com demo page: