NameVirtualHost Works on IP but not for ServerName? - apache

I am a beginner to Apache and have been trying to create virtual hosts but I come across a very odd issue. When the site is requested using the IP defined in the configuration, the virtual host works perfectly but when the name of the server is used it does not work.
Here is the configuration file which is an included file:
Listen 80
NameVirtualHost 192.168.1.5
<VirtualHost 192.168.1.5>
ServerName host1.localhost
DocumentRoot "C:/Program Files (x86)/Apache Group/Apache2/virtualHost1"
</VirtualHost>
In the browser I go to 192.168.1.5 and the index file for the Virtual Host appears but when I go to host1.localhost it doesn't work.
Any help is very appreciated, good day to all.

First you should try to see if your computer resolve host1.localhost hostname.
May be just try to ping it in your console: ping host1.localhost
If your computer is unable to resolve host1.localhost, as I suppose, then try to add it in your hosts file.
Hint: you'll find hosts file in C:\Windows\System32\drivers\etc directory

What do you mean by "it doesn't work".
1)Check your DNS settings, do you have record for host1.localhost pointing to 192.168.1.5?
2)Try using ServerAlias also
3)Check if there is another declaration for hosts1.localhost (by another ServerAlias)
I guess the most likely reason is 1)

Related

Accessing Virtual Hosts Across a Local Network

