# Apache: VirtualHost - Not Working - [Solved]

## Whitey_McNasty

I am at my wits end. I have been at this for several days now... Here is my problem(s)...

I emerge apache. Setup IP Aliasing - add my IPs, chech, they work fine. Goto /var/www and mkdir <domainname>/htdocs/index.php (and insert phpinfo(); etc.) fine. 

Edit: /etc/conf.d/apache2 add "-D PHP4" fine.

Edit /etc/apache2/conf/vhosts/vhosts.conf and add my <VirtualHost> info. Fine.

Edit /etc/apache2/conf/commonapache2.conf, no problems there.

But no matter what I do when I try to navigate to any of the pages setup to be virtual hosts I get;

Forbidden

You don't have permission to access / on this server.

I have read and read and read, and all I can deduce is that it SHOULD work... but alas it no worky... 

Ok here goes some specs'

Kernel = gentoo-2.6.12-r6

Apache = apache-2.0.54-r9

the relevent part of my vhost.conf;

```
<VirtualHost my.ip.add.ress>

ServerName www.mydomain.com

ServerPath /mydomain.com

DocumentRoot /var/www/mydomain.com

</VirtualHost>
```

Nothing special changed in my commonapache2.conf file;

The relevent parts of apache2.conf

```
### Main Configuration Section

### You really shouldn't change these settings unless you're a guru

###

ServerRoot /usr/lib/apache2

ServerName gentoo1.mydomain.com

#LockFile /etc/apache2/apache2.lock

PidFile /var/run/apache2.pid

ErrorLog logs/error_log

LogLevel warn

DocumentRoot /var/www/localhost/htdocs

###

### Virtual Hosts 

###

# We include different templates for Virtual Hosting. Have a look in the 

# vhosts directory and modify to suit your needs.

Include conf/vhosts/vhosts.conf

#Include conf/vhosts/dynamic-vhosts.conf

#Include conf/vhosts/virtual-homepages.conf

```

Relevent part of /etc/conf.d/apache2;

```
# Config file for /etc/init.d/apache2

# An example from /etc/apache2/conf/modules.d/40_mod_ssl.conf:

#

# <IfDefine SSL>

#  <IfModule !mod_ssl.c>

#    LoadModule ssl_module    extramodules/mod_ssl.so

#  </IfModule>

# </IfDefine>

#

# This means that the mod_ssl.so DSO module is only loaded

# into the server when you pass "-D SSL" at startup.  To

# enable WebDAV, add "-D DAV -D DAV_FS".  If you installed

# mod_php then add "-D PHP4".  For more options, please

# read the files in the /etc/apache2/conf/modules.d directory.

APACHE2_OPTS="-D PHP4 -D SSL"
```

and for giggles my directory permissions for /var/www/;

```
drwxr-xr-x  3 root root  72 Aug 29 19:58 mydomain.com

drwxr-xr-x  6 root root 144 Aug 29 19:50 localhost
```

any help would be awesome...  I know I have to be missing somthing but I cant seem to find it...

TIA

----------

## eealex

Any hints from error log?

----------

## Whitey_McNasty

This is about it;

```
[Mon Aug 29 20:02:12 2005] [error] [client my.ip.add.ress] client denied by server configuration: /var/www/mydomain.com/

[Mon Aug 29 20:02:13 2005] [error] [client my.ip.add.ress] client denied by server configuration: /var/www/mydomain.com/favicon.ico

[Mon Aug 29 20:02:17 2005] [error] [client my.ip.add.ress] client denied by server configuration: /var/www/mydomain.com/index.php

[Mon Aug 29 20:02:17 2005] [error] [client my.ip.add.ress] client denied by server configuration: /var/www/mydomain.com/favicon.ico

[Mon Aug 29 20:16:15 2005] [error] [client my.ip.add.ress] File does not exist: /var/www/localhost/htdocs/favicon.ico

[Mon Aug 29 20:16:21 2005] [error] [client my.ip.add.ress] client denied by server configuration: /var/www/mydomain.com/

[Mon Aug 29 20:16:21 2005] [error] [client my.ip.add.ress] client denied by server configuration: /var/www/mydomain.com/favicon.ico

```

