How to modify web address in localhost using wamp? - windows

How to change localhost/sample/index.php to dev.sample.com? I make it in ubuntu using nginx I wonder how to make it in windows using wamp server.

First you need to add this line:
127.0.0.1 dev.sample.com
To C:\Windows\System32\drivers\etc\hosts (edit with notepad).
After that, uncomment the line (around line number 512):
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf
Of C:\wamp\bin\apache\apacheX.Y.Z\conf\httpd.conf by removing the inital #, so you get:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
Finally, edit C:\wamp\bin\apache\apacheX.Y.Z\conf\extra\httpd-vhosts.conf by adding this to the end of the file:
<VirtualHost *:80>
DocumentRoot "c:/YOURPROJECTPATH/sample"
ServerName dev.sample.com
</VirtualHost>
The apacheX.Y.Z stands for the Apache version your WAMP is using.
You can check more examples on how to create virtual hosts in the Apache's docs examples (WAMP = Windows+Apache+MySQL+PHP).
Once you're done with these edits, left click the WAMP icon near the window's clock, go to Apache > Service > Restart Service and you can now use the new URL. A minor suggestion: you can choose more uncommon URLs, e.g. sample.developer so you don't end up on a real website when Apache service is down.
For keeping the http://localhost/ redirection
You can leave two lines in the C:\Windows\System32\drivers\etc\hosts file:
127.0.0.1 localhost
127.0.0.1 dev.sample.com
Or you can use the same line for this:
127.0.0.1 localhost dev.sample.com
However, I suggest that you don't pile up many references in one line (4 to 5 should be ok). It's preferable to have more lines to the same IP.
Along with your new site's virtual host, you can also have this in C:\wamp\bin\apache\apacheX.Y.Z\conf\extra\httpd-vhosts.conf (above or below the other):
<VirtualHost *:80>
DocumentRoot "c:/wamp/www"
ServerName localhost
</VirtualHost>

Related

Apache 2.4 ignore httpd-vhosts.conf file

I have been following this steps setup apache virtualhost (windows) to create a Virtual Host in Windows but I don't know what is wrong because it doesn't work.
I want a virtual host with this url: http://local.shop.
httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "C:/Apache24/htdocs/"
ServerName localhost
ServerAlias localhost
<Directory "C:/Apache24/htdocs/">
AllowOverride All
</Directory>
<VirtualHost *:80>
DocumentRoot "C:/Apache24/htdocs/shop/"
ServerName local.shop
ServerAlias local.shop
<Directory "C:/Apache24/htdocs/shop/">
AllowOverride All
</Directory>
ErrorLog "logs/localhost.html-error.log"
CustomLog "logs/localhost.html-access.log" common
</VirtualHost>
hosts
127.0.0.1 localhost
127.0.0.1 local.shop
If I try to access to http://localhost the page served is the page which is on C:/Apache24/htdocs/shop/ but if I try to access to http://local.shop I've got the next error ERR_NAME_NOT_RESOLVED
What am I doing wrong?
Edit 1:
I have removed ServerAlias directives and now If I try to access to http://localhost the page served is the right page, but if I try to acces to http://local.shop still doesn't work. I've got the same error ERR_NAME_NOT_RESOLVED
Edit 2:
I have used "ping" command from windows to try to reach to each host. And I get a response from localhost but not from local.shop
Edit 3:
I have made a change in my definition of localhost in httpd-vhosts.conf. I have changed DocumentRoot to "C:/Apache24/htdocs/shop"
<VirtualHost *:80>
DocumentRoot "C:/Apache24/htdocs/shop"
ServerName localhost
ServerAlias localhost
<Directory "C:/Apache24/htdocs/">
AllowOverride All
</Directory>
</VirtualHost>
Instead of getting the default page from shop directory, I still getting the default page from original localhost. It seems like Apache ignore httpd-vhosts.conf file.
Apache maybe listening (in case you restarted it after editing vhosts file), but nothing points to it. You need to edit your hosts file (located under C:\Windows\System32\drivers\etc\hosts) and point domain "local.shop" to IP address 127.0.0.1 so Apache can pickup from there. You may edit that file with Notepad, opened with admin rights.
It should looks something like this:
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# 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
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 local.shop
I encountered this same issue installing Apache 2.4 on Nov. Tuesday 2nd, 2021 at 2:37 pm.
Things you need to do if you are installing Apache without WAMP or LAMP:
If you do not want to do much configurations, unzip your downloaded files on c:\Apache 2.4 as many tutorials asked you to do.
If you want to use httpd command variable you need to add c:\Apache 2.4\bin into your Environment Variables path.
THIS IS WHAT MOST PEOPLE FAILED TO ADDRESS :: If you are going to create Virtual Hosts you need to open c:\Apache 2.4\bin\conf\httpd.conf in your favorite text editor or IDE and look for the line or statement #Include conf/extra/httpd-vhosts.com and uncomment it, which means removing the # (pound) sign at the beginning. At the time of this writing is line 510. It should look as follow:
509 #Virtual hosts
510 Include conf/extra/httpd-vhosts.conf
You need to open you text editor as admin and edit c:\windows\system32\drivers\etc\hosts file and add all your virtual hosts as so, you get the idea:
127.0.0.1 vhOne.com #your virtual host server name
127.0.0.1 vhTwo.com #your second virutal host server name
After doing that close any terminal and open that again, and you should be able to go to your terminal, without having to cd to the Apache directory all the time, just by typing:
> httpd -k install
> httpd -v
> httpd -k restart
You should also be able to ping your virtual host from the terminal and expect the response desired:
ping vhOne.com
And you should be able to visit your virtual host URL. Keep in mind that at first most browsers won't recognized 127.0.0.1 URL to be web addresses but search string so you have to type http://vhOne.com with the http:// in the URL. After that first visit you can go back by typing the server name without the http://

