Multiple Websites On The Same Server

From vWiki
Jump to navigation Jump to search

There are various methods that can be used to enable you to host multiple websites on the same server. This is but one, and allows you to host different domains on the same server.

You'll need a publicly accessible server running Apache, and the ability to create DNS entries for the domains you want to host. In this example we're creating two websites, called web1 and web2, on a server with IP address

If you want one (or more) of your sites to be secure (accessible via HTTPS), then also see the Secure Website page. Though you're recommended to get multiple sites working 1st, and the attempt to add security.


Before you configure the webserver, you need to set-up DNS correctly, for this example this could be done in one of two ways...

  • A records only
    • -- A record -->
    • -- A record -->
  • A and CNAME records
    • -- A record -->
    • -- CNAME record -->
    • -- CNAME record --> you choose to do it is up to you, and depends on what changes you might expect to make down the line. If you have a small set-up its probably easiest to go with purely A-records, but where you have a larger number of websites and web-hosts, it can be easier to manage with all your web-hosts having A-records and all web-sites having CNAME-records pointing to the host that they're running from.

Either way, you should be able to ping the addresses of either website and get the correct IP address back.

Web Server Configuration

Now you should check that you web server is working correctly. If its a new install, you should be able to browse to the server's IP address and get a basic "It works!" page back. This site, or any site that's using the root of your web-server will need to be (re)moved.

Now to create the web-sites...

  1. If you have an existing site that's served from the root of your webserver, this needs to be removed 1st. So to remove the default site that's installed with Apache...
    1. Disable the web-site by removing the link to its config from sites-enabled
      • rm /etc/apache2/sites-enabled/000-default
      • service apache2 restart (you may get a warning about no VirtualHosts)
    2. Test by browsing to your web-server's IP address, no webpage should be returned
  2. Now create the required folders...
    1. Create sub-folders for your web-sites, eg
      • mkdir /var/www/
      • mkdir /var/www/
    2. Create sub-folders for your web-sites' logs, eg
      • mkdir /var/log/apache2/
      • mkdir /var/log/apache2/
  3. Now create the required configs...
    1. Create a config for web1, using the example file contents below
      • vi /etc/apache2/sites-available/
    2. Create a config for web2, using the example file contents below, but edit so that web1 becomes web2
      • vi /etc/apache2/sites-available/
  4. Now create some test content, and enable...
    1. Copy the default index.html to your new web-site folders, and edit so that they identify the web-site they're in
      • cp /var/www/index.html /var/www/ and edit
      • cp /var/www/index.html /var/www/ and edit
    2. Enable the websites
      • cd /etc/apache2/sites-enabled
      • ln -s ../sites-available/
      • ln -s ../sites-available/
    3. Restart the Apache service to apply
      • service apache2 restart
  5. Test - you should now have two separately accessible websites..!
<VirtualHost *:80>

       # Indexes + Directory Root.
       DirectoryIndex index.php
       DocumentRoot /var/www/

       <Directory />
               Options FollowSymLinks
               AllowOverride None

       # Logfiles
       ErrorLog  /var/log/apache2/
       CustomLog /var/log/apache2/ combined