Setting up an Apache Web Server with per user Directories on Raspberry Pi

As part of the Code Club that I run at Windhill21 primary school we are currently learning how to develop web sites using HTML, CSS and a little JavaScript following the appropriate set of projects from the organisation.

I wanted to set up a web server that the children in the club could upload their creations too. Since the school has a number of Raspberry Pi’s, I thought it would be a cool idea to set one of them up to host a web server.

I wanted each of the children to have their own account on the Raspberry Pi that they could upload their files to and their own URL to access them from.

i.e. “johnny” would have an account they could log onto and then upload their files into the /home/johnny/public_html directory using FTP directly or a tool such as Microsoft WebMatrix and would then be able to access them at http://www.myservername.com/~johnny.

This proved to be quite time consuming so I wanted to present a step-by-step guide to how I achieved it.

Step-by-Step Guide

  1. Install Apache
    • sudo apt-get install apache2 -y
  2. Configure Apache to Use Per-User Directories
    • ln -s /etc/apache2/mods-available/userdir.conf /etc/apache2/mods-enabled/userdir.conf
    • ln -s /etc/apache2/mods-available/userdir.load /etc/apache2/mods-enabled/userdir.load
    • # reboot the server
      sudo apachectl restart
  3. Add Users (e.g. user bob) – you might want to write a script for this
    • # add the bob user
      sudo adduser bob (you will be asked to enter some basic information and a password for bob)
    • # make a directory to store web files in
      mkdir /home/bob/public_html
    • # add bob to the www-data group which Apache is part of
      sudo adduser bob www-data
    • # change the default ownership and permissions of public_html
      # set the ownership to user bob and group www-data
      sudo chown -R bob:www-data /home/bob/public_html
      # set the permissions to read-write for bob and read-only for www-data
      sudo chmod -R 750 /home/bob/public_html
    • # set new files to be owned by the www-data group
      sudo chmod g+s /home/bob/public_html

Now, if “bob” logs on to the Raspberry Pi and adds new files to /home/bob/public_html they will be created with the correct permissions to allow bob to edit them and for the Apache web server to read (and hence serve them).

Hopefully this may save someone else some time in the future.

One thought on “Setting up an Apache Web Server with per user Directories on Raspberry Pi

Leave a comment