Virtualhost setup on localhost displays indexof

I'm attempting to set up a virtual host on Windows vista32. I keep getting a listing of the root folder htdocs instead of the site I've setup.
Here's my virtual host entry in Apache httpd-vhosts.conf:
<VirtualHost *:80>
ServerName site-ashback.co.uk
ServerAlias www.site-ashback.co.uk
DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/site-ashback.co.uk"
DirectoryIndex index.php
</VirtualHost>
Hosts file:
127.0.0.1 site-ashback.co.uk
None of the examples I've been using have shown a few entries which are present in my hosts file thus:
127.0.0.1 localhost
::1 localhost
These two lines appear after all the commented examples and I've put my entry after these. Does this suggest I should use'localhost' instead of 127.0.0.1?
When I click my site in the listing it takes me to the site properly but the address then reads: site-ashback.co.uk/site-ashback.co.uk/. All I wanted to do is remove the word 'localhost' from local sites addresses when testing so that the path read like the real domain name when we go live.

Serving several host/domain names on local machine

I have a local PHP server on a Windows machine and the trouble I am having is that within the main root 127.0.0.1/localhost I would like to have several folders/sites that I can call on.
At present I can do localhost/siteA or localhost/siteB with no problem, but what I would like to do is to assign these locations test domain names:
EG: localhost/siteA would be sitea.dev, localhost/siteB would be siteb.dev
Not a problem as I have set up one on the host file and vhost config files.
mysites.dev which is linked to the root/localhost. So I can use it as above
mysites.dev/siteA and mysites.dev/siteB
But I would like to give each of these sites its own vhost name as mentioned above. So I have added the following:
Hosts File
127.0.0.1 mysites.dev
127.0.0.1 sitea.dev
vhost file
<VirtualHost *:80>
DocumentRoot "C:\Websites\"
ServerName mysites.dev
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:\Websites\sitea\"
ServerName sitea.dev
</VirtualHost>
The trouble is that whichever one I call they all seem to simply load the first one, or at least it seems like that, but without the vhost details they all still go to the root so it looks like the host file is directing them to 127.0.0.1 and that it the vhost file seems to be ignored.
SO what am I missing in order to hook up mty individual local sites?
SOLVED: See my answer will mark it as the correct one when i can in a few days.
Your vhosts are both trying to catch anything that comes in on port 80: <VirtualHost *:80>.
You need to specify the vhost name to point to a particular folder:
<VirtualHost sitea.dev:80>
DocumentRoot "C:\Websites\sitea\"
ServerName sitea.dev
</VirtualHost>
<VirtualHost siteb.dev:80>
DocumentRoot "C:\Websites\siteb\"
ServerName sitea.dev
</VirtualHost>
You can find more info on this in the Apache docs: http://httpd.apache.org/docs/2.2/vhosts/examples.html
Well i do feel stupid. After a long look i then noticed that in the config file it was not calling in the vhost file as i placed them in the main httpd.conf and it worked.
So i had to uncomment the following within the httpd.conf file:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf

