# Apache virtual hosts

## marxin

I'm trying to run 2 name-based virtual hosts on my Gentoo box, but I'm not succesfull.

/etc/conf.d/apache2

```

APACHE2_OPTS="-D DEFAULT_VHOST -D INFO -D LANGUAGE -D PHP5 -D SSL_DEFAULT_VHOST"

```

00_marxin.eu_vhost.conf

```

<IfDefine DEFAULT_VHOST>

Listen 80

NameVirtualHost *:80

<VirtualHost marxin.eu>

        ServerName marxin.eu

        ServerAlias www.marxin.eu

        Include /etc/apache2/vhosts.d/marxin.eu_vhost.include

        <IfModule mpm_peruser_module>

                ServerEnvironment apache apache

        </IfModule>

</VirtualHost>

</IfDefine>

```

00_gameground.cz_vhost.conf

```

<IfDefine DEFAULT_VHOST>

Listen 80

NameVirtualHost *

<VirtualHost gameground.cz>

        ServerName gameground.cz

        ServerAlias www.gameground.cz

        Include /etc/apache2/vhosts.d/gameground.cz_vhost.include

        <IfModule mpm_peruser_module>

                ServerEnvironment apache apache

        </IfModule>

</VirtualHost>

</IfDefine>

```

/etc/hosts

```

127.0.0.1       localhost

77.48.17.118    marxin.eu

77.48.17.118    gameground.cz

::1             localhost

```

ifconfig

```

eth0      Link encap:Ethernet  HWaddr 00:16:17:81:71:87

          inet addr:77.48.17.118  Bcast:77.48.17.119  Mask:255.255.255.252

          inet6 addr: fe80::216:17ff:fe81:7187/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:17127 errors:0 dropped:0 overruns:0 frame:0

          TX packets:18708 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:1769390 (1.6 Mb)  TX bytes:2786294 (2.6 Mb)

          Interrupt:23 Base address:0xc800

```

/etc/init.d/apache2 start

```

 * Caching service dependencies ...                                                                                                                    [ ok ]

 * Starting apache2 ...

apache2: apr_sockaddr_info_get() failed for marxin_server

apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

[Mon Aug 04 20:12:50 2008] [warn] VirtualHost marxin.eu:0 overlaps with VirtualHost gameground.cz:0, the first has precedence, perhaps you need a NameVirtualHost directive

[Mon Aug 04 20:12:50 2008] [warn] NameVirtualHost *:0 has no VirtualHosts

[Mon Aug 04 20:12:50 2008] [warn] NameVirtualHost *:80 has no VirtualHosts

(98)Address already in use: make_sock: could not bind to address [::]:80                                                                               

```

Thanks for your advices, marxin

----------

## FlorianSchmidt

In 00_marxin.eu_vhost.conf change

```
NameVirtualHost *:80
```

into

```
NameVirtualHost *
```

(This is more or less just a suggestion)

and

```
<VirtualHost marxin.eu>
```

into:

```
<VirtualHost *>
```

because of http://httpd.apache.org/docs/2.2/mod/core.html#namevirtualhost

in 00_gameground.cz_vhost.conf delete

```

Listen 80 

NameVirtualHost *

```

You must not have more than one "Listen 80" in your whole apache configuration and you only need to define "NameVirtualHost" once, at least for what it looks like you are planning to do.

And change

```
<VirtualHost gameground.cz>
```

into

```
<VirtualHost *>
```

So much for the apache stuff, lets have a look if that helps.

In general, if you really want to divide the configuration into seperated files, you should place the NameVirtualHost and Listen directives in your "main" configuration file, because they have a more general meaning. Sometimes there is also a listen.conf for the Listen directive exclusive, I think debian is doing it that way... Hmm, at this point I must admit that I have no idea how gentoo is doing it, anyway the suggestions made above will work.

----------

## kashani

You've got too many Listen statements. Here's how I set mine up. First off I generally leave the default installed Gentoo default_vhost and defualt.vhost.include files alone. I don't care about traffic that hits my server without specifying a domain name and rather it just hits a blank vhost.

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

```

<IfDefine DEFAULT_VHOST>

Listen 80

NameVirtualHost *:80

<VirtualHost *:80>

        ServerName localhost

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

        <IfModule mpm_peruser_module>

                ServerEnvironment apache apache

        </IfModule>

</VirtualHost>

</IfDefine>
```

Now I start defining my vhosts. No Listen statements, not NameVirtualHost, etc because you've already entered all that in the default_vhost.conf. 

/etc/apache2/vhosts.d/badapple.net.conf

