97

I am installing the Wamp Server on another computer to run a mid-sized database and UI. I have been successful in blocking IIS and routing the server to Localhost:8080. But whenever I try to access on of my projects from the localhost homepage, in the www file; I get redirected to a Page not found error.

When I hover above the links the directory always comes up "http:// ProjectFolderNameHere /". when it's supposed to be "http:// LocalHost:8080 / ProjectFolderNameHere /". What can I do to get the links working properly?

My Machine runs on Windows 7 Home Edition 64-bits, and I already have Microsoft's IIS disabled.

Hanif Formoly
  • 301
  • 5
  • 13
user3638431
  • 971
  • 1
  • 7
  • 3
  • Can you access localhost:8080/index.html(assuming you have one) ? Where are the links stored that show you those broken URLs? Who wrote them? It seems like those links are just written badly. – Patrick May 14 '14 at 21:14
  • Nope But I tried connecting to one of my files, all set here thank you! – user3638431 May 14 '14 at 21:16
  • If you found a solution, answer your own question for future reference :) – Patrick May 14 '14 at 21:26
  • Ok So what I did is I took Patrick's advice, but instead I typed in localhost:8080/ProjectFilename and finally got access to my project. My problem still remains but I found a way around it. – user3638431 May 14 '14 at 21:39
  • Well I do believe your links(the ones you click) are written wrong. If you could shed more light on how do you get to those links, where are they written before you 'click' on them, I could assist further – Patrick May 14 '14 at 21:53
  • I ran into the same situation when I upgrade to the current (v2.5) of Wampserver. Check out my answer below. – TheSteven Jun 29 '14 at 11:11
  • If this is on your own workstation and not in a production set-up, you can simply right-click the WAMP tray icon and select "Wamp Settings" -> "Add localhost in url", and it will work like it did in the old WAMP. – chjortlund May 03 '17 at 08:56
  • I Made solution for this issue : https://youtu.be/rRwG591fzU8 – Ns789 Dec 01 '20 at 05:53

13 Answers13

172

How to create a Virtual Host in WampServer


WAMPServer 3 has made this process much easier!

You can do almost everything from a utility provided as part of WAMPServer.

  • Create a folder inside to contain your project.site. This can be under the C:\wamp\www\ directory or in a completely seperate folder like C:\websites.

  • Create a folder inside the location you have chosen EG C:\websites\project1\www or under the c:\wamp\www\project1\www

  • Now open localhost wampmanager->localhost and click on the link Add a Virtual Host under the TOOLS section on the homepage.

You will see a page like this:

enter image description here

  • Fill in the fields as specified by the instructions above each field

  • The Virtual Host config will have been created for you.

  • Now you must restart the DNS Cache. You can do this from the wampmanager menus like this right click wampmanager->Tools->Restart DNS. The DNS Cache will be restarted and then Apache will also be stopped and restarted. When the wampmanager icon goes green again all is completed.

  • Now you must create a simple index.php file or install your site into the folder you created above.

  • Assuming your VH was called project.dev You should see that name under the Your Virtual Hosts Section of the WAMPServer homepage.

  • You can launch the site from this menu, or just use the new Domain Name in the address bar EG project1.dev and the site shoudl launch.


Old WAMPServer 2.5 mechanism, or if you want to do it all manually

There has been a change of concept in WampServer 2.5 and above and there is a good reason for this change!

In WampServer it is now STRONGLY encouraged to create a Virtual Host for each of your projects, even if you hold them in a \wamp\www\subfolder structure.

Virtual Hosts Documentation

Virtual Host Examples

The WampServer home page ( \wamp\www\index.php ) now expects you to have created a Virtual Host for all your projects and will therefore work properly only if you do so.

History

In order to make life easier for beginners using WampServer to learn PHP Apache and MySQL it was suggested that you create subfolders under the \wamp\www\ folder.

wamp
  |-- www
       |-- Chapter1
       |-- Chapter2
       |-- etc

These subfolders would then show as links in the WampServer Homepage under a menu called 'Your Projects' and these links would contain a link to localhost/subfoldername.

Acceptable only for simple tutorials

