Virtual Hosting: Name based

Revision as of 08:56, 2 March 2010 by Drtanz (talk | contribs) (→‎Hosts File)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Virtual Hosting
Uniform Server 3.5-Apollo

Hosting independent web sites is relatively easy using the Apache directive VirtualHost. Uniform Server uses name-based virtual hosting and automates the set-up process making it easy and transparent. I cover the automatic process first followed by a manual process. The manual process is important in understanding a virtual host block and allows you to tweak and refine.

Practical examples

When experimenting use real domain names see below these allow you to put your sites on-line and perform real tests. You can use fictitious domain names this restricts you to testing locally.

If using real domain names for existing sites you can test these sites locally before publishing.

Domain names

During DynDns set-up, I mentioned wildcards and their importance when virtual hosting. A quick recap wildcards allow you to append any characters before the base domain name. For example the base domain name fredtest.mine.nu wildcards allow you to type the familiar www.fredtest.mine.nu into a browser to access your site. You can type anything in place of www. These examples all access the same site:

  • www.fredtest.mine.nu
  • www.my_site1.fredtest.mine.nu
  • www.my_site2.fredtest.mine.nu
  • home-fredtest.mine.nu

If each of these names represents a separate independent site, the Apache Virtual Host directive can separate and point them to the appropriate site root folder on your server.(See previous page for root folder details.)

If you own more than one domain name each reflecting a different web site, you can host them on your personal web server using Apache Virtual Host.

Apache's Virtual Host is extremely flexible need to test your sites locally just add the appropriate host blocks. Uniform Server 3.5 makes adding virtual hosts a snip. Before looking at this you require two pieces of information for each site, domain name and location of the site root folder.

Sites to be hosted

As a starting point, lets assume I have two sites hosted at DynDNS and one personal domain hosted with another provider. I wish to host the three sites on my server and test them locally. The sites are contained in three separate folders named site1, site2 and site3 refer to configuration3 on the previous page for details.

Domain Root Folder name Root Folder path
  • www.my_site1.fredtest.mine.nu
  • www.my_site2.fredtest.mine.nu
  • www.ric.com
  • site1
  • site2
  • site3
  • I:/site1
  • /z_www/site2
  • /z_www/site3

Note: Left and right columns are all the information you require to set-up virtual hosts however before doing that first create the site roots folders.

To each of these add a simple index page for example:

<h1>Test Site 1</h1>
<?
phpinfo();
?>

Change "Test Site 1" to match the site it corresponds to. If you have an existing site pop it into a root folder no need to use a test index page.

Top

How to set-up a Vhost using apanel

Setting up a Vhost using apanel is straight forward, start the servers if not already running, display apanel (if not displayed type http://localhost/apanel/ into your browser address bar) and follow these steps:

 1  Scan down apanel's left menu and click on Admin Virtual Host This displays the Virtual host page.
Note: You can safely ignore the error message (Error in hosts file: localhost:80 does not exist)
 2 Enter the website address (what you would type into a browser excluding the http:// bit)
(www.my_site1.fredtest.mine.nu)
 3 Enter path to the corresponding root folder path.
(I:/site1)
 4 Click Create Host you will be taken back to apanel's home page
 

Note 1: Repeat steps 1) to 4) for each host you want to add.

Note 2: With all the hosts entered restart the servers for the changes to take place.

Note 3: Each host entered will be added to the list host links displayed at the top of the Host Page. After restarting the servers these links become active you can use them to test your sites.

 

Top

Testing

Testing is just a matter of typing a site's address into a browser.

Type the following into browser address bar: Result
  • www.my_site1.fredtest.mine.nu
  • www.my_site2.fredtest.mine.nu
  • www.ric.com
  • Displays heading "Test Site 1" and a list of PHP information
  • Displays heading "Test Site 2" and a list of PHP information
  • Displays heading "Test Site 3" and a list of PHP information

If you are not using the test index.php page the results will be dependent on what you placed in the root folders.

Note 1: If your servers are on-line you cannot use a local browser to check that your sites are accessible from the internet. Your site domain names are resolved locally via the hosts file. You have three option to test remote accessibility:

  • Go to Google translate enter your site address bottom of page and click translate. If the page is translated its accessible.
  • Get a friend to check or use a different machine.
  • Disable the enteries in the Host file - covered next

The remainder of this page looks at manually editing files and adds a little more background detail.

Top

Hosts File

The above process adds entries into your Host File allowing your site domains to be locally resolved. Advantage of this you can use fictitious domain names while experimenting or before you register a real domain name. You will notice on the Vhost page there is no way to delete a Vhost hence at sometime you will need to edit your Hosts File.

The hosts file is located in the system folder. The path may be different depending on your setup, these paths are for typical default installations.

