Add virtual hosts and subdomains to your Raspberry Pi Apache2 server

If you’re feeling brave, you might want your Raspberry Pi powered web server to host more than one website or subdomain. Providing you’ve made the necessary modifications to increase performance, and are running Raspbian from a USB flash drive, there’s no reason why your Raspberry Pi couldn’t host more than one site.

Before you begin, I’m going to presume that you’ve followed my earlier tutorials to install Apache2, PHP and MySQL on your Raspberry Pi, and you have already got your domain or subdomain DNS records pointing to your Raspberry Pi.

Step 1 – Create the sites available file for your domain or subdomain

Throughout this tutorial, I’ll be adding the subdomain video.dingleberrypi.com so replace this with your domain or subdomain you want to add. First, we’ll run our usual sudo bash command to login at root level:

sudo bash

After we’ve done this, we’ll want to copy the default file:

cp /etc/apache2/sites-available/default /etc/apache2/sites-available/video.dingleberrypi.com

Now, we’ll want to edit this file to tell Apache where the document  root is, as well as any other information we wish to provide. To do this, type:

nano /etc/apache2/sites-available/video.dingleberrypi.com

Once you’ve opened your file, we’ll need to add a line. After the line ‘ServerAdmin webmaster@localhot’ (change this to your email address if you really want to), type the following line (edit as you see fit):

ServerName video.dingleberrypi.com

After this, we’ll want to change the document root. Don’t worry if the folder isn’t there yet, we’ll create it in the next step. Change the document root and directory to something similar to how I have below from ‘/var/www’:

Change the sites-available fileMake a note of the directory, we’ll create it next. Now press CTRL + X to quit, and save when prompted.

Step 2 – Create the document root for your domain or subdomain

Next, we’ll need to create and set the correct permissions to the document root. You can choose whatever path to use, but I’m creating mine inside a directory called vhosts inside the www folder.

If you’ve not already created the ‘vhosts’ directory already, you’ll need to create this first:

mkdir /var/www/vhosts/

After this, create the site’s directory:

mkdir /var/www/vhosts/video.dingleberrypi.com/

If you’ve not already created the ‘vhosts’ directory already, you’ll need to create this first:

mkdir /var/www/vhosts/

Almost there! Now we need to set the permissions so that the system can write, and everyone else can read:

chmod -R 755 /var/www/vhosts/video.dingleberrypi.com

Step 3 – Enable the subdomain or domain and restart Apache2

Next, we’re going to use a built in extension to enable the submain or domain:

a2ensite video.dingleberrypi.com

Now, it’s time to restart apache:

service apache2 restart

Step 4 – Give it a try!

Let’s create a file in our site’s directory to give it a go. Type the following command to create our index.php file:

nano /var/www/vhosts/video.dingleberrypi.com/index.php

Now type the following into Nano, save and exit:

PHPinfoGive it a go, visit your subdomain and you should see the PHP info. Replace the index.php file with your subdomain or virtual host, and that’s it. Just in case you wondered what video is on my video.dingleberrypi.com it’s below!

Leave a comment below (or on the video if you really want to). Your feedback and thoughts always welcome!

 

 

Ste

Web Developer living in Manchester, working for Studio Skylab (http://www.studioskylab.com). Views and thoughts are my own.

You may also like...

17 Responses

  1. JohnMar says:

    Hey, is it possible to put the /vhosts/subdomain.domain.com directory in /var/ instead of /var/www/ ? I want to be able to edit /var/www/ sometimes, but I accidentally delete vhosts folder, so I want the vhosts in a different place, is that possible?

  2. Peter Fielden-Weston says:

    Well, that was fun! 🙂

    And it was well worth the effort. I have six sites/sub-domains that I grew one at a time and didn’t really put in any thought to their coexistence. And I am now reaping the ‘benefits’ of that lack of decision.

    So I have a couple of Raspberry Pi’s loafing about not doing much so I (actually) thought, why not use one of them as a local webserver and test machine. The problem was going to be sorting out all of my junk, err… the products of a lot of hard work, into a reasonable and sensible structure.

    So I needed virtual hosts. I can set these up on my host provider, but I want to get everything sorted out first. So this tutorial has been invaluable. I have just proved that each vhost responds to its own requests either in HTML or PHP. My next task is to copy the raw sites over to the vhosts and fing out how much work I need to put in to convert from my old PHP version to PHP 5.4. Now that should be fun too.

  3. Maarten says:

    I made a /domains folder in /var/www, but when i connect with ftp i see the /domains folder. How can i set the folder where it opens to /var/www/domains?

  4. Alex says:

    Great tutorial, works like a charm! Thank you

  5. Christoph says:

    Thank you so much for this post. This melted my head!

  6. Arik Savage says:

    I followed your tutorial. Woorks great, except one thing:
    http://siteurl.tld goes to the content /var/www/vhosts/siteurl.tld but
    http://www.siteurl.tld shows the index file in my /var/www/

    I’ve gone through enabling mod-rewrite and allowOverride ALL in but I can’t get the www pointing to the vhosts files. DNS is all correct (CNAME for www to @)

    Any suggestions would be greatly appreciated!

    • koen says:

      In case you didn’t find a solution yet or anyone else wonders :
      ServerName siteurl.tld
      ServerAlias http://www.siteurl.tld

      If apache doesn’t find a ServerName or ServerAlias it will display the first vhost it encounters, thus “default” which points to /var/www/

  7. Rudi Opperman says:

    Ok I am a bit lost. Sorry I am very new to this. I have a site up and running. Its in the default www/index.html. I followed your instructions but when I came to the park where “cp /etc/apache2/sites-available/default /etc/apache2/sites-available/video.dingleberrypi.com” must be used I replaced the second part with /var/www because that is the folder where my site was running currently, was this correct? Then I continued to the part where you did “a2ensite video.dingleberrypi.com”. but I replaced it with a2ensite mysite.mysite.com but gave the error saying that the site does not exist. Where did I go wrong?

  8. painting says:

    I like this mannual. And finally succeeded! As I don’t use “sudo bash” command, so I need to use “sudo” for other commands. So when I used “service apache2 restart”, it always failed with error. Then I used “sudo service apache2 restart”, then it is ok now. Thank you!

  9. Frank says:

    I’m following this exactly as you write here, and I’ve tried three times, but every time there are no errors or warnings, but still myvirtual site opens up the default site.

  10. fiddlestixbo says:

    to work on 2016 PI i needed to suffix the vhost files with “.conf”
    ie; /etc/apache2/sites-available/video.dingleberrypi.com.conf

  11. cplusplus says:

    NOP, doesn’t work.
    The domai does, the subdomain doesn’t load in the browser. No error message. Nothing in the logs
    This is what i LOVE about networking, apache etc. NO F-IN ERROR MESSAGE ever!!

    But I’m just a c++ programmer …

  1. 28th August 2013

    […] Ste W If you’re feeling brave, you might want your Raspberry Pi powered web server to host more than […]

  2. 10th December 2014
  3. 7th September 2016

    […] Add virtual hosts and subdomains to your Raspberry Pi … – If you’re feeling brave, you might want your Raspberry Pi powered web server to host more than one website or subdomain. Providing you’ve made the necessary … […]

  4. 3rd October 2016

    […] Add virtual hosts and subdomains to your Raspberry Pi … – If you’re feeling brave, you might want your Raspberry Pi powered web server to host more than one website or subdomain. Providing you’ve made the necessary … […]

Leave a Reply