This made life easy for the complete beginner, and was perfectly acceptable for example for those following tutorials to learn PHP coding. However it was never intended for use when developing a real web site that you would later want to copy to your live hosted server. In fact if you did use this mechanism it often caused problems as the live sites configuration would not match your development configuration.

The Problem for real website development.

The reason for this is of course that the default DocumentRoot setting for wamp is

DocumentRoot "c:/wamp/www/"

regardless of what your subfolder was called. This ment that often used PHP code that queried the structure or your site received different information when running on your development WampServer to what it would receive when running on a live hosted server, where the DocumentRoot configuration points to the folder at the top of the website file hierarchy. This kind of code exists in many frameworks and CMS's for example WordPress and Joomla etc.

For Example

Lets say we have a project called project1 held in wamp\www\project1 and run incorrectly as localhost/project1/index.php

This is what would be reported by some of the PHP command in question:

$_SERVER['HTTP_HOST'] = localhost
$_SERVER['SERVER_NAME'] = localhost
$_SERVER['DOCUMENT_ROOT'] = c:/wamp/www

Now if we had correctly defined that site using a Virtual Host definition and ran it as http://project1 the results on the WAMPServer devlopment site will match those received when on a live hosted environment.

$_SERVER['HTTP_HOST'] = project1
$_SERVER['SERVER_NAME'] = project1
$_SERVER['DOCUMENT_ROOT'] = c:/wamp/www/project1

Now this difference may seem trivial at first but if you were to use a framework like WordPress or one of the CMS's like Joomla for example, this can and does cause problems when you move your site to a live server.

How to create a Virtual Host in WampServer

Actually this should work basically the same for any wndows Apache server, with differences only in where you may find the Apache config files.

There are 3 steps to create your first Virtual Host in Apache, and only 2 if you already have one defined.

  1. Create the Virtual Host definition(s)
  2. Add your new domain name to the HOSTS file.
  3. Uncomment the line in httpd.conf that includes the Virtual Hosts definition file.

Step 1, Create the Virtual Host definition(s)

Edit the file called httpd-hosts.conf which for WampServer lives in

\wamp\bin\apache\apache2.4.9\conf\extra\httpd-vhosts.conf

(Apache version numbers may differ, engage brain before continuing)

If this is the first time you edit this file, remove the default example code, it is of no use.

I am assuming we want to create a definition for a site called project1 that lives in

\wamp\www\project1

Very important, first we must make sure that localhost still works so that is the first VHOST definition we will put in this file.

<VirtualHost *:80>
    DocumentRoot "c:/wamp/www"
    ServerName localhost
    ServerAlias localhost
    <Directory  "c:/wamp/www">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require local
    </Directory>
</VirtualHost>

Now we define our project: and this of course you do for each of your projects as you start a new one.

<VirtualHost *:80>
    DocumentRoot "c:/wamp/www/project1"
    ServerName project1
    <Directory  "c:/wamp/www/project1">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require local
    </Directory>
</VirtualHost>

NOTE: That each Virtual Host as its own DocumentRoot defined. There are also many other parameters you can add to a Virtual Hosts definition, check the Apache documentation.

Small aside

The way virtual hosts work in Apache: The first definition in this file will also be the default site, so should the domain name used in the browser not match any actually defined virtually hosted domain, making localhost the first domain in the file will therefore make it the site that is loaded if a hack attempt just uses your IP Address. So if we ensure that the Apache security for this domain is ALWAYS SET TO

Require local

any casual hack from an external address will receive an error and not get into your PC, but should you misspell a domain you will be shown the WampServer homepage, because you are on the same PC as WampServer and therfore local.

Step 2:

Add your new domain name to the HOSTS file. Now we need to add the domain name that we have used in the Virtual Host definition to the HOSTS file so that windows knows where to find it. This is similiar to creating a DNS A record, but it is only visible in this case on this specific PC.

Edit C:\windows\system32\drivers\etc\hosts

The file has no extension and should remain that way. Watch out for notepad, as it may try and add a .txt extension if you have no better editor. I suggest you download Notepad++, its free and a very good editor.

Also this is a protected file so you must edit it with administrator privileges, so launch you editor using the Run as Administrator menu option.

The hosts file should look like this when you have completed these edits

127.0.0.1 localhost
127.0.0.1 project1

::1 localhost
::1 project1