I've 2 machine on my local network. On my first machine A, i've creat a vhost mysite.lan and i want access to this vhost with my machine B.
If i don't edit my host file, i do not access.
Do you know if i can access on my vhost without edit my host file ?
My vhost :
<VirtualHost *:80>
ServerName wordpress.lan
ServerAlias www.wordpress.lan
DocumentRoot /var/www/html/www.wordpress.lan/webroot/
ErrorLog /var/www/html/www.wordpress.lan/logs/error.log
CustomLog /var/www/html/www.wordpress.lan/logs/access.log combined
</VirtualHost>
Thank you !
You'll need something on your network to read these requests and forward them to the correct address - in the absence of anything else an entry in the hosts file will do this.
Your router contains DNS servers, either entered manually or populated by your internet service provider. By default, these servers provide lookup information for DNS queries. You can run a DNS server locally, but it's a lot of work to achieve what you're looking for.
See this link for info on doing this on Linux (I'm not sure what OS you're on, but I assume it's either Mac or Linux based on format of your vhost entry):
https://superuser.com/questions/45789/running-dns-locally-for-home-network

Enabling virtual hosts in Apache makes every host forbidden (403)

I am on Windows 8 and just now installed Wamp 2.5 (Apache 2.4.9) at C:/wamp to test my code locally. I have my source code repository at D:/workpace/project1 which I manage using GIT (GIT is one of the reasons I don't want to disturb its location).
After Installing Wamp, I just went on to enable virtual hosts by uncommenting the following line in httpd.conf.
#Include conf/extra/httpd-vhosts.conf
Then I headed to conf/extra/httpd-vhosts.conf created its backup and created the virtual host.
<VirtualHost *:80>
ServerAdmin admin#example.com
DocumentRoot "D:/workspace/project1/"
ServerName project1
ServerAlias p1
ErrorLog "logs/project1(local)-error.log"
CustomLog "logs/project1(local)-access.log" common
<Directory "D:/workspace/project1/">
Require all granted
</Directory>
</VirtualHost>
Also edited the hosts file and added the necessary lines.
127.0.0.1 project1
127.0.0.1 p1
The problem is that I get 403 Forbidden error not just when I access project1/ and p1/, but also when I access localhost/.
When I disable httpd-vhosts.conf in httpd.conf by commenting out,
Include conf/extra/httpd-vhosts.conf
I am able to access localhost/ but as expected, project1/ and p1/ too resolve to localhost/.
Surprisingly, when I re-enable httpd-vhosts.conf in httpd.conf and restore the backup of original(unchanged) httpd-vhosts.conf, I still get 403 (Forbidden) even on localhost/. But again when I disable httpd-vhosts.conf, localhost/ comes to normal.
It is furthermore surprising to note that If I change DocumentRoot to a dummy folder within c:/wamp/www, the story is the same. So this is not just happening when DocumentRoot is outside c:/wamp/www.
I feel that I have something extra to enable because there is no chance I have messed up anything as this is a fresh installation and I have made no changes except the ones above.
What may be the reason Apache is not getting along with virtual hosts?
There are a few things that you have not taken into account.
When you create Virtual Hosts Apache ignores the host defined in httpd.conf so you also need to create a Virtual Host for localhost. This explains why you cannot get to localhost when you have activated Virtual Hosts.
Apache is IPV4 and IPV6 aware, so when you create your domain names in the HOSTS file you need to do so for both the IPV4 and IPV6 stacks. So you would need to have a HOSTS file like this :-
#IPV4 Stack
127.0.0.1 localhost
127.0.0.1 project1
#IPV6 Stack
::1 localhost
::1 project1
The browser makes some arbitrary decision on whether to use the IPV4 stack or the IPV6 stack. Wish I understood why and for what reason it makes this decision better, but basically when you enter a domainname, the browser, or the networking stack, picks IPv4 or IPv6 for your first contact to a domain and then sticks to that decision, but it could use either.
Here is a post that should help you create Virtual Hosts

How to setup name based virtual host using yast in open suse?

Im trying to setup virtual host under open suse. Im in Yast->Http server, when i check "Determine Request Server by HTTP Headers" I got error message To use name-based virtual you must designate the ip adress on the server(...)
I did some googling and according to what i found i uncommmented the NameVirtualHost *:80 line in /etc/apache2/listen.conf and restarted apache but it id not help. How can i setup it propely(maybe I should do it through yast without manually editing conf files but i can't find that option)
The problem I encountered was occurring because I have NameVirtualHost *:80 in listen.conf and example.com:80 in ServerName but they must be exactly the same.
PS it's still not working as expected when I set NameVirtualHost example.com:80 and then try to add server with ServerName example.com:80 I still get mentioned error message. Of course I got example.com in my hosts file pointing to 127.0.0.1
Quite outdated question but in case someone encounters it:
1) I added
NameVirtualHost my_virtual_hostname_here.tld:80
before <VirtualHost ... section
2) changed
<VirtualHost *:80> to <VirtualHost my_virtual_hostname_here.tld>
That did the trick for me. Of course as mentioned #Lord_JABA don't forget to add 127.0.0.1 my_virtual_hostname_here.tld to /etc/hosts

XAMPP apache server not using the ports I desire, not certain why

So, I've been setting up this HTTP server for a school project that has to be able to maintain multiple domains through virtual hosts. Using XAMPP I have my server set up, I thought I'd done it properly but perhaps not.
I had heaps of issues with not being able to listen to a port, to solve this I used (in httpd.conf under apache/conf/)
Listen 0.0.0.0:8080
ServerName localhost:8080
If I used 80/81/8080/321/any combination that did not have the prefix 0.0.0.0 it told me the port could not be listened or whatever (sorry, a little rusty with my syntax).
I set up my first virtual host up like so (under apache/conf/extra/httpd-vhosts.conf):
NameVirtualHost *:8080
<VirtualHost *:8080>
DocumentRoot "H:/xampp2/xampp/htdocs"
ServerName localhost:8080
<Directory "H:/xampp2/xampp/htdocs">
Option Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
I thought this seemed all good and well and I don't believe I've modified anything else. To me, I would expect that this means my Apache server would use port 8080 for the server? However, when I start Apache in XAMPP it states quite clearly
Apache started [Port 80]
Why would this be port 80?
In conjunction, localhost:8080 doesn't navigate me to the documents, but localhost:80 or localhost do. I presume that this just means it's still on port 80. I've tried resetting my computer and XAMPP.
If you need any more information, please let me know. I tried following guides such as this one: http://ailoo.net/2008/07/set-up-multiple-virtual-hosts-on-xampp-for-windows/
Cheers.
Oh, I also edited my Windows HOSTS file; it looks like so:
# localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
::1 localhost
127.0.0.1 localhost:8080
Edit: New error... Now getting this when I init, doesn't seem to be affecting it though:
ERROR: Status Check Failure [3]
This program must be run from your XAMPP root directory.
It is being run from teh root directory as far as I can tell.
Ok Im not advanced in this area either. However after much playing around with XAMPP and following a few different guides I got mine working.
The Host under drivers seems abit weird. I declared mine as:
127.0.0.1:8080 test.localhost.com
127.0.0.1:8080 test2.localhost.com
Back up you Xampp, then extract a clean install to C: - Just because that Status check failure sounds like bad news.
!Remember to run setup_xampp.bat from the xampp directory and install the apache service!
Then under you httpd.conf add:
ServerRoot C:\xampp\apache
Listen 7070
Listen 8080
Listen 9090
Apache might not like that at first but meh.
The ServerRoot may already be declared above the Listen spot so just double check.
After that head to C:\xampp\apache\conf\extra.
Edit the httpd-vhosts.conf.
Under the section Name-based virtual hosting add:
NameVirtualHost *:7070
NameVirtualHost *:8080
NameVirtualHost *:9090
There will be a couple of examples of virtual hosts below that. Add:
<VirtualHost *:7070>
DocumentRoot "C:/xampp/www/YOURWEBSITEDIR1"
ServerName test.localhost.com
<Directory "C://xampp/www/YOURWEBSITEDIR1">
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:9090>
DocumentRoot "C:/xampp/www/YOURWEBSITEDIR2"
ServerName test2.localhost.com
<Directory "C://xampp/www/YOURWEBSITEDIR2">
Allow from all
</Directory>
</VirtualHost>
Not the double slash in the Directory tag. Now ensure there is an index.html under each of those directories. Restart the apache (Stop, start).
I cant navigate to the test.localhost.com URLs, probably due to the fact they are terribly wrong in the HOSTs config. Though I can connect to 127.0.0.1:7070, 127.0.0.1:9090 and access the webpages from there.
Also 127.0.0.1:8080 connects me to the xampp welcome panel that localhost used to connect you too.
Hope this helps and good luck!
I'm pretty sure that saying 'listening on port 80' is hard-coded into XAMPP, regardless of the port you use. Thus, your code should work fine.
In my .conf file, it just says "listen 80" instead of Listen 0.0.0.0:8080
Cheers.

WAMP Server takes another DocumentRoot than I prescribed

I have WAMP Server installed, which works fine—normally.
I have changed some settings in httpd.conf, including the DocumentRoot directive. But when I start the server, and open localhost through a web browser, it gives a 403 Forbidden error. When I open the error log, I see the cause:
Warning: DocumentRoot [C:/Apache2/docs/dummy-host.example.com] does not exist
But wait... In the httpd.conf file, the DocumentRoot directive is properly set to A:/website/www/.
How is that possible?
Sounds like your editing the wrong DocumentRoot.
I use another WAMP (Wamp-Developer) but I'm guessing that your httpd.conf's DocumentRoot is the server's (non-VirtualHost) DocumentRoot.
You're looking to edit the website's (localhost's) VirtualHost block, which is either at the end of httpd.conf or in another file (that's included by httpd.conf).
Run Apache via the command line like this:
httpd -S
It will show you where the VirtualHost is.
You'll also need to make sure that your custom DocumentRoot has all the permissions set for access.
<Directory /xxxx>
order allow,deny
allow from all
...
Probably you've uncommented this line in httpd.conf:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
httpd-vhosts.conf is the file where you can put your VirtualHost directives for having them separate from the rest, but it contains a sample VirtualHost (exactly your dummy-host.example) that is causing the error.
So you have to replace the sample host with your one(s).
But you can also comment the include line and write the VirtualHost directly in your httpd.conf.
It's important to read the error log properly:
Warning: DocumentRoot [C:/Apache2/docs/dummy-host.example.com] does not exist
So we have to find a directive in some file that is trying to define a virtual host. In my case (W7pro, WAMP) I found it in the following location:
C:\wamp\bin\apache\Apache2.2.21\conf\extra\httpd-vhosts.conf
Just edit the file and delete the corresponding virtual host directives.
try 127.0.0.1, if it works. go to C:\Windows\System32\drivers\etc and add (or uncomment) the following line:
127.0.0.1 localhost
For me it was MSSQL reporting service:
SQL Server Reporting Services (MSSQLSERVER)
disabled it and it works.