It doesn't help me much either...

Oh I should also add this tibit of info...

I have found that if I start with a stock config I can place directorys inside /var/www/localhost/htdocs like this;

/var/www/localhost/htdocs/mydomain1.com

/var/www/localhost/htdocs/mydomain2.com

/var/www/localhost/htdocs/mydomain3.com

and then modify vhosts.conf to reflect the location and it will work fine.. but the very minute that I try to place my directories outside of localhost like I want it.. such as;

/var/www/mydomain1.com

/var/www/mydomain2.com

/var/www/mydomain3.com

and of coarse put the proper locations in the vhosts.conf file.. this has never worked right...

This has all popped up when trying to get phpmyadmin to install properly into the correct directories...

----------

## Whitey_McNasty

OK here is what I had to do;

By putting this <Directory> directive into my vhost.conf file I was able to make it work. 

```

#### LocalHost aka Domain 1 ####

<VirutalHost my.ip.add.ress>

       ServerName www.mydomain1.com

       ServerPath /mydomain1.com

       DocumentRoot /var/www/localhost/htdocs

</VirtualHost>

#### Domain 2 ####

<VirtualHost my.ip.add.ress>

        ServerName www.mydomain2.com

        DocumentRoot /var/www/mydomain2.com/htdocs

        <Directory "/var/www/mydomain2.com/htdocs">

          Options None

          AllowOverride None

          Order allow,deny

          Allow from all

        </Directory>

</VirtualHost>

#### Replicate Domain 2 for further Domains ####

                               ""

                               ""

```

But now my question is why did this not work with my original configuration? Why did adding;

```
<Directory "/var/www/mydomain2.com/htdocs">

          Options None

          AllowOverride None

          Order allow,deny

          Allow from all

        </Directory>
```

make it work?

And yes I tested yet again just commenting out the <Directory> directive and it reverted back to not working... I really do wanna know... PLEASE!!

----------

## johannesbrunner

Well there is just one more thing to say,

many many many thx 4 this little "howto" because i decided to give up setting up virtual hosts.

Now I tried your way and look everything works fine.

----------

## Whitey_McNasty

That make me feel warm and fuzy inside!

I am glad I was not the only one... Maybe this should be a howto in the gentoo-wiki...

I know I have seen LOTS of grief over somthing as simple as VirtualHosting...Bah!

--EDIT--

Ok it is in the Wiki now... http://gentoo-wiki.com/HOWTO_Apache_VirtualHost_by_IP_Address

I am still interested in an explaination.. If have a stack of Apache books and the ENTIRE internet and I find it amazing that it took me this long to figure out somthing that should be so brain dead simple.. Besides it shoudl just work out of the box. I am a bit disappointed that it doesn't.

----------

## Whitey_McNasty

I believe I may have come up with an answer for this problem...

After much reading on the subject it seems that the first <VirtualHost> is already defined by a <Directory> directive. The second and subsequent <VirtualHosts> then get their <Directory> directives from the commonapache2.conf file with seem to be where the restrictions come from and why permission is denied when browsing to new VirtualHosts. So therefor by giving the new VirtualHosts new <Directory> directives they then overwrite the restricted directives that were placed by the commonapache2.conf file. I guess you could go into the commonapache2.conf file and loosen it up a bit... but then again maybe it is better and more secure to simply define the <Directory> directive in the VirtualHost itself like I have done above...

Does this make sense or am I just talking out my ass?

----------

## Dr_Dooom33

After following gentoo wiki instructions I am failing:  :Confused:   :Confused:   :Confused:   :Confused:   :Confused:   :Confused: 

/etc/init.d/apache2 restart

 * Starting apache2 ...

[Fri Oct 14 09:03:26 2005] [warn] NameVirtualHost *:80 has no VirtualHos   [ !! ]

/etc/apache2/conf/apache2.conf

