VIrtualHost: Different hosts point to the same location - apache

I am trying to set virtual hosts for two Zend Framework applications. I started by changing the system32 hosts file.
It contains the following lines now:
127.0.0.1 localhost
# ::1 localhost
127.0.0.1 quickstart
After that, I proceeded with changing the httpd-vhosts.conf file. Its current content:
<VirtualHost *:80>
ServerAdmin postmaster#dummy-host2.localhost
DocumentRoot "G:\workspace\Andrew\ProjManer\public"
ServerName localhost
ServerAlias localhost
ErrorLog "logs/localhost-error.log"
CustomLog "logs/localhost-access.log" combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin postmaster#dummy-host2.localhost
DocumentRoot "G:\workspace\Andrew\quickstart\public"
ServerName quickstart
ServerAlias quickstart
ErrorLog "logs/quickstart-error.log"
CustomLog "logs/quickstart-access.log" combined
</VirtualHost>
If I don't add the virtual host with localhost first, I get a "Access forbidden 403 Error message".
The problem now is that both point to the same location, the localhost. How am I supposed to get the second virtual host working? I used the flushdns also.

You don't need the ServerAlias in them unless you want say quickstart2 to go to quickstart. In that case you will do ServerAlias quickstart2. You get access forbidden because your document root in your httpd.conf doesn't have an index.php or that virtualhost doesn't have an index.php and you have -Indexes set
Other than that the virtualhost and hosts file look fine. Try restarting your browser and restarting apache.

Related

Virtual Hosts on WAMP causing 403 forbidden on localhost... other aliases still work

I just realized that nothing else on WAMP is accessible unless it's under the virtual host alias. For example: if I name my vHost 'mysite.dev', I can only access mysite.dev and everything else gives a 403 forbidden error. If I add a vHost called anothersite.dev in addition to mysite.dev, only those sites can be accessed. The only thing under localhost that I can access is PHPMyAdmin. I have uncommented the line that includes vHosts.conf in the Apache httpd.conf file. This problem does not happen until I modify the vHosts.conf file. Here is the config for the other files:
vHosts.conf:
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "c:/Apache24/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error.log"
CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host2.example.com
DocumentRoot "c:/Apache24/docs/dummy-host2.example.com"
ServerName dummy-host2.example.com
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/dummy-host2.example.com-access.log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host2.example.com
DocumentRoot "W:/wamp/www/mysite"
ServerName mysite.dev
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/dummy-host2.example.com-access.log" common
</VirtualHost>
Windows hosts file:
127.0.0.1 localhost
127.0.0.1 mysite.dev
Ok first, the first 2 VHOST definitions in the httpd-vhost.conf file (vhost and vhost2) are examples, supplied by Apache, to help you get started and of course point to folders that do not exist so and should be removed.
Second When you create a Virtual Host you should include the access privilages for the VHOST in a <Directory....> group.
Third, you should always create a VHOST for localhost as once VHOSTs are created Apache ignores the localhost definition in httpd.conf
So your httpd-vhost.conf file should look like this
# Should be the first VHOST definition so that it is the default virtual host
# Also access rights should remain restricted to the local PC and the local network
# So that any random ip address attack will recieve an error code and not gain access
<VirtualHost *:80>
ServerAdmin webmaster#homemail.net
DocumentRoot "W:/wamp/www"
ServerName localhost
<Directory "W:/wamp/www">
AllowOverride All
Require local
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "W:/wamp/www/mysite"
ServerName mysite.dev
ErrorLog "logs/mysite.dev-error.log"
CustomLog "logs/mysite.dev-access.log" common
<Directory "W:/wamp/www/mysite">
Options Indexes FollowSymLinks
AllowOverride All
Require local
</Directory>
</VirtualHost>
Once you have done this, you now need to edit your c:\windows\system32\drivers\etc\hosts file to look like this. You need to have admin privilages to edit this file, also some anti virus suites also protect this file, so you may need to stop that protection temporarily in order to amend this file.
127.0.0.1 localhost
127.0.0.1 mysite.dev
::1 localhost
::1 mysite.dev
Then restart the dnscache to pick up these changes, from a command line also started with admin privilages.
net stop dnscache
net start dnscache
This post may help you understand more about Virtual Hosts

Apache Virtual Host IP Works but not ServerName

I am using Wamp.
My Apache Server is running on port 90.
I Tried adding a virtual host by doing the following steps:
1)Removed the comment from the line Include conf/extra/httpd-vhosts.conf line in my Apache httpd.conf file.
2)I Added the following code to my C:\wamp\bin\apache\Apache2.4.4\conf\extra\httpd-vhosts.conf
<VirtualHost *:90>
ServerAdmin admin#laravel.dev
DocumentRoot "c:/wamp/www/laravel/public"
ServerName laravel.dev
ServerAlias www.laravel.dev
ErrorLog "logs/laravel.dev.log"
CustomLog "logs/dummy-laravel.dev.log" common
</VirtualHost>
<VirtualHost *:90>
DocumentRoot "c:/wamp/www/"
</VirtualHost>
3)I made changes to my windows hosts file.This is the content of my hosts file
127.0.0.1:90 localhost
127.0.0.1:90 laravel.dev
127.0.0.1:90 www.laravel.dev
Now localhost:90 will Redirect to my C:/wamp/www and 127.0.0.1:90 will Redirect to my C:/wamp/www/laravel/public but both laravel.dev and www.laravel.dev are not working
I get the message Oops! Google Chrome could not find laravel.dev in my chrome.
Any help would be appreciated :)

