# hosting multi websites, all sites use the same doc root

## ImAmMe

Apache serving wrong document root, i can not figure why for the life of me.   I have done some googling found a few tutorials on subject and i dont understand why i can not change the document root, its allways the same.   I thought it was using the default website configuration, i dont believe so anymore.

fun facts

- im using one machine, one ip

- I can ping both foo, and altsite.  so i can resolve the addresses ok.   

- For debugging i have all files and directory set to 755, under /var/www/*

- right now im not getting any complaints from Apache on reload/restart

emerge -pv apache 

```
[ebuild   R    ] www-servers/apache-2.2.22-r1  USE="ssl threads -debug -doc -ldap (-selinux) -static -suexec" APACHE2_MODULES="actions alias auth_basic authn_alias authn_anon authn_dbm authn_default authn_file authz_dbm authz_default authz_groupfile authz_host authz_owner authz_user autoindex cache cgi cgid dav dav_fs dav_lock deflate dir disk_cache env expires ext_filter file_cache filter headers include info log_config logio mem_cache mime mime_magic negotiation rewrite setenvif speling status unique_id userdir usertrack vhost_alias -asis -auth_digest -authn_dbd -cern_meta -charset_lite -dbd -dumpio -ident -imagemap -log_forensic -proxy -proxy_ajp -proxy_balancer -proxy_connect -proxy_ftp -proxy_http -proxy_scgi -reqtimeout -substitute -version" APACHE2_MPMS="-event -itk -peruser -prefork -worker" 0 kB

```

/etc/apache2/httpd.conf

```

....

Include /etc/apache2/modules.d/*.conf

NameVirtualHost *:80

Include /etc/apache2/vhosts.d/*.conf

```

/etc/apache2/vhosts.d/00_default_vhost.conf

```

<IfDefine DEFAULT_VHOST>

Listen 80

# Use name-based virtual hosting.

#NameVirtualHost *:80   ### put this in /etc/apache2/httpd.conf

ServerName foo

 

<VirtualHost *:*>

        ServerName localhost

        Include /etc/apache2/vhosts.d/default_vhost.include

        # Redirect http -> https, rewrite

        RewriteEngine On

        RewriteCond %{HTTPS} !on

        RewriteRule ^/(.*) https://%{SERVER_NAME}%{REQUEST_URI} [R]

        <IfModule mpm_peruser_module>

                ServerEnvironment apache apache

        </IfModule>

</VirtualHost>

</IfDefine>

```

/etc/apache2/vhosts.d/foo.conf

```
<VirtualHost *:80>

        ServerName foo

        DocumentRoot "/var/www/localhost/htdocs"

        <Directory "/var/www/localhost/htdocs" >

                Options Indexes

                AllowOverride All

                Order allow,deny

                Allow from all

        </Directory>

</VirtualHost>
```

/etc/apache2/vhosts.d/altsite.conf

```
<VirtualHost *:80>

        ServerName dev.altsite.com 

        DocumentRoot "/var/www/altsite/htdocs"

        <Directory "/var/www/altsite/htdocs" >

                Options Indexes

                AllowOverride All

                Order allow,deny

                Allow from all

        </Directory>

</VirtualHost>
```

Any suggestions?

thanks for taking a gander

----------

## Veldrin

your virtual hosts all listen on *:80, which matches every domain. you could this a some kind of last resort vhosts for any unmatched domains, but if that wildcard vhosts is put first, it will always match. 

use something like

```
<VirtualHost foo:80> 

        ServerName foo 

        DocumentRoot "/var/www/localhost/htdocs" 

        <Directory "/var/www/localhost/htdocs" > 

                Options Indexes 

                AllowOverride All 

                Order allow,deny 

                Allow from all 

        </Directory> 

</VirtualHost>
```

any additional names (aliases, ip address or whatever it should listen on) can be introduced with ServerAlias.

HTH

V.

----------

## ImAmMe

thank u for the suggestion...

but still im getting the wrong directory for the altsite

I tried changing both VirtualHost line at separate times and still no luck. 

I played with the default_vhost.include file changing the DocumentRoot and Directory.   And doing that corrected my altsite root, but my foo site not right now.   So i guess im having an issue with my default website catching all the request...right?

----------

## Veldrin

try disabling the DEFAULT_VHOST - I still have a feeling that it is interfering. from the little information i have, the default_vhost will be read before the other vhosts, and therefore overriding them.

V.

----------

## ImAmMe

ok...

- removed the DEFAULT_HOST flag service config file

- moved the Listen, ServerName to /etc/apache/httpd.conf

restarted the service.   Still no change.

Im more than happy to share any more information, i dont know what share to make this easy for u

----------

## Veldrin

how are you accessing the pages? what browser? what url? 

are you using dns resolution? or are the hosts added /etc/hosts?

is the cache disabled? did you clean it between the tries?

just some random thoughts. - i am especially curious about the last ones.

V.

----------

## ImAmMe

im using several browers  

win7 box:  firefox 14.0.1, Chrome 21.0.1180.79

gentoo box: elinks 0.11.7

firefox and chrome are the primary two im testing with.    Now i havent been very religious about clear dns between goes.   But it seems to find 'foo' and dev.altsite.com most every time.  but just consistently displays the wrong document root for the dev.altsite.com.   I have tried from a three different boxes, one of which is the local box.   Get the same result every time

the machine running the apache server, is running dnsmasq-2.57.  

my /etc/hosts

```
10.11.11.8 foo

10.11.11.8 dev.altsite.com
```

it doesnt seem to matter if the domain names are on separate lines or not.   After every edit i restart dnsmasq and clear out flush dns in browsers.

url im using for access is

https://foo

https://dev.altsite.com

and 

http://foo

http://dev.altsite.com

any more questions?

----------

## s_bernstein

Stupid question, but you did activate vhost support in your /etc/conf.d/apache, right?

On the other hand, some hints:

1. Your vhost.d/*.conf seems to be correct. Also, you can mix the default vhost with own vhosts without problems and <VirtualHost *:80>

 is also correct.

2. You do not need to add NameVirtualHost statement to httpd.conf. The vhost module will do this for you.

3. Your server must be able to resolve the hostnames - always double check with nslookup

4. You can check your setup with apache2ctl configtest. There was a -S parameter to that commend to test vhost setup but taht doesn't seems to work anymore.

----------

