Apache and Multiple Domains – from Home
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:
- Three domains: www.domain1.com, www.domain2.com, www.domain3.com
- Domain1 home is in /srv/www/htdocs; Domain2 home is in /srv/www/htdocs/domain2; and Domain3 home is in /srv/www/htdocs/domain3
- 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 firstname.lastname@example.org 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 email@example.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 firstname.lastname@example.org 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.