Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I would like to have 2+ sites running on the same apache server. I cant figure out how to do it. This example file was taken from a tutorial i found. Then i searched for another tutorial online and i cam up with the 2nd file.

Apache seems to use the default unless i disable it. When i enable the default it continues to use my other one until i disable that (and when i enable it, it continues to use the default...). I cant get both of them working at once. How do i configure this?

-edit- I changed the 2nd host (first two lines) made a clone and changed the domain to the ip address so i now have 3 and i still have my ip and domain always the same site and not the (mono) test site + my main site.

<VirtualHost *:80>
    ServerName www.local.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/test
    DirectoryIndex index.html index.aspx
    MonoDocumentRootDir "/var/www/test"
    MonoServerPath rootsite "/usr/local/bin/mod-mono-server2"
    MonoApplications rootsite "/:/var/www/test"

    <Directory /var/www/test>
        MonoSetServerAlias rootsite
        SetHandler mono
        AddHandler mod_mono .aspx .ascx .asax .ashx .config .cs .asmx
    </Directory>


</VirtualHost>

Second domain

NameVirtualHost MyTestDomain.com:80
<VirtualHost MyTestDomain.com:80>
    ServerName  MyTestDomain.com
    ServerAdmin webmaster@localhost
    ServerAlias MyTestDomain.com

    DocumentRoot /var/www/myapp_path
    DirectoryIndex index.html index.aspx
    MonoDocumentRootDir "/var/www/myapp_path"
    MonoServerPath rootsite "/usr/local/bin/mod-mono-server2"
    MonoApplications rootsite "/:/var/www/myapp_path"

    <Directory /var/www/myapp_path>
        MonoSetServerAlias rootsite
        SetHandler mono
        AddHandler mod_mono .aspx .ascx .asax .ashx .config .cs .asmx
    </Directory>


</VirtualHost>
share|improve this question
add comment

5 Answers

up vote 2 down vote accepted

You usually just need one host definition (the *:80 in <VirtualHost *:80>) and thus just one NameVirtualHost statement. (Usually meaning always except you have tons of IPs that server is listening on.) So your config should be like that:

NameVirtualHost *:80
<VirtualHost *:80>
    ServerName example.com
    # No ServerAlias required: Non-Matching domains go here anyway.
    ...
</VirtualHost>
<VirtualHost *:80>
    ServerName example.org
    ServerAlias www.example.org
    ...
</VirtualHost>

For more information, see the Apache2 docs about virtual hosts (specifically the name-based section)

Why your approach does not work: If you use a hostname instead of the *, this does not mean that this virtual host block is used when this hostname is sent as the Host header. It just means that this block is only considered when the IP address the request was sent to matches the IP the hostname resolves to. Short: don't use hostnames there.

share|improve this answer
    
ok THIS worked and i dont know why. Its similar to what i originally tried (look at first revision). I am going to try to get a 3rd site but using SSL –  acidzombie24 Jul 13 '10 at 15:29
    
I do get this warning # /etc/init.d/apache2 restart Restarting web server: apache2[Tue Jul 13 15:30:48 2010] [warn] NameVirtualHost *:80 has no VirtualHosts ... waiting ..[Tue Jul 13 15:30:51 2010] [warn] NameVirtualHost *:80 has no VirtualHosts . –  acidzombie24 Jul 13 '10 at 15:31
add comment

Do you have NameVirtualHost *:80 somewhere in your config? You need this to turn on multiple domains via name based virtual hosting.

share|improve this answer
    
Where is the config? do you mean the same file? no i dont. I tried NameVirtualHost MyTestDomain.com:80 and NameVirtualHost MyTestDomain.com without luck. -edit- i googled and tried it just now after i notice your answer 1 min in. –  acidzombie24 Jul 13 '10 at 14:15
    
note: both these answers ATM provide no help –  acidzombie24 Jul 13 '10 at 15:21
add comment

Just use

NameVirtualHost *:80

Remove

NameVirtualHost MyTestDomain.com:80

Switch

<VirtualHost MyTestDomain.com:80>

To this

<VirtualHost *:80>

Then restart apache

share|improve this answer
1  
Then i'd have two <VirtualHost *:80>. I did that before. It didnt allow me to have two different sites running at once. Just one or the other on the ip and domain. –  acidzombie24 Jul 13 '10 at 14:58
    
You need a name on all your additional domains. The first domain (default) should have no name. Your errorlog should indicate if you have any problems. –  BillThor Jul 13 '10 at 15:00
    
I also get this error ---> Restarting web server: apache2[Tue Jul 13 15:00:33 2010] [warn] NameVirtualHost *:80 has no VirtualHosts ... waiting .[Tue Jul 13 15:00:35 2010] [warn] NameVirtualHost *:80 has no VirtualHosts . –  acidzombie24 Jul 13 '10 at 15:01
    
@BillThor: How do i put names? where? isnt that what i have above? –  acidzombie24 Jul 13 '10 at 15:01
1  
Every virtual host should have a name. It's just that the first one is used no matter the name if no other ones matching the request are found. –  Marian Jul 13 '10 at 15:02
show 2 more comments

Mike and Mark are both correct. The NameVirtualHost argument must exactly match the argument to <Virtualhost>

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName www.local.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/test
</VirtualHost>


<VirtualHost *:80>
    ServerName www.example.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/test2
</VirtualHost>

A configuration like this should work for you.

share|improve this answer
    
hmm, this worked and i dont know why. Its similar to what i originally tried (look at first revision). I do get a warning whenever i have `NameVirtualHost *:801 –  acidzombie24 Jul 13 '10 at 16:52
    
The NameVirtualHost is required for your setup, which is why it didn't work to begin with. Are you duplicating the NameVirtualHost line? –  theotherreceive Jul 13 '10 at 20:52
add comment

Here is the setup for a working virtual server. After trials and errors I reached this conclusion and it works perfectly with multiple domains and the default vhost unchanged:

Add NameVirtualHost *:80 in ports.conf or httpd.conf and include it above the virtual hosts, or write it straight to apache2.conf above includes.

This is the basic working vhost setup:

/etc/apache2/sites-available/domain.info.conf :

<VirtualHost *:80>
DocumentRoot /var/www/domain.info
ServerName domain.info
ServerAlias *.domain.info
<Directory /var/www/domain.info>
Allow from all
Options +Indexes
ServerSignature Off
</Directory>
</VirtualHost>

And DNS setup :

domain.info.           IN   A          11.11.11.11
www.domain.info.       IN   CNAME      domain.info.

I hope it helps.

share|improve this answer
    
I went with nginx, never looked at apache since. Old post :) but +1 anyways –  acidzombie24 Apr 4 '13 at 23:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.