c:\windows\system32\drivers\etc\ — Windows 7
c:\windows\~system32\drivers\etc — Windows Vista
c:\windows\hosts — Windows 95/98/me
c:\winnt\system32\drivers\etc\hosts — Windows NT/2000/XP Pro
c:\windows\system32\drivers\etc\hosts — Windows XP Home

Open the file hosts in a text editor it will look similar to this:

# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1       localhost

127.0.0.1    www.my_site1.fredtest.mine.nu
127.0.0.1    www.my_site2.fredtest.mine.nu
127.0.0.1    www.ric.com

You can either delete entries or comment them out using #.

While you have this file open now would be a good time to resolve the error message (localhost:80 does not exist) mentioned above. Add the following line: 127.0.0.1 localhost:80 I have also commented out the sites so I can test remote access using my local browser, this file now looks like this:

# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1       localhost
127.0.0.1       localhost:80

#127.0.0.1    www.my_site1.fredtest.mine.nu
#127.0.0.1    www.my_site2.fredtest.mine.nu
#127.0.0.1    www.ric.com

Top

Apache configuration file httpd.conf

All virtual hosts are added to the end of Apache's configuration file httpd.conf open this file you will find it in folder *\Uniform Server\udrive\usr\local\apache2\conf scroll to the end and it will look similar to this:

NameVirtualHost *
<VirtualHost *>
	ServerName localhost:80
	DocumentRoot /www
</VirtualHost>
##########VIRTUAL HOST SETUP##########
# WWW.MY_SITE1.FREDTEST.MINE.NU
<VirtualHost *>
ServerName www.my_site1.fredtest.mine.nu
DocumentRoot I:/site1
</VirtualHost>

##########VIRTUAL HOST SETUP##########
# WWW.MY_SITE2.FREDTEST.MINE.NU
<VirtualHost *>
ServerName www.my_site2.fredtest.mine.nu
DocumentRoot /z_www/site2
</VirtualHost>

##########VIRTUAL HOST SETUP##########
# WWW.RIC.COM
<VirtualHost *>
ServerName www.ric.com
DocumentRoot /z_www/site3
</VirtualHost>

There will come a point where you will need to edit the configuration file below I explain each block so you can fine tune if you wish. I tend to hack Uniform Server to death well more often than not I kill Apache off due to some typo in this configuration file hence I wrote this page Debugging Tips.

Top

Vhost detail

Looking at the above automatically generated hosts in detail:

Apache Directive Main Points to Note

NameVirtualHost *

Enables Named Virtual Hosting

<VirtualHost *>
  ServerName localhost
  DocumentRoot /www/
</VirtualHost>

Each virtual host is a separate block placed within a VirtualHost tag pair.

  • <VirtualHost *>
  • </VirtualHost>

First virtual host listed is the default site used if Apache can not resolve a request.

<VirtualHost *>
  ServerName www.my_site1.fredtest.mine.nu
  DocumentRoot I:/site1
</VirtualHost>

Server name is the text typed into a browser address bar to access the site.

Apache sees this as a text string and makes no distinction between local or remote access.

<VirtualHost *>
  ServerName www.my_site2.fredtest.mine.nu
  DocumentRoot /z_www/site2
</VirtualHost>

DocumentRoot defines the top-level folder path for that particular server name.

Apache assigns this as the root folder for that server name.

<VirtualHost *>
  ServerName www.ric.com
  DocumentRoot /z_www/site3
</VirtualHost>

The domain ric.com is hosted at another registrar however www.ric.com will be processed it's pointed to this server's IP address. Apache processes any request supplied to it.

Top

Vhost template

The configuration file provides a template for Vhost as follows:

# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
#<VirtualHost *>
#    ServerAdmin webmaster@dummy-host.example.com
#    DocumentRoot /www/docs/dummy-host.example.com
#    ServerName dummy-host.example.com
#    ErrorLog logs/dummy-host.example.com-error_log
#    CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>

What’s important all Vhost’s inherit most of the main server configuration attributes. These can be overridden by placing the appreciate directive in the virtual host.

Top

Portability

Assuming you have placed all your site root folders below folder *\Uniform Server\udrive they are portable. You can copy Uniform Server to a USB stick and run on another PC. However if you are using virtual hosts corresponding entries in the Hosts file must be copied to the new host PC. This can be inconvenient and easily resolvable using a PAC file

Even this has a slight irritation you need to configure the hosts browser so it can find this file. The solution is to use a portable browser making everything completely portable.

I cover PAC and portable browsers on the new two pages.

Summary

I have shown how easy it is to implement virtual host on Uniform Server using apanel. Because a local Hosts file is used the configuration is not portable if you need a portable solution check out the PAC page.

Top


  Ric