How to setup virtual hosts

Over the last few days i had some problems with a project, when i tried to move it from development server to production server.  More exactly, i encountered difficulties when making a .htaccess file. Bassically i had to make 2 versions of this file, one for offline use, and one for online. This it’s just a small example of the problems you can encounter when working offline. Then i said to me: it must be a solution for this, to ease my work, and started to search google, and found the answer: VIRTUAL HOSTS.

What can you do with virtual hosts? Well, you can use them to setup on your computer (development server) projects(websites) in such a way you can access them by a simple URL you decide.

Let’s take a real example and see how we can setup a virtual host

I have a project called mywebsite, and it’s located under my local server root (D:\php\www\). So normally i would access it by typing in browser http://localhost/mywebsite. Btw, i am using WAMP. And what we want to achieve is to be able to type in browser something like this: http://mywebsite. Let’s make this in steps:

  1. When you type a url in browser,  first thing that happens is to look if the url is to be found in a file HOSTS located here C:/WINDOWS/system32/drivers/etc/ If is there, then it won’t look on the internet. So, go and open that file. You will find a line like this there 127.0.0.1       localhost . Right under it let’s right something like 127.0.0.1       mywebsite. The idea is that, when in browser its typed the word mywebsite, the browser looks in this file, and it sees its there, and it redirects to local server, and of course, no need to look on the internet.
  2. I said it redirects to local server. But we want to redirect to a certain directory from our local server (D:\php\www\mywebsite). For this locate your httpd.conf file. It’s the apache configuration file. At the end write something like this:
?View Code APACHE
1
2
3
4
5
6
7
8
9
10
11
12
# Be sure to only have this line once in your configuration
NameVirtualHost 127.0.0.1
 
<VirtualHost 127.0.0.1>
    ServerName localhost
    DocumentRoot "D:\php\www"
</VirtualHost>
 
<VirtualHost 127.0.0.1>
    ServerName mywebsite
    DocumentRoot "D:\php\www\mywebsite"
</VirtualHost>

Now let’s explain.

  • The second line it’s the apache directive that specifies the IP address on which the server will receive requests for the name-based virtual hosts. For more info go here.
  • The lines 9-12 defines our virtual host. Basically  here we tell apache where to go when we type in browser mywebsite . For more info go here
  • What about the 4-7 lines? Those are here for one simple reason: to still to be able to access server root, localhost.

That’s it. Don’t forget to restart your server.

VN:F [1.1.5_471]
Rating: 10.0/10 (3 votes cast)

Share/Save/Bookmark

One Response

  1. Cioroianu Nicolin Says:

    I’m not a web developer but you make it interested for me!Thanks!

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.