# How do I set up named-based vhosts? [SOLVED]

## audiodef

I know Apache documents this thoroughly. The problem is, I haven't found Apache documentation detailing in which exact files to put directives. There are various config files (/etc/conf.d/apache2, /etc/apache2/httpd.conf, /etc/apache2/vhosts.d/*). 

So here's what I want to do. 

I have one server with one static IP address. I want to create two vhosts, each with their own document root (they can both be subdirs of /var/www/localhost/htdocs - I don't care). 

I have one site currently working under the default document root. I've moved it to a subdir of htdocs, where it doesn't quite work because some includes are referenced at the document root, which is one dir back. I assuming if I've configured vhosts correctly, the subdir should be the doc root for that vhost and includes referenced at the doc root should then work. Ditto all that for the other site I want to run under a vhost of the same IP address. 

What directives do I need to place in what files to get this working? 

Specific - and actual, live - example: http://209.177.157.239/audiodef.com/

I want the dir "audiodef.com" to become the doc root for that site so that clicking on home goes to "/" for that site. Meanwhile, I want the other site I plan to run - not created yet - to have, for example, http://209.177.157.239/jlynnpro.com as it's doc root. The two sites have nothing to do with each other. 

When I'm done with all that and it's all working from the IP address in my browser, I will then go to my registrar to point two domains  to the two vhosts I've set up. 

I hope I've made sense describing what I want to do.   :Laughing: 

----------

## Hu

/etc/conf.d/apache2 controls the Gentoo initscript that starts Apache.  /etc/apache2/httpd.conf includes every .conf file in /etc/apache2/vhosts.d.  It is traditional to put named virtual hosts in dedicated files in vhosts.d, but you could put it elsewhere.  Once you have picked a file, then you want something like this (untested):

```
<VirtualHost audiodef.com:80>

ServerAdmin webmaster@audiodef.com

DocumentRoot "/var/www/audiodef/htdocs"

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

    Options Indexes FollowSymLinks

    AllowOverride All

    Order allow,deny

    Allow from all

</Directory>

</VirtualHost>
```

```
<VirtualHost jlynnpro.com:80>

ServerAdmin webmaster@jlynnpro.com

DocumentRoot "/var/www/jlynnpro/htdocs"

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

    Options Indexes FollowSymLinks

    AllowOverride All

    Order allow,deny

    Allow from all

</Directory>

</VirtualHost>
```

You can add other site-specific directives, such as ScriptAlias, as needed.  If you find the sites have a significant amount in common, you may want to use an Apache Include directive so that you can maintain a single copy of some directives.

----------

## audiodef

Thanks, Hu. 

I decided to see what would happen if I pointed my domain to my server's IP address. When I did that and pointed my browser at audiodef.com, I got the empty doc root of http://209.177.157.239/ instead of the files from /var/www/audiodef/htdocs. 

What'd I miss?

----------

## Hu

Did you disable the default vhost?  It likely sorts earlier in the list than the files I gave.

----------

## audiodef

I'm trying to figure out how to disable the default vhost. Moving /etc/apache2/vhosts.d/00_default_vhost.conf results in an unreachable server.

----------

## Atle

Always remember to restart apache after making changes.

Also it's good practice to run apache2ctl configtest for a syntax check.

----------

## audiodef

 *Atle wrote:*   

> Always remember to restart apache after making changes.
> 
> 

 

Check!   :Cool: 

 *Atle wrote:*   

> 
> 
> Also it's good practice to run apache2ctl configtest for a syntax check.

 

I'll keep that in mind, thanks!   :Cool: 

I commented out the <VirtualHost> directive in 00_default_vhost.conf, and now it seems to be working, but when I restart apache2, I get

```

 * Stopping apache2 ...

[Sat Apr 02 08:22:11 2011] [warn] NameVirtualHost *:80 has no VirtualHosts                    [ ok ]

 * Starting apache2 ...

[Sat Apr 02 08:22:13 2011] [warn] NameVirtualHost *:80 has no VirtualHosts                    [ ok ]

```

Is this something that should be fixed, even though I can now see audiodef.com hosted on my new server?

----------

## Anon-E-moose

 *audiodef wrote:*   

> I'm trying to figure out how to disable the default vhost. Moving /etc/apache2/vhosts.d/00_default_vhost.conf results in an unreachable server.

 

look in /etc/conf.d/apache

```
#  DEFAULT_VHOST      Enables name-based virtual hosts, with the default

#                     virtual host being in /var/www/localhost/htdocs

#  SSL_DEFAULT_VHOST  Enables default vhost for SSL (you should enable this

#                     when you enable SSL)

#

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

```

Although I left it enabled on my system and used it for localhost and I have the external IP in a separate file.

Edit to add: this is the default minus the "#" lines

```
<IfDefine DEFAULT_VHOST>

Listen 8080

NameVirtualHost *:8080

SetEnvIf Remote_Addr "127\.0\.0\.1" dontlog

<VirtualHost *:8080>

   ServerName localhost

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

   <IfModule mpm_peruser_module>

      ServerEnvironment apache apache

   </IfModule>

</VirtualHost>

</IfDefine>

```

this is a sample based on my config called tst-vhost.conf

```
SetEnvIf Request_URI "/default.ida" dontlog

SetEnvIf Request_URI "cmd.exe" dontlog

SetEnvIf Request_URI "/scripts" dontlog

SetEnvIf Request_URI "/MSADC" dontlog

SetEnvIf Request_URI "\.gif$" dontlog

SetEnvIf Request_URI "\.jpg$" dontlog

SetEnvIf Request_URI "\.css$" dontlog

SetEnvIf Request_URI "images$" dontlog

SetEnvIf Request_URI "\.avtr$" dontlog

SetEnvIf Remote_Addr "127\.0\.0\.1" dontlog

RedirectMatch 400 "\\\.\."

<VirtualHost www.tst.com:8080>

   ServerName www.tst.com

   ServerAlias tst

   SetOutputFilter DEFLATE

   AddOutputFilterByType DEFLATE text/html text/plain text/xml

   # enable expirations

   ExpiresActive On

   ExpiresDefault "access plus 1 day"

   # expire GIF images after a month in the client's cache

   ExpiresByType image/gif A2592000

   DocumentRoot /var/www/tst/htdocs

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

      # Some sane defaults - see httpd.conf for details

      Options Indexes FollowSymLinks

      AllowOverride None

      Order allow,deny

      Allow from all

   </Directory>

   ScriptAlias /cgi-bin "/var/www/tst/cgi-bin"

   <Directory "/var/www/tst/cgi-bin">

      AllowOverride None

      Options None

      Order allow,deny

      Allow from all

   </Directory>

</VirtualHost>         
```

----------

## audiodef

I think I get it now. It seems like I have vhosts set up correctly. When I point my browser to my server's IP address, I get audiodef.com. I'm assuming this is because it's the "first" in the "list". I have my vhosts in separate files, but I'm guessing the first file in alphabetical order is read as the default when using the IP address in a browser. 

Next, to move the other domain over and make sure my name-based vhost setup is indeed working.

----------

## cach0rr0

 *audiodef wrote:*   

> I think I get it now. It seems like I have vhosts set up correctly. When I point my browser to my server's IP address, I get audiodef.com. I'm assuming this is because it's the "first" in the "list". I have my vhosts in separate files, but I'm guessing the first file in alphabetical order is read as the default when using the IP address in a browser. 
> 
> Next, to move the other domain over and make sure my name-based vhost setup is indeed working.

 

sounds like audiodef.com is the first vhost that gets loaded/read from any of the configuration files, and what in effect is treated as your default vhost 

not sure if you saw this snippet which explains somewhat how it works

```

# When virtual hosts are enabled, the main host defined in the default

# httpd.conf configuration will go away. We redefine it here so that it is

# still available.

#

# If you disable this vhost by removing -D DEFAULT_VHOST from

# /etc/conf.d/apache2, the first defined virtual host elsewhere will be

# the default.

```

If it helps, This is my 00_default_vhost.conf

```

<IfDefine DEFAULT_VHOST>

Listen 80

NameVirtualHost *:80

<VirtualHost *:80>

        ServerName whitehathouston.com

        ServerAlias bauer.whitehathouston.com

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

        <IfModule mpm_peruser_module>

                ServerEnvironment apache apache

        </IfModule>

</VirtualHost>

<VirtualHost *:80>

        ServerName blueball.me

        ServerAlias dont.blueball.me www.blueball.me blueballed.me www.blueballed.me

        Include /etc/apache2/vhosts.d/insecure-blueball_vhost.include

        php_admin_value session.save_path /www/blueball.me/tmp/

        <IfModule mpm_peruser_module>

                ServerEnvironment apache apache

        </IfModule>

</VirtualHost>

</IfDefine>

```

I have two main domains

-whitehathouston.com

-blueball.me

my default vhost is the first one, as it's what gets read from that file first 

So if I browse to a website (that points to this box of course) that doesn't match any ServerName or ServerAlias directive elsewhere, (e.g. if I set up an "A" record in DNS for "test.blueball.me"), it will be routed to the default vhost of 'whitehathouston.com'

You'll also notice above, that I've followed the default file's trend of using an "Include" statement in my <VirtualHost> block. The file I'm including has a handful of additional settings that cover permissions and whatnot on the directory; for example, default_vhost.include:

```

ServerAdmin root@whitehathouston.com

DocumentRoot "/www/whitehathouston.com/htdocs"

<Directory "/www/whitehathouston.com/htdocs">

        Options Indexes FollowSymLinks

        AllowOverride All

        Order allow,deny

        Allow from all

</Directory>

<IfModule alias_module>

        ScriptAlias /cgi-bin/ "/www/whitehathouston.com/cgi-bin/"

</IfModule>

<Directory "/www/whitehathouston.com/cgi-bin">

        AllowOverride None

        Options None

        Order allow,deny

        Allow from all

</Directory>

```

Notice that my VirtualHost block does not define a DocumentRoot; my Include file, however, does. You could just as easily not use that Include statement, and put all of the other stuff directly into the VirtualHost block

I also, as you see above, have one for the blueball.me domain. It specifies a different DocumentRoot, and of course that prompts apache to serve files for that ServerName or any ServerAlias listing, from this other DocumentRoot. 

Hopefully that helps instead of adding to the confusion?

----------

## audiodef

 *cach0rr0 wrote:*   

> 
> 
> Hopefully that helps instead of adding to the confusion?

 

Yes, that does help, thanks! It's another way of doing things. The way you've described appears more efficient to me, so once I have both my domains hooked up and running, I'll probably do it that way, too.

----------

