Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Apache

Apache and Multiple Domains – from Home

Submitted by on June 10, 2008 – 5:15 pm 3 Comments

Apparently there are folks out there who are still interested in running their own Web server from home. More than that – they want to have multiple domains. Guys, Web hosting these days is so cheap – they are virtually paying you to host your site. Besides, there are ISPs out there that don’t look kindly on their customers running their own Web servers. However, if you insist, here are the step-by-step (more or less) instructions for setting up a multi-domain Apache 2 server under Linux.

The IP address your ISP gave you is going to change sooner or later. So you need a way of quickly updating DNS servers. You cannot do this from home – you will need to sign up for a managed DNS service. One of such services is No-IP. If you need to register a domain name, they can do it for you as well. If you already have a domain name, you will need to update your DNS information with your registrar to point to the No-IP DNS servers.

At the No-IP site, you will need to specify your home IP address. If your IP changes often, you may need to look into the dynamic DNS feature. There is a client application that you can download, that will detect when your IP changes and it will update your No-IP information automatically. For an additional fee No-IP and similar providers can handle email service for your domain.

You can have more than one domain point to your home IP. For example, you can have “www.domain1.com”, “www.domain2.com”, and “www.domain3.com” all point to the same IP address. The trick is to make your Web server differentiate among the three. So, when an HTTP request comes in, how does your server know which domain it’s for? When you click on a link, the HTTP referrer information specifies the domain name. This information is passed to your server and that’s how it knows which domain to use.

Apache supports name-based virtual hosts. As you probably realize, with a single IP address you can’t have three separate IP-based Web servers running on your system. But you can have virtual and that’s good enough for most purposes. Below is the virtual host section of a sample httpd.conf tested on Apache 2 running under SuSE 9. Make sure to backup your original httpd.conf before making any changes. The basic setup goes something like this:

  1. Three domains: www.domain1.com, www.domain2.com, www.domain3.com
  2. Domain1 home is in /srv/www/htdocs; Domain2 home is in /srv/www/htdocs/domain2; and Domain3 home is in /srv/www/htdocs/domain3
  3. Domain1 is the primary physical domain. If an HTTP request comes in and there is no HTTP referrer information, then it will default to Domain1.
    DocumentRoot /srv/www/htdocs/domain3
    ServerName www.domain3.com
    ServerAlias domain3.com *.domain3.com

    ServerAdmin webmaster@domain3.com
    ErrorLog /var/log/apache2/domain3_error.log
    CustomLog /var/log/apache2/domain3_access.log combined

    HostNameLookups off
    UseCanonicalName On
    ServerSignature On

        Options -Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all

    DocumentRoot /srv/www/htdocs/domain2
    ServerName www.domain2.com
    ServerAlias domain2.com *.domain2.com

    ServerAdmin webmaster@domain2.com
    ErrorLog /var/log/apache2/domain2_error.log
    CustomLog /var/log/apache2/domain2_access.log combined

    HostNameLookups off
    UseCanonicalName On
    ServerSignature On

        Options -Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all

    DocumentRoot /srv/www/htdocs
    ServerName www.domain1.com
    ServerAlias domain1.com *.domain1.com

    ServerAdmin webmaster@domain1.com
    HostNameLookups off
    UseCanonicalName On
    ServerSignature On

        Options -Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all

        Options None
        AllowOverride None
        Order allow,deny
        Deny from all

        Options None
        AllowOverride None
        Order allow,deny
        Deny from all

Now a couple of security pointers. Make sure that your primary (catch-all) “domain1” is last in the list of virtual servers. Also, in the directory configuration of the “domain1” virtual server restrict access to the “domain2” and “domain3” subfolders (see “Deny from all” syntax above). This way these directories can only be accessed via their respective virtual servers.

As shown above, it is a good idea to have a separate access and error log for each virtual server. This way you can analyze traffic for each domain separately. In the “Options” for the home directory for each virtual server make sure to use the “-Indexes” flag. This will prevent people from getting a directory listing for subfolders that do not have a default page, such as index.htm or index.php: you don’t want anybody snooping around.

The next step is often overlooked and it’s not mentioned in the official Apache documentation. You need to make sure that you put the following entries in your /etc/hosts:

127.0.0.1         localhost
127.0.0.1         www.domain1.com         domain1.com
127.0.0.1         www.domain2.com         domain2.com
127.0.0.1         www.domain3.com         domain3.com

The first line should already be there, but you will need to add the other three to make sure that your server knows that its handling all three domains. Otherwise, all attempts to access virtual servers will result in “host not found” type of error or it will default to your primary domain. Now you need to restart your Apache and, if you didn’t skip any steps, everything should be working.

Print Friendly, PDF & Email

3 Comments »

  • plunk7 says:

    Thanks for the tip. I did not have the /etc/hosts entries and every time I tried accessing virtual hosts, I was getting the default domain. Now everything works as it should.

  • llb443 says:

    I recently downloaded WAMP server. I put my website on it, so that whenever I type “localhost” into the URL bar, my website comes up. I also bought a domain name form GoDaddy and assigned it to my router’s static IP address. But whenever I type in my domain name, it brings me to the setup page fore my router, not my website. Also, my Server is online, there are multiple computers on my home network, and I think it has something to do with the port forwarding settings.

  • NC Baller says:

    I’m buying a new system tomorrow, and I want to use my old system to host my website. I’ve purchased a domain name through Godaddy.com, and want to move it to my own server at home. What OS should I use, what server software should I use, can I set up an email server too? Where should I start? Help?

Leave a Reply

%d bloggers like this: