Ubuntu 16.04 / Debian 8: Install apache2 userdir and run web server for each user

This article will describe installing apache2 userdir and running web server for each user.

 

1 Install apache2

Install apache2 with apt.

$ sudo apt-get install -y apache2

2 Enable userdir module

Enable userdir module with a2enmod.

$ sudo a2enmod userdir
$ sudo systemctl restart apache2

3 Create ~/public_html directory

$ mkdir ~/public_html

Now accessing to below URL returns below HTML. If you need to provide file downloader, you only put your file to public_html directory.

http://<server>/~<username>

0001_FileList.png

A index.html or index.cgi will be loaded by DirectoryIndex when accessing to URL.

I have created below index.html written by Japanese with emacs org-mode.

0002_IndexHTML.png

4 Digest authentication

Enable auth_digest module with a2enmod.

$ sudo a2enmod auth_digest
$ sudo systemctl restart apache2

Create public_html/.htaccess as below. "hiroom2" is a realm for digest authentication.

AuthType Digest
AuthName "hiroom2"
AuthUserFile /home/hiroom2/.htdigest
require valid-user

Add user to accessing to realm "hiroom2" with htdigest.

$ htdigest -c ~/.htdigest "hiroom2" hiroom2
Adding password for hiroom2 in realm hiroom2.
New password:
Re-type new password:

Username and password is required when accessing to URL.

0003_Password.png

5 CGI

A userdir module does not allow ExecCGI by default.

It may be better to use container like LXC for ExecCGI.

 

Add ExecCGI to Options in userdir.conf.

$ diff -uprN /etc/apache2/mods-available/userdir.conf{.org,}
--- /etc/apache2/mods-available/userdir.conf.org        2016-05-11 07:22:10.861339651 +0900
+++ /etc/apache2/mods-available/userdir.conf    2016-05-11 08:45:45.955093386 +0900
@@ -4,7 +4,7 @@

        <Directory /home/*/public_html>
                AllowOverride FileInfo AuthConfig Limit Indexes
-               Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
+               Options ExecCGI MultiViews Indexes
                SymLinksIfOwnerMatch IncludesNoExec
                <Limit GET POST OPTIONS>
                        Require all granted
                </Limit>

Enable cgi module with a2enmod.

$ sudo a2enmod cgi
$ sudo systemctl restart apache2

Create public_html/.htaccess as below.

AddHandler cgi-script .cgi

This article created public_html/index.cgi as below and ran "chmod a+x public_html/index.cgi".

#!/bin/sh

echo "Content-type: text/html"
echo ""
echo "hello"

Accessing to URL returned HTML generated by index.cgi.

0004_IndexCGI.png