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>
Related
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
I want to configure a VirtualHost for subdomain.mydomain.tld with own DocumentRoot.
and another VitualHost with own DocumentRoot catching all others.
I tried
<VirtualHost *:80>
ServerAlias subdomain.mydomain.tld
...
</VirtualHost >
<VirtualHost *:80>
ServerAlias *.mydomain.tld mydomain.tld
...
</VirtualHost >
and it diddn't work properly...
any other suggestions?
If there is no way to fix this, i would like to map a virtulahost to a subfolder of mydomain.tld and let the other VirtualHost handle the rest, but no clue how to do this.
Thanks in advance.
If you don't specify a ServerName apache can get a bit confused about which vhost should be used to serve a request.
Try poping in ServerName in as below
<VirtualHost *:80>
ServerName subdomain.mydomain.tld
...
</VirtualHost >
<VirtualHost *:80>
ServerName mydomain.tld
ServerAlias *.mydomain.tld
...
</VirtualHost >
I'm trying to set up some basic virtualhosts in Apache2, within the domain
nosoftpatents.com (invented). I'm supposed to have two subdomains, friendly
and previous, each of them accessible from two ports, 80 and 8800, that are to
be linked to the stable and devel subdirectories of each domain. So far, I
have this:
<VirtualHost *:80>
ServerName friendly.nosoftpatents.com
DocumentRoot /var/www/nopatents/friendly/stable
</VirtualHost>
<VirtualHost *:80>
ServerName previous.nosoftpatents.com
DocumentRoot /var/www/nopatents/previous/stable
</VirtualHost>
<VirtualHost *:8800>
ServerName friendly.nosoftpatents.com
DocumentRoot /var/www/nopatents/friendly/devel
</VirtualHost>
<VirtualHost *:8800>
ServerName previous.nosoftpatents.com
DocumentRoot /var/www/nopatents/previous/devel
</VirtualHost>
Is there a more elegant way of doing it? Some way of grouping the hosts from the
same port?
I am trying to route requests to different docroots depending on the HOST name (not domain name):
<VirtualHost 123.123.12.12:80>
SuexecUserGroup "#521" "#521"
ServerName forum.somesite.net
DocumentRoot /home/somesite/public_html/forums
</VirtualHost>
<VirtualHost 123.123.12.12:80>
SuexecUserGroup "#521" "#521"
ServerName www.somesite.net
DocumentRoot /home/somesite/public_html
</VirtualHost>
It is not working though. Apache seems to go to the first entry regardless of what is in the hostname. What am I doing wrong?
Try changing the second Virtual host to this
<VirtualHost 123.123.12.12:80>
SuexecUserGroup "#521" "#521"
ServerName somesite.net
ServerAlias www.somesite.net
DocumentRoot /home/somesite/public_html
</VirtualHost>
I have a virtual host setup on Apache 2 like so (simplified):
<VirtualHost *>
ServerName domain.com
ServerAlias *.domain.com
DocumentRoot /var/www/domain.com/html
</VirtualHost>
<VirtualHost sub1.domain.com>
ServerName sub1.domain.com
DocumentRoot /var/www/sub1.domain.com/html
</VirtualHost>
<VirtualHost sub2.domain.com>
ServerName sub2.domain.com
DocumentRoot /var/www/sub2.domain.com/html
</VirtualHost>
The outcome I'm looking for is everything that isn't sub2.domain.com or sub1.domain.com should go to the domain.com. They all have to be on the same default port and they all share the same IP address.
What actually happens is that sub2 goes to sub2 as expected, and everything else goes to sub1, i.e. the wildcard definition is ignored.
How can I fix this up?
Thanks!
Change the <VirtualHost sub1.domain.com> to <VirtualHost *> and
<VirtualHost sub2.domain.com> to <VirtualHost *> also. First one is the default one anyway.