```

<VirtualHost *:80>

ServerName badapple.net

ServerAlias www.badapple.net

ServerAdmin webmaster@badapple.net

DocumentRoot /var/www/badapple.net/htdocs

CustomLog /var/log/apache2/badapple.net.access.log combined

ErrorLog /var/log/apache2/badapple.net.error.log

# PHP tweaks

php_value post_max_size 104857600

php_value upload_max_filesize 104857600

<Directory "/var/www/badapple.net/htdocs">

        Options -Indexes FollowSymLinks MultiViews

        AllowOverride All

        Order allow,deny

        Allow from all

</Directory>

RewriteEngine on

RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)

RewriteRule .* - [F]

</VirtualHost>
```

You can continue to define the DEFAULT_VHOST stuff in each vhost you create, but generally I assume I want all my vhosts all the time. 

kashani

----------

## leonardosam

Hey guys,

I'm running into the same stupid problem here and can't find where the problem is. I have tried many suggestions and none seemed to work. I have a default host in www.x.com and the one that I want to work under /home/user/public_html is www.y.com , however, whenever I try to open www.y.com , I end up at www.x.com

It is driving me crazy.

Here's my configuration:

/etc/conf.d/apache2

```
APACHE2_OPTS="-D DEFAULT_VHOST -D INFO -D LANGUAGE -D SSL -D SSL_DEFAULT_VHOST -D PHP5 -D MAILMAN -D USERDIR -D STATUS"
```

/etc/apache2/httpd.conf

```

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

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

```
<IfDefine DEFAULT_VHOST>

Listen 80

NameVirtualHost *:80

  <VirtualHost *:80>

    ServerName  x.com

    ServerAlias www.x.com

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

    <IfModule mpm_peruser_module>

      ServerEnvironment apache apache

    </IfModule>

  </VirtualHost>

</IfDefine>
```

/etc/apache2/vhosts.d/01_y_com.conf

```
<IfDefine DEFAULT_VHOST>

  <VirtualHost *:80>

    ServerName y.com

    ServerAlias www.y.com

    DocumentRoot /home/user/public_html

  </VirtualHost>

</IfDefine>
```

Plus, I have modules alias and virtualhost_alias loaded.

Everything seems to be okay, right? What the heck is going on in here anyway?

I would appreciate any support. Thank you.

----------

## cach0rr0

@leonardosam

I don't see anywhere that your y.com vhost is actually configured to use that .conf file

I only see default_vhost.include

Here is how I have mine set up, which works

00_default_vhost.conf snippet:

```

<IfDefine DEFAULT_VHOST>

Listen 80

# Use name-based virtual hosting.

NameVirtualHost *:80

<VirtualHost *:80>

   ServerName mydomain.com

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

   <IfModule mpm_peruser_module>

      ServerEnvironment apache apache

   </IfModule>

</VirtualHost>

<VirtualHost *:80>

        ServerName eu.mydomain.com

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

   php_admin_value session.save_path "/var/www/eu/tmp"

   php_admin_value file_uploads off

        <IfModule mpm_peruser_module>

                ServerEnvironment apache apache

        </IfModule>

</VirtualHost>

```

Notice the Include statement in the second block? and of course, snippet of eu_vhost.include

```

DocumentRoot "/var/www/eu/htdocs"

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

   Options Indexes FollowSymLinks

   AllowOverride All

   Order allow,deny

   Allow from all

</Directory>

```

note that when you include all *.conf in your main httpd.conf is going to be out of whack with your file named as it is. You need apache to include that config in a very specific place, so you use the Include directive as I have above. 

hope that helps

----------

## cach0rr0

sorry for the double post, im watching the game and distracted  :Smile: 

So, first and foremost, let's rename that file

```

mv /etc/apache2/vhosts.d/01_y_com.conf /etc/apache2/vhosts.d/01_y_com.include

```

then change your 00_default_vhost.conf to be like so:

```

<IfDefine DEFAULT_VHOST>

Listen 80

NameVirtualHost *:80

  <VirtualHost *:80>

    ServerName  x.com

    ServerAlias www.x.com

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

    <IfModule mpm_peruser_module>

      ServerEnvironment apache apache

    </IfModule>

  </VirtualHost>

  <VirtualHost *:80>

    ServerName y.com

    ServerAlias www.y.com

    Include      /etc/apache2/vhosts.d/01_y_com.include

    <IfModule mpm_peruser_module>

      ServerEnvironment apache apache

    </IfModule>

  </VirtualHost> 

</IfDefine>

```

Then change 01_y_com.include like so:

```

DocumentRoot "/home/user/public_html"

<Directory "/home/user/public_html">

   Options Indexes FollowSymLinks

   AllowOverride All

   Order allow,deny

   Allow from all

</Directory>

```

restart apache and see how you go

----------

## leonardosam

Everything was fine regarding the configuration. I was trying to setup the vhost without any further configuration first. The problem is that Apache does not accept to have virtual hosts under a public_html (~username) directory.

It is now running sound and smoothly.

----------