Access virtual host from another machine over LAN

I am using Windows 7 with Wamp 2.2 server.
I have setup 2 virtual hosts: www.project1.com and www.project2.com.
I have modified the "hosts", the httpd.conf, and the httpd-vhosts.conf files, to the changes I mentioned below.
Using my browser, when I type www.project1.com or www.project2.com, I successfully get my web pages opened on the laptop that has the server installed on.
Changes in the "hosts file": I've appended the followings to the end of the file:-
127.0.0.1 localhost
127.0.0.1 www.project2.com
127.0.0.1 www.project1.com
Changes in the httpd.conf file:-
Include conf/extra/httpd-vhosts.conf
Changes in httpd-vhosts file:-
NameVirtualHost *:80
<Directory "D:/websites/">
AllowOverride All
Order Deny,Allow
Allow from all
</Directory>
<VirtualHost 127.0.0.1>
DocumentRoot "D:/websites/wamp/www/"
ServerName localhost
</VirtualHost>
<VirtualHost 127.0.0.1>
DocumentRoot "D:/websites/project1/"
ServerName www.project1.com
</VirtualHost>
<VirtualHost 127.0.0.1>
DocumentRoot "D:/websites/project2/"
ServerName www.project2.com
</VirtualHost>
Now; since I can open these web pages from a browser in PC_1 (the one with the server), how can I access these web pages from a browser in PC_2? (I mean any PC connected to PC_1 via LAN.)
In addition to danp's answer, you can access the virtual host without having to change the client machine's etc/hosts file by assigning a port to the virtual host. This is ideal if you want to access the server with a mobile or tablet device:
Edit server's httpd.conf file at:
\wamp\bin\apache\apache2.2.x\conf\httpd.conf
Search for "Listen" (around line 61). You should see the following that allows for Apache to listen for port 80:
Listen 0.0.0.0:80
Listen [::0]:80
Add the following lines to add listening for port 81 (or any port):
Listen 0.0.0.0:81
Listen [::0]:81
Edit the httpd-vhosts.conf file at:
\wamp\bin\apache\apache2.2.x\conf\extra\httpd-vhosts.conf
Change your "Virtual Host" tag to port 81:
<VirtualHost *:81>
DocumentRoot "D:/websites/project1/"
ServerName www.project1.com
</VirtualHost>
Restart Apache server.
On the client machine/tablet/mobile, on the web browser, enter the server's IP address (192.168.0.10, or whatever IP) followed by the port number in the following format:
http://192.168.0.10:81
In your virtualhost directive, change 127.0.0.1 to *:80 and as Gabriel mentioned, add an entry to the hosts file in the other machine, adding your domain to be associated with the IP of your server.
When you put an explicit IP into the directive, apache will only listen on that IP - but the wildcard will tell it bind to all IPs available to it.
<VirtualHost *:80>
DocumentRoot "D:/websites/project1/"
ServerName www.project1.com
</VirtualHost>
If your server is on 192.168.1.70 for example, then in the other machines on your lan, the hosts entry will look like:
192.168.1.70 www.project1.com
Restart apache and it should work fine.
As a note, when you are using virtualhosts, apache will use the first definition as a default for when it can't make a match between the domain passed in the HTTP request header and the sites setup in the config, which is why your default page was appearing.
You told apache to bind to all IPs with the NameVirtualHost *:80 directive, but then didn't setup a site for that external IP. Hope that helps!
There are two computer in local network.
A computer(192.168.1.70) Setup(D:\wamp\bin\apache\Apache2.2.11\conf\extra\httpd-vhosts.conf):
<VirtualHost *:80>
DocumentRoot "D:/websites/project1/"
ServerName www.project1.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "D:/websites/project2/"
ServerName www.project2.com
</VirtualHost>
B computer Setup(c:/windows/system32/drives/etc/hosts):
192.168.1.70 www.project1.com
192.168.1.70 www.project2.com
B access A,My project is working.
A couple of updated points to consider for the selected answer:
NameVirtualHost is no longer used after Apache version
2.3.11 and can be omitted.
In 2.3.11 and later, any time an IP address and port combination is
used in multiple virtual hosts, name-based virtual hosting is
automatically enabled for that address.
Because we are talking about hosting a website over LAN, let's set a
requirement* to only accept connections from IP addresses on your
local network. For example, on a common Linksys router, the default
IP assigned to each device on the network is between 192.168.1.100
to 192.168.1.255. You can allow connection from all devices on the
LAN with an IP address 192.168.1.XXX by using Require ip 192.168.1
(notice the final octet is left off the IP to allow the entire
range).
This allows you to configure access per project so that one may be
available over LAN and another is only available locally.
# This will allow all LAN connections to www.project1.com
<VirtualHost *:80>
DocumentRoot "D:/websites/project1/"
<Directory "D:/websites/project1/">
Require local
Require ip 192.168.1
</Directory>
ServerName www.project1.com
</VirtualHost>
# This will allow only the machine hosting the website to access www.project2.com
<VirtualHost *:80>
DocumentRoot "D:/websites/project2/"
<Directory "D:/websites/project2/">
Require local
</Directory>
ServerName www.project2.com
</VirtualHost>
While your site will not be served publicly without the router forwarding traffic on port 80 to your host, I believe this is considered best practice. It is especially necessary if you need to control which projects are available to devices on the LAN.
Reminder: Your host machine should be configured to use a static IP address
instead of being assigned one by your router's DHCP. Since we are
editing the hosts file of other devices to point to the server's IP,
we don't want it to change.
* I'm including this because it is common to have access restrictions on a local development server and you will need to specifically make it available to your local network.
You need to change the hosts file on the machine you're trying to view the page from.
So you need to add them to the hosts file on the other lan machine.
if you are not able to access your virtual host server in your local area network then check you have configured your firewall to allow httpd.exe file (lying in folder C:\wamp64\bin\apache\apache2.4.51\bin) to allow both incoming and outgoing connections

