How To Assign a Virtual Local Domain Name To Your Localhost

A. Step by Step Instructions to Assign Multiple Virtual Domain Names and Websites to Your Localhost


With the help of this tutorial you can assign a virtual domain name to your localhost.

You can access your local website with your favourite name like http://mysite.web instead of http://localhost and also you can assign different virtual domain names to different local webistes.

Requirements:

  • Apache webserver installed. It would be nice if you have WAMP server. This tutorial is based on WAMP server. But it works on any Apache server.

Procedure:

Part 1:

        1. First go to location "C:\WINDOWS\system32\drivers\etc" directory.(or where you installed windows).
            Then open "hosts" file with simple text editor like notepad.
        2. You'll see "127.0.0.1 localhost" at the end of the file.
            In the next line add your virtual domain name like the example shown below.
            Here mysite.web is just my example. You can use anything you like.
127.0.0.1    mysite.web              #this is virtual domain name.
        3. Now save the hosts file. mysite.web is just an example. You can add anything like mywebsite.local and you can use any extension or no extension at all.
            You can simply add mysite also.
        4. Now test your virtual domain. Just type "http://mysite.web" You must see your wamp page or webservers defalut page. If not go through the process again.
NOTE: Don't use any real domain name like www.google.com or your own domain name if you have any. If you did so, you cannot that access the original remote site. This is because, 127.0.0.1 is loopback address, anything with that address will never leave your computer.


Part 2- Now some difficult part. Assigning this virtual domain name to your web site in your local server.

        1. Open your httpd.conf file in conf directory of Apache webserver folder.
            If you are using WAMP click on WAMP icon, go to Apache menu and select httpd.conf there.
 edit-httpd-wamp-web-server-virtual-domain
        2. Create a new folder mysite in your C:\>. And create a new page index.html. These are for testing purposes.
            If you have a local website, specify the full path of website in below code.
        3. Now add the following code at the end of the httpd.conf file.
NOTE: PLEASE TYPE IN THE CODE MANUALLY without comments, DON'T COPY AND PASTE.
  NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
   ServerName localhost
   DocumentRoot "C:/wamp/www"                      #this is default wamp root for websites,
</VirtualHost>

<VirtualHost 127.0.0.1>
   ServerName mysite.web                           #your virtual domain name
   DocumentRoot "C:/mysite"                        #location of your site, no extenison needed.

<Directory C:/mysite>                              #again location of your website
   Order Allow,Deny
   Allow from all
 </Directory>
 </VirtualHost>
Save this file. Restart your WAMP server. Now type http://mysite.web You'll see the index page of mysite.
If you want another website, first add another virtual domain in hosts file as shown in part1.
And then copy and paste the following code at the end of httpd.conf file. Just change the virtual domain name, and locations of website.
<VirtualHost 127.0.0.1>
   ServerName mywebsite.web               #change this virtual domain name
   DocumentRoot "C:/mywebsite"            #location of your site, change this.

   <Directory C:/mywebsite>               #again location of your website, change this
   Order Allow,Deny
   Allow from all
   </Directory>
 </VirtualHost>

You can add as many websites as you wish. Just repeat the above procedure. And access all your local websites with your favorite name. It will be fun and will save lot of your time. 

B. Setting Up Your Localhost As Example.Com


Often times when people install WAMP [or LAMP] stacks on their machines, they’ll commonly use http://localhost/ to access their local server. I did so for years until I started working with dynamic sub domains and needed to use something other than localhost to access my server.
Example.com [or example.org, example.net] is a special domain as defined in RFC 2606 used for, you guessed it, examples. These domains will never be available to the public, so there’s no need to worry about not being able to access the actual site in the future.
Setting this up involves three main parts:
  1. Set up a static IP on your machine.
  2. Configuring Apache.
  3. Editing your host file.

Set up your static IP
There’s really no need to go into this. There are a ton of sites out there to help you set up your static IP:
– Windows
– Appletosh
– Linux

Apache Config
Everyone will have their httpd.conf file set up differently, so use this as an example of how to accomplish this. Keep in mind this file fragment should appear at the bottom of httpd.conf. I have also included my SSL layer here as well. It’s not important for most installations, but I included to show how to use it with example.com.
The main theory behind this is to set up everything under a VirtualHost:
Note: 192.168.1.23 is my machine’s static IP, so be sure to replace it with YOUR static IP

# apache #
    NameVirtualHost *:80

# Dir structure #
    Listen 192.168.1.23:80
    ServerName 192.168.1.23:80

    <VirtualHost *:80>
        DocumentRoot "S:/apache/htdocs/website1.com/"
#       DocumentRoot "S:/apache/htdocs/website2.com/"
#       DocumentRoot "S:/apache/htdocs/website3.com/"
#       DocumentRoot "S:/apache/htdocs/website4.com/"
#       DocumentRoot "S:/apache/htdocs"
        ServerName www.example.com:80
    </VirtualHost>

# PMA #
    <VirtualHost *:80>
        DocumentRoot "S:/apache/htdocs/sql"
        ServerName sql.example.com:80
    </VirtualHost>

# SSL -- Don't include this if you don't use SSL #
    LoadModule ssl_module modules/mod_ssl.so
    SSLRandomSeed startup builtin
    SSLRandomSeed connect builtin
    SSLMutex default
    SSLSessionCache none

    Listen www.example.com:443

    <VirtualHost *:443>
        DocumentRoot "S:/apache/htdocs/website1.com/"
#       DocumentRoot "S:/apache/htdocs/website2.com/"
#       DocumentRoot "S:/apache/htdocs/website3.com/"
#       DocumentRoot "S:/apache/htdocs/website4.com/"
#       DocumentRoot "S:/apache/htdocs"
        ServerName www.example.com:443

        SSLEngine On
        SSLCertificateFile S:/apache/conf/pfs.crt
        SSLCertificateKeyFile S:/apache/conf/pfs.key
        SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
        ErrorLog logs/ssl_www.example.com-error.log
    </VirtualHost>

Some directories are commented out, this allows me to run multiple sites on the server root. I just un-comment the site I want to work on, comment out the rest, restart the server and I’m good to go.

Host Files
Host files are like mini, stripped-down DNS servers that sit on your machine. This is where you tell your computer, “When I type in http://www.example.com, I want it to go to my local server, not the internet”. Once again, this is something widely available on the net:
– Windows
– Appletosh
– Linux
Once you have your host files editable, add this to the bottom [don't over-write existing entries!] … also be sure to replace: 192.168.1.23 with YOUR static IP:

192.168.1.23    localhost
192.168.1.23    example.com
192.168.1.23    www.example.com
192.168.1.23    sql.example.com

Using your machine’s static IP over 127.0.0.1 will allow other machines on your net work to connect to your server via http://www.example.com … but you’ll need to edit those machine’s host files as well .. just use the same info from above on all your machines. Also make sure your firewall allows communication on Apache’s ports [80, 443].
You will more than likely have to restart your web browser or possibly your computer to get this to work.
DNS
Using a DNS server would eliminate the host file layer of this guide, but since I don’t use one, it’s not in this tutorial … but it would be ideal for larger companies or offices [after all, it's just me, myself and I in my company].
One thing I will openly admit about this tutorial is that I have very little experience configuring apache. everything you see here I found around the internet and played with it until it worked. With that said, if you are more knowledgeable with apache config and have something to add, PLEASE DO SO! :-)

Comments

Popular posts from this blog

16 Design Tools for Prototyping and Wireframing

What is Functional and Non Functional Requirement

LESS CSS – Beginner’s Guide