Apache IP Virtual Hosts

Server has two IPs, fresh centos min install. Apache is working, both ips load Apache test page. both www.domain.com and domain.com resolve to second IP.
I'd like for the first IP (192.168.0.1) to load Apache test page, this is working fine
I want the second IP (192.168.0.2) to load a website in /home/site/www
Currently when we goto domain.com or www.domain.com or 2nd IP it loads apache test page instead of the site, here's our config. Also I have the IPs listed as 192 instead of the real ips. What am I missing? Why isn't 192.168.0.2 loading /home/site/www instead of the Apache test page?
ServerRoot "/etc/httpd"
Listen 80
ServerName 192.168.0.1:80
DocumentRoot "/var/www/html"
<Directory "/var/www/html">
NameVirtualHost 192.168.0.2:80
<VirtualHost 192.168.0.2:80>
DocumentRoot /home/site/www
ServerName mydomain.com
ServerAlias *.mydomain.com
ErrorLog logs/mydomain.com-error_log
CustomLog logs/mydomain.com-access_log common
</VirtualHost>
Update
The Fix
chcon -R --reference=/var/www /home/site/www
SELinux needed the correct permissions set on it, using the reference it copies the same permissions to my new folder
Try this:
ServerRoot "/etc/httpd"
Listen 80
ServerName 192.168.0.1:80
NameVirtualHost 192.168.0.1:80
NameVirtualHost 192.168.0.2:80
<VirtualHost 192.168.0.1:80>
DocumentRoot /var/www/html
ServerName mydomain.com #change accordingly
ServerAlias *.mydomain.com
ErrorLog logs/mydomain.com-error_log
CustomLog logs/mydomain.com-access_log common
</VirtualHost>
<VirtualHost 192.168.0.2:80>
DocumentRoot /home/site/www
ServerName mydomain2.com
ServerAlias *.mydomain2.com
ErrorLog logs/mydomain2.com-error_log
CustomLog logs/mydomain2.com-access_log common
</VirtualHost>
Don't forget to apply the changes on apache.
service httpd reload or similar command.
Also, make sure the directory /var/www/html has, at least, reading permissions for the apache user.
You are missing the NameVirtualHost directive.
NameVirtualHost 192.168.0.2:80
I would also highly suggest putting in Directory directives in as well.

Strange problem with Apache Virtual Hosts

I have recently installed Apache 1.3.41 on a Windows Vista machine. I have not changed the default settings in httpd.conf aprart from trying to setup virtual hosts, as follows:
Added some host names in the hosts file:
127.0.0.1 localhost
#::1 localhost
127.0.0.1 mysite
127.0.0.1 mydomain
I made the following folders in C:/Users/Moukasp/
C:/Users/Moukasp/Apache
C:/Users/Moukasp/django/mysite
Then added some simple html pages in each of those folder and the c:/Users/Moukasp/pictures folder and, finally, I added the following settings at the end of httpd.conf:
NameVirtualHost 127.0.0.1
<VirtualHost localhost>
# ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "C:/Users/Moukasp/Apache"
ServerName localhost
Alias "/pics" "c:/users/moukasp/pictures"
Alias "/ap" "C:/Program Files/Apache Group/Apache/htdocs"
Alias "/dj" "C:/Users/Moukasp/django/mysite"
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
<VirtualHost mysite>
# ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "C:/Users/Moukasp/django/mysite"
ServerName mysite
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
<VirtualHost mydomain>
# ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "C:/Users/Moukasp/django/mysite"
ServerName mydomain
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
So, urls
http://localhost
http://localhost/pics
http://localhost/ap
http://localhost/dj
http://mydomain
work fine. But I there's no response from http://mysite which, as can be seen from the settings, serves from the same folder as http://mydomain. I have tried various names but no response. The hosts file is being read every time I start the Apache server. I even removed the mydomain server from the hosts file and the httpd.conf lest there's a limitation in the number of virtual hosts, but no luck.
Any help would be greatly appreciated, els I go mad! It is true, I have being trying this after complete failure to make Apache work with mod_python for django! I just hoped that a step at a time would lead to some success!
Try:
NameVirtualHost 127.0.0.1:80
In your httpd.conf. My config file includes a comment stating a port-specifier should be used.
The VirtualHost and NameVirtualHost directives should have IP addresses and ports:
NameVirtualHost 127.0.0.1:80
<VirtualHost 127.0.0.1:80>
# ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "C:/Users/Moukasp/django/mysite"
ServerName mysite
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
Tried NameVirtualHost 127.0.0.1:80 but it doesn't make any difference! The only servers that work are localhost and mydomain.
I wonder whether I need to do something more than just declare the server name of each virtual host in the hosts file.
Try adding a port declaration to the hosts. For example:
<VirtualHost mydomain:80>
DocumentRoot "C:/Users/Moukasp/django/mysite"
ServerName mydomain
</VirtualHost>

