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 123.10.10.2

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.

DNS

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
    • web1.domain.com -- A record --> 123.10.10.2
    • web2.domain.com -- A record --> 123.10.10.2
  • A and CNAME records
    • web.domain.com -- A record --> 123.10.10.2
    • web1.domain.com -- CNAME record --> web.domain.com
    • web2.domain.com -- CNAME record --> web.domain.com

...how 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/web1.domain.com
      • mkdir /var/www/web2.domain.com
    2. Create sub-folders for your web-sites' logs, eg
      • mkdir /var/log/apache2/web1.domain.com
      • mkdir /var/log/apache2/web2.domain.com
  3. Now create the required configs...
    1. Create a config for web1, using the example file contents below
      • vi /etc/apache2/sites-available/web1.domain.com
    2. Create a config for web2, using the example file contents below, but edit so that web1 becomes web2
      • vi /etc/apache2/sites-available/web2.domain.com
  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/web1.domain.com/ and edit
      • cp /var/www/index.html /var/www/web2.domain.com/ and edit
    2. Enable the websites
      • cd /etc/apache2/sites-enabled
      • ln -s ../sites-available/web1.domain.com web1.domain.com
      • ln -s ../sites-available/web2.domain.com web2.domain.com
    3. Restart the Apache service to apply
      • service apache2 restart
  5. Test - you should now have two separately accessible websites..!
<VirtualHost *:80>
       ServerAdmin info@domain.com
       ServerName  web1.domain.com

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

       <Directory />
               Options FollowSymLinks
               AllowOverride None
       </Directory>

       # Logfiles
       ErrorLog  /var/log/apache2/web1.domain.com/error.log
       CustomLog /var/log/apache2/web1.domain.com/access.log combined

</VirtualHost