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.

On my local machine I have several named virtual hosts set up in apache, all on port 80.

NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot "/site1"
    ServerName http://site1.local
        <Directory "site1" >
                Options +Includes
                Order Deny,Allow
                Allow from all
        </Directory>
</VirtualHost>   


<VirtualHost *:80>
    DocumentRoot "/site2"
    ServerName http://site2.local
        <Directory "site2" >
                Options +Includes
                Order Deny,Allow
                Allow from all
        </Directory>
</VirtualHost>   

For testing on mobile devices I need these sites to be reachable via my machine's IP address, in which case the first virtual host declared is the default. I assume I need to set up something with listening on different ports in order to be able to access multiple virtual hosts on the same IP address, but I've tried quite a few variations and can't get it so using the port works remotely but the name still works locally.

How do I set up multiple virtual hosts which are both named and listened to on different ports. Is this even possible?

share|improve this question
add comment

3 Answers

Sure, you just specify the IP address in the <VirtualHost> tag. This means you need to have separate IP addresses for each vhost, but if you've got to differentiate by IP address, you'll already have those allocated.

share|improve this answer
add comment

The ServerName clause should look like this:

ServerName test.example.org

The rest of your configuration is fine for multiple virtualhosts on the same IP and port. For different IPs, womble's answer is correct.

share|improve this answer
    
Actually, I thought that was weird too so I looked it up in the docs. The scheme and port are optional but allowed in ServerName. –  Ladadadada Apr 5 '12 at 15:40
    
Ah, weird. My setup is like his and it works. Something else wrong maybe? DNS? –  gparent Apr 5 '12 at 15:47
add comment

I found the answer in the end.

The trick is to use

<VirtualHost *:80 *:otherportnumber>

This way the server name works (because listening on :80) and the IP address + port number works (because no other named host is listening on that port number)

share|improve this answer
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.