Multiple sites with Apache Server on different ports or domains

Apache Server multiple site on ports

Sometimes people become amazed with an awesome feature of his new shiny tool, and never discover that it was already there for years in his old boring tool. A good example of this phenomenon is – I see people choosing nginx for it’s ability of listening to multiple ports and domains.

Definitely there are solid reasons and cases for choosing nginx. But, I’ll say, this one alone is not a good reason for switching to nginx if you’re already running on and confident with Apache Server. Because, you can serve multiple apps/sites on different ports or domains using Apache too.

Let’s see some quick example of how Apache can do it. I’ll here just list a few example of Apache VirtualHost configs for various cases with a single server.

A. Multiple sites with different domain names

Basic, simple name based VirtualHosts.

Listen 80
<VirtualHost 12.34.56.78:80>
    ServerName www.mysite.com
    DocumentRoot "/www/mysite"
</VirtualHost>
 
<VirtualHost 12.34.56.78:80>
    ServerName www.othersite.org
    DocumentRoot "/www/othersite"
</VirtualHost>

B. Multiple applications on different ports

Listen to multiple port and map them to different root directory.

Listen 80
Listen 8080
 
<VirtualHost 12.34.56.78:80>
    ServerName www.example.com
    DocumentRoot "/www/domain-80"
</VirtualHost>
 
<VirtualHost 12.34.56.78:8080>
    ServerName www.example.com
    DocumentRoot "/www/domain-8080"
</VirtualHost>

C. Same content to multiple IPs

Simply list all the IPs in your VirtualHost config.

<VirtualHost 11.11.11.11 22.22.22.22>
    DocumentRoot "/www/server1"
    ServerName server.example.com
</VirtualHost>

D. Make a catch-all/any domain host for a port

<VirtualHost _default_:8000>
    DocumentRoot "/www/default8000"
    # ...
</VirtualHost>

 

There are lots of other interesting things you can do with Apache VirtualHost. For example, listening to subdomains, proxy_pass to other IP or Port, mixing IP and name based host etc. So, please don’t forget to check Apache’s VirtualHost guide and .htaccess tricks before thinking “may be it’s not possible with Apache”.

3 Comments

    1. The configuration and concept described in this post are specific to Apache server. So, it’s equally applicable for Ubuntu too.
      The only thing you need to ensure is that you are editing the right configuration file for your Apache installation.

Leave a Comment

Your email address will not be published. Required fields are marked *