DocumentRoot is set to docRoot of a virtualHost? - apache2

OS: centOS 6.3 Final
I've installed the mysql and apache2(httpd) packages and changed the config in /etc/httpd/conf/httpd.conf as seen below:
<VirtualHost *:80>
DocumentRoot /var/www/html/wordpress/
ServerName www.asterix.int
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/staticSite/
ServerName www.meins.lan
ServerAlias www.deins.lan
ServerAlias www.obelix.int
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/joomla/
ServerName www.example.com
ServerAlias www2.example.com
ServerAlias www3.example.com
</VirtualHost>
All ServerName and ServerAlias are working fine and I can access the 3 pages.
But when I'm trying to access a page over the server's ip something strange (at least for me) happens. apache2 returns me the index page of the first defined virtualHost(in this example wordpress). I've tried this with all 3 virtualHosts and get the same Results.
Is this a normal behavior or what I'm doing false ?
If this is a normal behavior: Can I set the DocumentRoot exclusively for all requests to the ip ?
Thx !

This is the intended behavior. If you use the IP (let's say http://123.123.123.123/), Apache will use 123.123.123.123 as HTTP the hostname. Since there is no VirtualHost with a ServerName or ServerAlias of 123.123.123.123, the first VirtualHost is used.
So if you want a VirtualHost that listens only for http://123.123.123.123/ you can simply create a VirtualHost with:
<VirtualHost *:80>
DocumentRoot /var/www/html/my-ip-site/
ServerName 123.123.123.123
</VirtualHost>

This is a normal behaviour.
You are using a vhost-method called name-based virtual hosts. As you can imagine that means, that the "routing" of the apache is then only done by the hostname in the HTTP-request, according the ServerName and ServerAlias directives in the config.
As the request to the IP of your server - e.g. 1.2.3.4 - can not be routed into any of the defined vhost, apache takes the default virtualhost.
The default virtualhost is more or less the first virtualhost defined.
The request on the IP is accepted, because you used wildcard definitions *:80.
You can check the virtualhosts set in apache by the apache
# command apache2 -S

Related

What is the meaning of star in <VirtualHost *:80> in apache?

I want to know the use of this * and how to configure it?
* means a wildcard when you use wildcard you do bifurcation on ServerName
<VirtualHost *:80>
DocumentRoot /var/www/app1
ServerName app1.example.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/example
ServerName example.com
</VirtualHost>
Apache will route example.com to /var/www/example where as app1.example.com would be routed to /var/www/app1
VirtualHost directive allows you to configure and use multiple sites located on the same IP address. In this case, with *:80 you are creating a virtual host for every request coming on the port 80. It becomes more interesting when you start specializing and start to insert something other than * in the virtual host. An example can be, that according to the IP address with which a port is hit, you can open a different version of a web site, perhaps with different resources like: language translations, styles etc.

Named virtual local host?

I've a local apache2 site which works if I have:
<VirtualHost *:80>
ServerName localhost
...
</VirtualHost>
<VirtualHost *:443>
ServerName localhost
in .conf file. So when I point browser to localhost I get to the site. How do I assign name to the site? E.g. I want example.com drive me there.
I tried to set the ServerName and NameVirtualHost:
NameVirtualHost 172.20.30.40:80
NameVirtualHost 172.20.30.40:443
<VirtualHost 172.20.30.40:80>
ServerName www.example.com
...
<VirtualHost 172.20.30.40:443>
ServerName www.example.com
But it doesn't work.
Edit:
I'm on linux (kubuntu).
Edit 2:
I also tried to
<VirtualHost *:80>
ServerName example.com
...
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
Also set example.com as the server name in the underlying database (postgresql). And in /etc/hosts:
127.1.0.0 example.com
After restarting my app and apache I expected 127.1.0.0 to reditect me to my site. When I actually point browser to 127.1.0.0 it tries to go somewhere but couldn't make it.

Apache 2 different Virtual Hosts point to first one

Well, I'm pretty new to this, so, I'm sorry if I'm making some stupid mistake but I've been trying to make my Apache Virtual Hosts work fine for different domains. I have one VPS with CentOS 6 and Apache 2 where I'm running 2 websites with diffent domain names; they were both working fine some days ago. But After I made some changes (i dont remember them) they're acting stupidly. The problem is that the both of the domains are pointing to the first document root in the vhosts file. This is my vhosts.conf
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin support#domain.me
DocumentRoot /var/www/domainme
ServerName domain.me
ErrorLog /var/www/log/domainme.log
</VirtualHost>
<VirtualHost *:80>
ServerAdmin support#domain.com
DocumentRoot /var/www/domaincom
ServerName domain.com
ErrorLog /var/www/log/domaincom.log
</VirtualHost>
Everytime I try to access domain.com it gives me the index file of domain.me but if I put domain.com's configuration before the domain.me in vhosts file, and go to domain.me; it will give me the index of domain.com. I have two IP's for the VPS by the way.
Thank for reading, hope that its not a big issue.
Try this one
Listen *:80
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin contact#domain.tld
DocumentRoot /path/to/domain1/
ServerName domain1.com
ServerAlias domain1.com www.domain1.com
</VirtualHost>
<VirtualHost *:80>
ServerAdmin contact#domain.tld
DocumentRoot /path/to/domain2/
ServerName domain2.com
ServerAlias domain2.com www.domain2.com
</VirtualHost>

Apache2 isolate virtualhosts

I'd like to isolate each "www subdirectory" so mysite1.ext can't access to mysite2.ext files, is this possible? How can I do it?
you Must conf You virtualHost Apache for this
like This
<VirtualHost x.x.x.x:80>
ServeName www.example.com
DocumentRoot /path/to/your/virtualroot
...
... usual stuff ...
...
php_admin_value open_basedir /path/to/your/virtualroot:/some/other/path
</VirtualHost>
Last Line Is For Isolate PHP To Access Other Directory
To do what you want in Apache you would use Name Based VirtualHosts. This allows you to configure two or more separate Web server environments on one IP address. It would look something like this.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.mysite1.ext
ServerAlias domain.ext *.domain.ext
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *:80>
ServerName www.mysite2.ext
DocumentRoot /www/otherdomain
</VirtualHost>
You can read more about this configuration here This would all go in Sites.enabled or http.conf depending on your end stage configuration.

Does Apache2 support virtual hosting of subdomains?

Currently my Apache server is set up like so
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www
</VirtualHost>
The problem is that everything below /var/www is accessible from everywhere else. If I have a web page /var/www/john/bio.html, then that web page could borrow scripts/pictures from var/www/jane/
I want to set up my Apache server like so
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www
</VirtualHost>
<VirtualHost *:80>
ServerName www.john.example.com
ServerAlias john.example.com
DocumentRoot /var/www/john
</VirtualHost>
<VirtualHost *:80>
ServerName www.jane.example.com
ServerAlias jane.example.com
DocumentRoot /var/www/jane
</VirtualHost>
So all the files for user john go in the /var/www/john/ folder, and likewise for user jane. Then, with symbolic links turned off (by default), and access only provided from /var/www/user/ downwards (again by default), I don't have to worry about john's web page including scripts/images from jane's web page.
Using local measures only (/etc/hosts instead of a DNS) I found that this can indeed work.
First, change your /etc/hosts file to have a mapping of your desired website name(s) (www.example.com), and target IP address (192.168.1.1). I used my local IP address.
IPAddress Hostname Alias
----------- -------------------------- ------------------
192.168.1.1 www.example.com example.com
192.168.1.1 www.john.example.com john.example.com
192.168.1.1 www.jane.example.com jane.example.com
Your web browser will check your /etc/hosts file before looking at the world wide web.
Next go through all your Apache config files (httpd.conf, apache2.conf, ports.conf, conf.d/*) and make sure in exactly one file the command NameVirtualHost *:80 is issued (it doesn't have to be port :80 but if it is issued more than once, you will get this problem). Mine was issued in /etc/apache2/ports.conf, so put yours there if you have to. Finally, update your Apache configuration file (mine was at /etc/apache2/sites-available/default) like so.
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www
</VirtualHost>
<VirtualHost *:80>
ServerName www.john.example.com
ServerAlias john.example.com
DocumentRoot /var/www/john
</VirtualHost>
<VirtualHost *:80>
ServerName www.jane.example.com
ServerAlias jane.example.com
DocumentRoot /var/www/jane
</VirtualHost>
As a final step, you may need to add the websites to Apache by issuing the below commands (this step is not necessary, if you give all websites into sites-available/default and not into separate files for individual websites).
# a2ensite www.example.com
# a2ensite www.john.example.com
# a2ensite www.jane.example.com
After doing this, john.example.com will go to /var/www/john. That directory will then act as the root directory, and john will no longer have access to www, and, therefore, have no access to /var/www/jane.
Likewise, after doing this, jane.example.com will go to /var/www/jane. That directory will then act as the root directory, and jane will no longer have access to www, and, therefore, have no access to /var/www/john.
With symbolic links turned off --by default-- neither directories will be able to access each other

Resources