Why does http://localhost redirect to my default virtual host once I setup virtual hosts in Apache?

This is probably an easy question, but I want to understand better how Apache works with virtual hosts. I am setting up virtual hosts because I work on multiple websites at once and I don't want to use subdirectories. I was pretty much using the default Apache httpd.conf file with the DocumentRoot pointing to something like "/www". I uncommented the virtual hosts include and added the following:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName site1.dev
DocumentRoot /www/site1
</VirtualHost>
<VirtualHost *:80>
ServerName site2.dev
DocumentRoot /www/site2
</VirtualHost>
Now when I go to http://localhost I get the default page for site1.
I'm sure there is a reason why this makes sense, but I don't quite understand it. I would've thought that only requests that were explicitly to http://site1.test would get routed through that directive and it wouldn't just become the default. Can someone explain why it becomes the default.
http://httpd.apache.org/docs/1.3/vhosts/name-based.html
(Should be true for 2.x also)
"If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.
As a consequence, the first listed virtual host is the default virtual host. The DocumentRoot from the main server will never be used when an IP address matches the NameVirtualHost directive. If you would like to have a special configuration for requests that do not match any particular virtual host, simply put that configuration in a container and list it first in the configuration file."
answer 1 is correct
and i'd add with namevirtualhosts as the first entry
essentially matches any not-named elsewhere virtualhost
it should ONLY be used to catch unintentional mal-formed and broken traffic
ie a machene with one ip called john.domain.com running www.domain.com and www.domain2.com as valid webservers on ip www.xxx.yyy.zzz might have an optimal config like thus
<VirtualHost *:80>
DocumentRoot /var/webserver/static-sites/unknown/
# a directory readable by apache with only a robots.txt denying everything
ServerName bogus
ErrorDocument 404 "/errordocuments/unknown-name.html"
#custom 404 describing how/what they might have done wrong try pointing a browser {with a hosts file at http://bogus/ on 193.120.238.109 to see mine#
ErrorLog /var/log/httpd/unknown-error.log
CustomLog /var/log/httpd/unknown-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/static-sites/unknown/
# a possibly different directory readable by apache with only a robots.txt denying everything
ServerName www.xxx.yyy.zzz
ServerAlias john.domain.com
ErrorDocument 404 "/errordocuments/ip-name.html"
ErrorDocument 403 "/errordocuments/ip-name.html"
#custom 404 telling them as a likely hacker/bot you wish to have nothing to do with them see mine at http://193.120.238.109/
ErrorLog /var/log/httpd/ip-error.log
CustomLog /var/log/httpd/ip-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain.com
RedirectPermanent / http://www.domain.com/
ErrorLog logs/www.domain.com-error.log
CustomLog logs/www.domain.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/ftpusers/domain
ServerName www.domain.com
ServerPath /domain
ErrorLog logs/www.domain.com-error.log
CustomLog logs/www.domain.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain2.com
RedirectPermanent / http://www.domain2.com/
ErrorLog logs/www.domain2.com-error.log
CustomLog logs/www.domain2.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/ftpusers/domain2
ServerName www.domain2.com
ServerPath /domain2
ErrorLog logs/www.domain2.com-error.log
CustomLog logs/www.domain2.com-access.log combined
</VirtualHost>
Confirming that for Apache 2.x, the first virtual host (with the same port number) will be used if a matching virtual host is not found.
http://httpd.apache.org/docs/2.2/vhosts/details.html
"If no matching vhost could be found the request is served from the first vhost with a matching port number that is on the list for the IP to which the client connected"
You can always add this code below, put it right below NameVirtualHost *:80 so that your default document root is served by default if no other virtual hosts found.
<VirtualHost *:80>
ServerName localhost
DocumentRoot /my/default/document/root
</VirtualHost>
Simply put this code at top in httpd-vhosts.conf
<VirtualHost localhost:80>
ServerName localhost
DocumentRoot d:/xampp/htdocs
<Directory "d:/xampp/htdocs/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
One way to do this is:
In your VirtualHosts configuration, enter the specific local site name you want to enable instead of using a wildcard:
<VirtualHost site1.dev:80> instead of <VirtualHost *:80>
Switch off NameVirtualHost *:80 which can be done by commenting it out in your vhosts.conf file
In your /etc/hosts file mention both aliases for the loopback IP:
127.0.0.1 localhost site1.dev
That's it. You should see that localhost goes to the default DocumentRoot as usual and the site1.dev goes to the site you've setup as virtual host.