```

### Main Configuration Section

### You really shouldn't change these settings unless you're a guru

###

ServerRoot /usr/lib/apache2

ServerName yourserver.yourdomain.com

#LockFile /etc/apache2/apache2.lock

PidFile /var/run/apache2.pid

ErrorLog logs/error_log

LogLevel warn

DocumentRoot /var/www/localhost/htdocs 

###

### Virtual Hosts

###

# We include different templates for Virtual Hosting. Have a look in the

# vhosts directory and modify to suit your needs.

Include conf/vhosts/vhosts.conf

#Include conf/vhosts/dynamic-vhosts.conf

#Include conf/vhosts/virtual-homepages.conf 

```

/etc/conf.d/apache2

```

# Config file for /etc/init.d/apache2

# An example from /etc/apache2/conf/modules.d/40_mod_ssl.conf:

#

# <IfDefine SSL>

#  <IfModule !mod_ssl.c>

#    LoadModule ssl_module    extramodules/mod_ssl.so

#  </IfModule>

# </IfDefine>

#

# This means that the mod_ssl.so DSO module is only loaded

# into the server when you pass "-D SSL" at startup.  To

# enable WebDAV, add "-D DAV -D DAV_FS".  If you installed

# mod_php then add "-D PHP4".  For more options, please

# read the files in the /etc/apache2/conf/modules.d directory.

APACHE2_OPTS="-D PHP4 -D SSL"

```

/etc/apache2/conf/vhosts/vhosts.conf

```

#### LocalHost aka Domain 1 ####

 <VirutalHost my.ip.add.ress>

       ServerName www.mydomain1.com

       ServerPath /mydomain1.com

       DocumentRoot /var/www/localhost/htdocs

 </VirtualHost>

 #### Domain 2 ####

 <VirtualHost my.ip.add.ress>

        ServerName www.mydomain2.com

        DocumentRoot /var/www/mydomain2.com/htdocs

        <Directory "/var/www/mydomain2.com/htdocs">

          Options None

          AllowOverride None

          Order allow,deny

          Allow from all

        </Directory>

 </VirtualHost>

 #### Replicate Domain 2 for further Domains ####

                                ""

                               "" 

<Directory "/var/www/mydomain2.com/htdocs">

         Options None

         AllowOverride None

         Order allow,deny

         Allow from all

</Directory>

```

 /etc/apache2/listen.conf  (I had no listen.conf file so I made one)

```

NameVirtualHost *

```

```
[color=green] 

# /etc/init.d/apache2 restart

```

[/color]

Then I get

```

/etc/init.d/apache2 restart

 * Starting apache2 ...

[Fri Oct 14 09:03:26 2005] [warn] NameVirtualHost *:80 has no VirtualHos [ !! ]

```

Im frustrated    :Evil or Very Mad:   :Evil or Very Mad:   :Evil or Very Mad:   :Evil or Very Mad: 

----------

## elcman

Dr_Dooom I think you might be running into something that I've been having trouble with. There should be no apache2.conf file in /etc/conf.d anymore. They force it to not work when it is there. After I updated what I needed to and removed the file, it started giving me another error that read to be much the same...

```

[warn] NameVirtualHost *:80 has no VirtualHost  [ !! ] 

```

To fix this I had to reconfigure the vhost.d/ file that I am using (the default). This is what I found at the top:

```

<IfDefine DEFAULT_VHOST>

```

I commented that line out and its ending tag (as I don't where where to define the DEFAULT_VHOST flag). After that, it worked like a charm with the default file settings with many adjustments, of course.

I'm a complete newb when it comes to fiddling in conf files, but growing up trying to make games work on an PC XT with MS-DOS 3.3 using only config.sys and autoexec.bat does give me a certain tenacity.

Now, my question, the thing I'm having trouble with... The APACHE2_OPTS="PHP -D SSL" doesn't seem to work in httpd.conf that now replaces the new Apache2 configuration (/etc/conf.d/apache2.conf). This is what I'm currently struggling with, how do I enable my PHP stuff? As there is a "<IfDefine PHP4>" that seems to block loading of PHP4 unless it is defined... somewhere.

When the "theys" tell me to merge my apache2.conf file and httpd.conf file, are they intending this APACHE_OPTS section to be merged as well? Where can I define the variables that flag the <IfDefine *> tags?

Thanks in advance!

----------

## Dr_Dooom33

Discussion continued here

https://forums.gentoo.org/viewtopic-t-391396-highlight-.html

----------