Apache 2.2 localhost VirtualHosts problems

I am trying to setup my dev machine with virtual hosts so I can access my development projects with a simple url, like http:// project.dev/ rather that a whole http:// 127.0.0.1/dev/path/to/root/of/project/ type of url.
Now, I'm aware there are many tutorials and questions already answered about this, but after trying many of them, I feel like I'm running in circles here.
The problem is simple: after setting up my hosts file and my virtual hosts, any attempt to access a virtual hosts irremediably displays the content of my server DocumentRoot.
my hosts file:
127.0.0.1 localhost
127.0.0.1 mydomain.dev
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
my httpd-vhosts.conf
NameVirtualHost *:80
<VirtualHost *:80>
ServerName localhost
DocumentRoot /Users/pilot/Sites/
</Virtualhost>
<VirtualHost *:80>
ServerName mydomain.dev
DocumentRoot /Users/pilot/Sites/devel/vytamin/dev/
</VirtualHost>
I precise that the httpd-vhosts.conf file IS loaded in my httpd.conf and I do get a warning if I point the virtual host document root to a wrong folder.
I made a try setting the localhost virtual host to the following, without any success.
<VirtualHost *:80>
ServerName localhost
DocumentRoot /Users/pilot/Sites/devel
</Virtualhost>
The following URLS all display the DocumentRoot of my server defined in my httpd.conf file, and seem to ignore the DocumentRoot defined in my VirtualHosts.
http:// 127.0.0.1/
http:// localhost/
http:// mydomain.dev/
I'm running Apache2.2 on Mac OSX 10.5 Leopard, but not the one shipped with mac os. I compiled my own under /usr/local/apache2 and web sharing IS disabled
I can't see anything wrong with my set up so I hope the community will!
Thanks for your help!
Ok, I found the problem!
The configuration above is perfectly fine, it was just that my apache was not restarting properly.
Neither apache_ctl graceful or apache_ctl restart was terminating all httpd processes I have running, therefore failing to reload properly the changes in my configuration files.
Once I killed the remaining processes and really restarted apache, it worked...
Solved, but I feel dumb to have found that!
Now I have a daemon situation to solve!

Resources