Note that you should have definitions in here for the IPV4 loopback address 127.0.0.1 and also the IPV6 loopback address ::1 as Apache is now IPV6 aware and the browser will use either IPV4 or IPV6 or both. I have no idea how it decides which to use, but it can use either if you have the IPV6 stack turned on, and most window OS's do as of XP SP3.

Now we must tell windows to refresh its domain name cache, so launch a command window again using the Run as Administrator menu option again, and do the following.

net stop dnscache
net start dnscache

This forces windows to clear its domain name cache and reload it, in reloading it will re-read the HOSTS file so now it knows about the domain project1.

Step 3: Uncomment the line in httpd.conf that includes the Virtual Hosts definition file.

Edit your httpd.conf, use the wampmanager.exe menus to make sure you edit the correct file.

Find this line in httpd.conf

# Virtual hosts
#Include conf/extra/httpd-vhosts.conf

And just remove the # to uncomment that line.

To activate this change in you running Apache we must now stop and restart the Apache service.

wampmanager.exe -> Apache -> Service -> Restart Service

Now if the WAMP icon in the system tray does not go GREEN again, it means you have probably done something wrong in the \wamp\bin\apache\apache2.4.9\conf\extra\httpd-hosts.conf file.

If so here is a useful mechanism to find out what is wrong. It uses a feature of the Apache exe (httpd.exe) to check its config files and report errors by filename and line numbers.

Launch a command window.

cd \wamp\bin\apache\apache2.4.9\bin
httpd -t

So fix the errors and retest again until you get the output

Syntax OK

Now there is one more thing.

There are actually 2 new menu items on the wampmanager menu system. One called 'My Projects' which is turned on by default. And a second one, called 'My Virtual Hosts', which is not activated by default.

'My Projects' will list any sub directory of the \wamp\www directory and provide a link to launch the site in that sub directory. As I said earlier, it launches 'project1` and not 'localhost/project1' so to make the link work we must create a Virtual Host definition to make this link actually launch that site in your browser, without the Virtual Host definition it's likely to launch a web search for the site name as a keyword or just return a site not found condition.

The 'My Virtual Hosts' menu item is a little different. It searches the file that is used to define Virtual Hosts ( we will get to that in a minute ) and creates menu links for each ServerName parameter it finds and creates a menu item for each one. This may seem a little confusing as once we create a Virtual Host definition for the sub directories of the \wamp\www folder some items will appear on both of the 'My Projects' menu and the 'My Virtual Hosts' menu's.

How do I turn this other 'My Virtual Hosts' menu on?

  • Make a backup of the \wamp\wampmanager.tpl file, just in case you make a mistake, its a very important file.
  • Edit the \wamp\wampmanager.tpl
  • Find this parameter ;WAMPPROJECTSUBMENU, its in the '[Menu.Left]' section.
  • Add this new parameter ;WAMPVHOSTSUBMENU either before or after the ;WAMPPROJECTSUBMENU parameter.
  • Save the file.
  • Now right click the wampmanager icon, and select 'Refresh'. If this does not add the menu, 'exit' and restart wampmanager.

Big Note The new menu will only appear if you already have some Virtual Hosts defined! Otherwise you will see no difference until you define a VHOST.

Now if you take this to its logical extension

You can now move your web site code completely outside the \wamp\ folder structure simply by changing the DocumentRoot parameter in the VHOST definition. So for example you could do this:

Create a folder on the wamp disk or any other disk ( beware of network drive, they are a bit more complicated)

D:
MD websites
CD websites
MD example.com
CD example.com
MD www

You now copy your site code to, or start creating it in the \websites\example.com\www folder and define your VHOST like this:

<VirtualHost *:80>
    DocumentRoot "d:/websites/example.com/www"
    ServerName example.dev
    ServerAlias www.example.dev
    <Directory  "d:/websites/example.com/www">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    php_flag display_errors Off
    php_flag log_errors On

    php_value max_upload_size 40M
    php_value max_execution_time 60
    php_value error_log "d:/wamp/logs/example_com_phperror.log"
</VirtualHost>

Then add this new development domain to the HOSTS file:

127.0.0.1 localhost
::1 localhost

127.0.0.1 project1
::1 project1

127.0.0.1 example.dev
::1 example.dev

