Web Server Virtual Host

Posted on  by admin

Virtual hosting is the practice of serving multiple websites from a single web server. One way to differentiate sites is by using the apparent hostname of the request instead of just the path name part of the URI. Apache virtual host configuration allows you to run multiple websites on the same server, that means you can run more than one website on the same Apache web server. You simply create a new virtual host configuration for each of your websites and restart the Apache configuration to start serving the website.

-->

This article describes how to create, test, and remove a virtual directory on an existing Web site to a folder that resides on a remote computer.

Original product version: Windows Server 2012 R2
Original KB number: 308150

A remote virtual directory is a directory that's not contained within the Web site's home directory but appears to client browsers as though it's within the home directory. A remote virtual directory has an alias that is mapped to a Universal Naming Convention (UNC) share location. A client appends the alias to the URL of the Web site to browse the Web content in that virtual directory. The following table illustrates these mappings:

Physical locationAliasURL path
C:WWWroothome directory
(none)
http://Sales
RemoteServer
SalesDataProdCustomers
Customershttp://Sales/Customers

Both virtual directories and physical directories (directories without an alias) are listed in Internet Services Manager. A virtual directory is indicated by a folder icon that has a globe in the corner.

How to configure a remote network share

To create a virtual directory to a remote network share, create the share, and then store the Web content in that share. Set the appropriate sharing permissions, and then add the appropriate NTFS permissions to control access to the folder that contains your content.

Note

You can also publish Web content to the remote share after the virtual directory is created.

How to create a virtual directory

  1. Log on to the Web server computer using an account that has administrative privileges.

  2. Click Start, point to Programs > Administrative Tools, and then click Internet Services Manager.

  3. In the Internet Information Services window, expand server name (where server name is the name of the server).

  4. Right-click the Web site that you want (for example, Default Web Site), point to New, and then click Virtual Directory.

  5. On the Welcome to the Virtual Directory Creation Wizard page, click Next.

  6. On the Virtual Directory Alias page, type the alias that you want (for example, Sales), and then click Next.

  7. On the Web Site Content Directory page, type the UNC path to the remote folder that you've created (for example, ServerShare), and then click Next.

  8. On the User Name and Password page, type the user name and password that has sufficient privileges to gain access to the remote folder.

    Note

    To maintain the highest levels of security, use an account that has the minimum permissions that are necessary to provide access to the remote content.

  9. Click Next, retype the password that you used in step eight in the Confirm Password dialog box, and then click OK.

  10. On the Access Permissions page, click to select the check boxes of the permissions that you want to set for the virtual directory.

    By default, Read permissions and Run scripts permissions are already selected. For example, if you want to allow users to change the content in the virtual directory, click to select the Write check box.

  11. Click Next, and then click Finish.

    Note

    The virtual directory inherits the configuration and security settings of the Web site in which it is created.

Nginx Web Server Virtual Host

How to test the virtual directory

  1. Start Internet Explorer.

  2. In the Address box, type the URL to your Web server (for example, http://WebServer), and then click Go.

    Verify that you can view the default Web site.

  3. Append the alias of the virtual directory to the address that you typed in step two (for example, http://WebServer/Sales), and then click Go.

    The virtual directory Web content is displayed in the browser window.

How to remove a virtual directory

Server

To delete a virtual directory, remove the alias that Internet Information Services (IIS) uses to reference the content stored in that directory.

Note

When you delete a virtual directory, the network share and its content are not also deleted.

To delete a virtual directory, follow these steps:

  1. Click Start, point to Programs > Administrative Tools, and then click Internet Services Manager.

  2. In the Internet Information Services window, click to expand server name (where server name is the name of the server).

  3. Expand the Web site that contains the virtual directory that you want to delete. For example, expand Default Web Site.

  4. Right-click the virtual directory that you want (for example, Sales) and then click Delete.

  5. Click Yes when the following message is displayed:

    Are you sure you want to delete this item?

    Note

    The Web content remains in the remote folder to which the virtual directory was mapped.

  6. Stop, and then restart the Web site:

    1. Right-click the Web site that you want (for example, Default Web Site), and then click Stop.
    2. Right-click the Web site, and then click Start.
  7. Quit the Internet Information Services snap-in.

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.

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).

Virtual

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.

Web

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

Mac Web Server Virtual 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.

Apache Web Server Virtual Hosting

Tips & Tricks

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

Synology Web Server Virtual Host

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.

Centos 7 Web Server Virtual Host

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