How to set up Apache UserDir on Ubuntu Linux

|

|

The post provides a step-by-step guide on setting up the UserDir feature with Apache HTTP server on Ubuntu Linux – a useful method for school environments where teachers need to publish materials online. Upon enabling UserDir, users can share content from their home directories via Apache. The process involves installing the Apache2 HTTP Server, enabling…

This post describes the steps to set up the UserDir feature with the Apache HTTP server in Ubuntu Linux.

In some school environments, you’ll see examples where teachers are given their website to publish materials for students to access. Instead of creating multiple server blocks, the system administrator can use the UserDir feature.

The URL to reach the teacher’s page is usually the domain name followed by /~teacher_name.

User Directory, or UserDir for short, is a feature for Apache web servers that allows user-specific directories to be accessed via Nginx. 

For example, when you enable this feature in Apache, users with accounts on the system can share content in their home directories with the world via Apache.

This tutorial assumes that you already have an Apache2 web server installed. If you haven’t, you may want to do that before continuing below.

Install Apache2 HTTP Server

First, run the commands below to install Apache2 HTTP Server on Ubuntu

sudo apt install apache2

Enabling UserDir on Apache2

To enable the Userdir module for Apache2 webservers, run the commands below.

sudo a2enmod userdir

After running the commands above, the feature will be enabled and ready to be used. The configuration file is at /etc/apache2/mods-enabled/userdir.conf. But you don’t have to do anything. It’s already configured with the best options.

To access the config file, run the commands below

sudo nano /etc/apache2/mods-available/userdir.conf

Then validate the settings below:

<IfModule mod_userdir.c>
     UserDir public_html
     UserDir disabled root

        <Directory /home/*/public_html>
             AllowOverride FileInfo AuthConfig Limit Indexes
             Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
             Require method GET POST OPTIONS
        </Directory>
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Creating User Directories

Now that the feature is enabled, all users have to do is run the commands below to create a folder in their home directories called public_html by running the commands below.

mkdir ~/public_html

In the ~/public_html folder, create html documents to be shared and accessed via the web server.

Restart the Apache2 webserver to load the settings.

sudo systemctl restart apache2.service

Test it by browsing the server hostname or IP address followed by the username.

example: http://example.com/~richard

This is how the Apache2 Userdir module is configured for users to allow users with accounts on the systems to share content from their home directories.

That’s it!

Like this:



3 responses to “How to set up Apache UserDir on Ubuntu Linux”

  1. Chris Avatar
    Chris

    I tried your setting, but PHP and htaccess doesn’t by this way.
    PHP work correctly in the localhost (phpmyadmin work, phpinfo work). But PHP doesn’t work in the home directory !?
    What’s wrong ???
    Thanks

  2. objectswork Avatar
    objectswork

    Serving PHP files from user’s public_html directoriy is disabled by default for security reasons. If you want to enable PHP processing when using userdir this is what you need to do. First you edit following Apache configuration file in this way:

    sudo nano /etc/apache2/mods-available/php5.conf
    Now you need to comment out a few lines from to the next so you get something like this:

    SetHandler application/x-httpd-php

    SetHandler application/x-httpd-php-source

    # To re-enable php in user directories comment the following lines
    # (from to .) Do NOT set it to On as it
    # prevents .htaccess files from disabling it.
    #
    #
    # php_admin_value engine Off
    #
    #

    Last thing you just restart Apache web server and create public_html directory inside your home directory like this:

    sudo service apache2 restart
    mkdir /home/$USER/public_html

    That’s it and every user on your PC should be able to serve PHP pages from his /home/username/public_html directory, and that pages should be accessible on http://127.0.0.1/~username/ address. Now you can develop for web using public_html to store and test your PHP and HTML code.

  3. rjohnson Avatar
    rjohnson

    So funny. You had to repeat exactly what the post said because the person is complaining about his inability to follow directions. Worse than trolls.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.