NOTE: It is not a good idea to use a ServerName or ServerAlias that is the same as your live domain name, as if we had used example.com as the ServerName it would mean we could no longer get to the real live site from this PC as it would direct example.com to 127.0.0.1 i.e. this PC and not out onto the internet.

ALSO: See that I have allowed this site to be accessed from the internet from within the VHOST definitions, this change will apply to only this site and no other. Very useful for allowing a client to view your changes for an hour or so without having to copy them to the live server. This does mean that we have to edit this file manually to turn this access on and off rather than use the Put Online/Offline menu item on wampmanager.

Also I have added some modifications to the PHP config, again that will only apply to this one site. Very useful when maintaining a site with specific requirement unlike all the other sites you maintain. I guess we can assume from the parameters used that it has a long running page in it somewhere and it is very badly written and will not run with errors being displayed on the browser without making a horrible mess of the page. Believe me sites like this exist and people still want them maintained badly. But this mean we only have to change these parameters for this specific site and not globally to all Virtual sites running on WampServer.

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Hugely helpful. This really should be the accepted answer. Very informative and if followed properly, will have you up and running in about 10 minutes. Great advice. One quick point though. I got a .htaccess: Invalid command 'RewriteEngine' error. I had to tick rewrite_module under the Apache Modules menu. Could someone answer why I had to do this? – Chris Dec 01 '14 at 18:21
  • 1
    @Chris Apache's rewrite_module is the code that actually provides the rewrite engine. Without it apache does not know how to do url rewriting. Basicaly its not part of the core of apache but a addon for want of a better description – RiggsFolly Dec 04 '14 at 12:41
  • "Edit the file called httpd-hosts.conf which for WampServer lives in \wamp\bin\apache\apache2.4.9\conf\extra\httpd-hosts.conf" In my fresh install of wampserver 2.5, this file does not exist. If you meant httpd-vhosts.conf, please correct. – bcsteeve Jun 29 '15 at 21:45
  • @bcsteeve Thanks for that – RiggsFolly Jun 29 '15 at 22:12
  • Wow - great help for us newb php'ers (and WAMP'ers) -thanks! – SunfiShie Jul 17 '15 at 19:46
  • Can you please teach how to run such host from local server? I have 3 computers in my home, previously i used to call them with ip or computer name (http://xena-pc/project1/) from next computer now how to call vhosts?? – Khadka Pushpendra Aug 10 '15 at 06:45
  • @KhadkaPushpendra Ask this as another question – RiggsFolly Aug 10 '15 at 23:40
  • You may probably need `Options Indexes FollowSymLinks` within directory code when you are trying to setup virtual host outside from `\wamp\` folder structure. – user3733831 Oct 27 '15 at 03:36
  • @user3733831 Good point, I have added that to the post. Thanks – RiggsFolly Oct 27 '15 at 09:27
  • @RiggsFolly, What is the reason to use code below the `` when we setup virtual hosts outside from `\wamp` folder structure. I mean this code of blocks `php_flag display_errors Off php_flag log_errors On php_value max_upload_size 40M php_value max_execution_time 60 php_value error_log "d:/wamp/logs/example_com_phperror.log"` – user3733831 Oct 28 '15 at 08:21
  • @user3733831 That is a way of setting PHP parameters specifically for this Virtual Host. For example, you may have an old app or a badly written app and want to turn off on screen error reporting, or an app that requires the `max_execution_time` to be increased. These changes will only effect this one Virtual Host instance. Making these changes in `PHP.INI` would effect all VH's this way it does not. – RiggsFolly Oct 28 '15 at 09:03
  • Now I wish the next version of WampServer would include a "_new project_" feature that would automate all of this. – starleaf1 Feb 19 '16 at 08:28
  • 2
    @starleaf1 You wish is our command! Run `localhost/add_vhost.php` on WAMPServer 3, check out [this post on WAMPServers Forum](http://forum.wampserver.com/read.php?2,138295,138295#msg-138295) – RiggsFolly Feb 19 '16 at 09:09
  • Thanks for this! A lot of great information! Having come back to WAMP I completely forgot about the System Host file. Thanks again! – Das Aug 24 '16 at 03:00
  • Thanks for the information and I can see why a virtual host might be useful, but I have a folder for a website and inside that I have many versions numbered of the same website as I make changes. So adding a virtual host for everyone of these would be impossible to do. – Thomas Williams Aug 27 '16 at 18:23
  • Thanks alot for this! I am starting work on a new web project and they require all work be done locally. With the Virtual Hosts is place - I can create many projects all configured differently! YOU ARE AWESOME!! – Radmation Nov 19 '16 at 19:29
  • note: project/folder name cannot contain underline sign ("_"), it may contain minus sign "-". – Atara May 23 '17 at 11:40
  • 1
    Talk about in-depth explanation! +1 for the `right click wampmanager -> Tools -> Restart DNS` that did the trick after adding a new virtual host and not understanding why it wasn't working still. – Paesano2000 Oct 03 '19 at 00:23
  • Video solution can be find here: https://youtu.be/rRwG591fzU8 – Ns789 Dec 01 '20 at 05:56
74

I believe this is the best solution:

Open index.php in www folder and set

change line 30:$suppress_localhost = true;

to $suppress_localhost = false;

This will ensure the project is prefixed with your local host IP/name

Sudheer
  • 2,955
  • 2
  • 21
  • 35
Hussain
  • 947
  • 5
  • 15
  • 1
    thanks! solved problem but icons/images of /icons path not showing blank.git, back.gif, folder.gif shows not found. any solution for this too? – Shubham Badal Nov 08 '14 at 09:38
  • 3
    Yes, create a virtual Host for each site, see answer above for how to. – RiggsFolly Nov 26 '14 at 10:48
  • 1
    There is now (WAMPSERVER 3) a simple menu option on wampmanager to do this if you really dont understand what a bad idea it is. `right click wampmanager->Wamp Settings->Add localhost to url` The only good thing about this is you can then turn it off again easily when you realise what a bad idea it was in the first place! – RiggsFolly Jun 06 '17 at 17:00
24

Open index.php in www folder and set

$suppress_localhost = false;

This will prepend http://localhost/ to your project links

Arun Unnikrishnan
  • 2,339
  • 2
  • 25
  • 39
11

In order to access project from the homepage you need to create a Virtual Host first.

Most easiest way to do this is to use Wamp's Add a Virtual Host Utility.

Just follow these steps:

  1. Create a folder inside "C:\wamp\www\" directory and give it a name that you want to give to your site for eg. 'mysite'. So the path would be "C:\wamp\www\mysite".
  2. Now open localhost's homepage in your browser, under Tools menu click on Add a Virtual Host link.
  3. Enter the name of the virtual host, that name must be the name of the folder we created inside www directory i.e. 'mysite'.
  4. Enter absolute path of the virtual host i.e. "C:\wamp\www\mysite\" without quotes and click the button below saying 'Start the creation of the VirtualHost'.
  5. Virtual Host created, now you just need to 'Restart DNS'. To do this right click the wamp server's tray menu icon, click on Tools > Restart DNS and let the tray menu icon become green again.
  6. All set! Now just create 'index.php' page inside "C:\wamp\www\mysite\" directory. Add some code in 'index.php' file, like
    <?php echo "<h1>Hello World</h1>"; ?>

Now you can access the projects from the localhost's homepage. Just click the project link and you'll see 'Hello World' printed on your screen.

Rakesh
  • 706
  • 6
  • 10
  • 1
    I think this should be the accepted awnser because i had the same problem and it worked! – Nmaster88 Sep 27 '16 at 16:50
  • 1
    this one is latest and perfect and worked for me . thanks . some of the above answers are lengthy and obsolete. – Junaid Dec 06 '16 at 08:24
  • 2
    This should be the current accepted answer as of 4/18/2017. Worked perfectly for me. – rolo Apr 19 '17 at 00:08
  • this worked, however the virtualhost for one of the project folders I created, say example, has not changed the example under the 'Your Projects' heading as a link.. why is this? – Krys Oct 13 '20 at 05:24
  • If anyone has any issues with the project folders not turning from black text to http links, I followed this quick way to enable it: open index.php from wamp/www/ , copy the string on line 152:
  • '.str_replace('.conf','',$file).'
  • and replace string
  • . $file .
  • on line 359. hey presto! – Krys Oct 13 '20 at 06:17