I have a remote server with IP address, I want to set up multiple virtual hosts on this server by giving different ports for each virtual host.

my httpd.conf:

Listen 80
Listen 3333

NameVirtualHost *:80
NameVirtualHost *:3333

<VirtualHost *:80> 
     DocumentRoot "/path/to/site1/" 
     ServerName *:80

<VirtualHost *:3333> 
     DocumentRoot "/path/to/site2/" 
     ServerName *:3333

If no port specified, it will hit site1 (port 80) When I go to or it opens site1 successfully

But when I try it doesn't work.

Is the issue to do with the syntax? or maybe with /etc/hosts file? or firewall?

Please help


When I try to connect to, I get the following error message:

Can't connect to server

  • 2
    'doesn't work' - hangs, gives an error, gets a page but 404, permission denied? What kind of 'doesn't work'. Jun 22 '11 at 14:04
  • 3
    (1) Don't use non-standard ports unless you have a good reason (You have not provided a good reason in your question, therefore you should not be using a non-standard port. Expect strict firewalls to stomp on random high-numbered ports.). (2) See what EightBitTony said above :)
    – voretaq7
    Jun 22 '11 at 14:14

Check netstat -an and make sure Apache is listening on port 3333. Check nothing else is listening instead. If it is, it's most likely a firewall issue (iptables -L and look for a rule allowing port 3333).

  • I've edited the question, it gives me an error "Can't connect to server". When I tried stopping iptables firewall service "service iptables stop", it worked! Now, of course, I don't want to stop the firewall, I only want to allow that specific 3333 port, how can I do that, please?
    – evilReiko
    Jun 23 '11 at 10:26
  • 1
    the answer to that is both simple, and complex - simple, you need to add a rule allowing traffic on port 3333. Complex - how you add that rule depends entirely on what you use to build your current set of firewall rules. It's probably a single iptables command, but integrating it into your firewall requires knowing what firewall you use (its iptables based, but what product manages it for you, if anything). Jun 23 '11 at 10:42

At first: "It doesn't work" is no propper error description. What do you expect to see? What do you actually observe?

Having said that, it seems you are mixing up the concepts of name based virtual hosting and IP/Port based virtual hosting. In the first case (where you actually need the NameVirtualHost and ServerName directive, you can have multiple virtual hosts on the same IP/port. Apache decides which one to use by comparing the contents of the Host header sent with each request by the client with value of the ServerName of the virtual host.

In your case, where you have a single vhost on a port, this is not necessary. Thus, you should remove the NameVirtualHost directives and the ServerName directives (which are in the wrong format nevertheless).

Read http://httpd.apache.org/docs/2.2/vhosts/name-based.html for more information about the concept of name-based virtual hosting.

  • As you said, seems like I mixed between Name-base and IP-base, thanks for the info
    – evilReiko
    Jun 23 '11 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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