Search code examples
phpapachewindows-7wampdocument-root

How to create Apache Virtual host?


Im using Windows 7, and there I installed apache server in c:\wamp\apache\, PHP is located in c:\wamp\php\, and loaded as module.

So lets say I'm building test project and I only have index.php in that project. Folder of that project will be located in c:\wamp\apache\htdocs\test\index.php and I can access that through my browser: localhost\test

This all works nice, but I have problem when I include images, css and js files. lets say I want to include css style, i must use this:

<link rel="stylesheet" href="/test/images/image.jpg" />

and I would like to use this for a href:

<link rel="stylesheet" href="/images/image.jpg" />

So that / is root, and from there I can search for files inside my project folder.

What should I do? Should I create virtual server for every project, or there is a way that every folder in my htdocs folder acts as it would on live server, where / would translate to document root?

How should I setup Apache server so that every folder in my htdocs folder acts as server for itself? When I use XAMPP it works like that.

Thanks!


Solution

  • If you want to avoid these kind of headaches, you should have a virtualhost for every project.

    You can just put in your httpd.conf (with wamp: left click on wamp > apache > httpd.conf)

    # To access your old projects under C:\wamp\www
    NameVirtualHost localhost:80
    <VirtualHost localhost:80>
    ServerName localhost
    ServerAlias localhost
    DocumentRoot C:\wamp\www
    <Directory "C:\wamp\www">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1
    </Directory>
    </VirtualHost>
    
    # each virtualhost should look like that
    NameVirtualHost localhost.yourproject.com:80
    <VirtualHost localhost.yourproject.com:80>
    ServerName localhost.yourproject.com
    ServerAlias localhost.yourproject.com
    DocumentRoot C:\path\to\yourproject
    <Directory "C:\path\to\yourproject">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1
    </Directory>
    </VirtualHost>
    

    Don't forget to restard wamp, and include in your hostfile (C:\Windows\System32\drivers\etc\hosts):

    127.0.0.1       localhost.yourproject.com