Tuesday, March 29, 2011

Multiple domains and subdomains in Apache2

If you want to host multiple websites and have one server running Apache2, it's pretty simple to setup virtual hosts so that each domain and subdomain will show different content. You'll need to point your domains and subdomains to the IP address of your server first, as DNS takes some time to update. Once that is done it's just a matter of configuring Apache.

You'll want to find your sites folder, it should be /etc/apache2/sites-available  Here you can either edit the default file or create your own. The advantage to creating your own is being able to enable/disable parts of your configuration rather than the whole thing. Either way, below is what you need for each domain/subdomain you wish to configure:

<VirtualHost *>
        DocumentRoot "/var/www/subdomain/"
        ServerName subdomain.cezary.com

        <Directory /var/www/subdomain/>
                Options Indexes FollowSymLinks MultiViews +Includes
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
</VirtualHost>


What does all that do? I'm glad you asked.

DocumentRoot  is the local path to your content
ServerName  has to match the domain or subdomain of your site
<Directory ...>  contains options and settings for your site. The ones listed here are pretty standard. Modify these to your liking.

You'll want to make a new <VirtualHost *> entry for each domain and subdomain you host. If you put this configuration in the default file then you are almost done. If you made your own file then there is one more step. You need to enable your custom configuration file. You can do this with the following command (as root):

a2ensite xxFILExx

Where xxFILExx is the name of your own config file. And that's it! Now you just need to tell Apache to reload the new settings.

/etc/init.d/apache2 reload

Enjoy!

3 comments:

  1. Thank You, this worked great.

    ReplyDelete
  2. great info, I have also found this https://help.ubuntu.com/12.04/serverguide/httpd.html very helpful and descriptive.

    Cheers

    ReplyDelete
  3. When I add a second .conf file using a2ensite. The first site is no longer available, both sites are now redirecting to site 2

    help

    ReplyDelete