How to Enable Userdir for Apache2 / Nginx on Ubuntu 17.04 | 17.10

User Directory or Userdir for short is a module for Apache2 web server that allows user-specific directories to be accessed via Apache2. This can able be configured Nginx webservers, although no modules to enable.

For example, when you enable this feature in Apache2 or Nginx web servers, users with accounts on the system will be able to share content in their home directories with the world using the webserver.

This brief tutorial shows students and new users how to enable and configure it on Ubuntu 17.04 | 17.10 using Apache2 or Nginx.

To get started with enabling Userdir on Ubuntu, continue with the steps below:

This tutorial assumes that you already have Apache2 or Nginx webserver installed. If you haven’t, you may want to do that before continuing below.

Step 1: Enabling Userdir on Apache2

To enable this feature on 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.

<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

Step 2: 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 that folder, create html documents to be shared and accessed via the webserver.

Restart Apache2 webserver to load the settings.

sudo systemctl restart apache2.

Now test it out by browsing to the server hostname or IP address followed by the username.

example: http://192.168.71.136/~richard

apache2 userdir feature

Step 3: Enable Userdir on Nginx Webservers

For Nginx webservers, there are no modules to install. All one has to do is add the block of code into the default site configuration file inside the server block.

sudo nano /etc/nginx/sites-available/default

Then add the highlighted lines inside the server block and save.

# Default server configuration
#
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;
        server_name _;

         location ~ ^/~(.+?)(/.*)?$ {
         alias /home/$1/public_html$2;
         index index.html index.htm;
         autoindex on;
           }

}

Then restart Nginx webserver by running the commands below.

sudo systemctl restart nginx

Next, have users create a folder in their home directories called public_html. Then create an index.html page and save. Browse to the server hostname or IP address followed by the user name.

example: http://192.168.71.136/~richard

That’s it!

This is how to enable users to share content from their home directories to the world.

Enjoy!

You may also like the post below:

Install phpMyAdmin, Apache2, MariaDB and PHP on Ubuntu 17.04 / 17.10

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.