Enable Nginx Userdir on Ubuntu 24.04

The article details enabling the Nginx Userdir feature on Ubuntu 24.04, allowing users to host websites from their home directories. It contrasts Nginx with Apache, outlines configuration file modifications, emphasizes necessary directory permissions for security, and describes testing the feature by creating a basic HTML page accessible via a specific URL.

This article explains how to enable the Nginx Userdir feature on Ubuntu 24.04.

Userdir is a feature in the Apache HTTP Server that allows users to have web directories. When enabled, users can create content in their home directories to host their web pages.

Unlike Apache, Nginx doesn’t support modules for Userdir. However, you can configure settings to enable the experience in Nginx environments.

Nginx Userdir empowers users to host their websites without needing a full-fledged server or administrative access to the main server.

Users can have their directory (usually under ~username/public_html), and by accessing http://yourserver/~username, they can view their files.

Enable Userdir

With Nginx installed, run the command below to open the default web settings configuration file.

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

Then, edit the file and add the highlighted lines under the [server] block.

server {
listen 80 default_server;
listen [::]:80 default_server;
#
root /var/www/html;
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;
}


.........
........

Restart Nginx once enabled to apply your changes.

sudo systemctl restart nginx

There is nothing else to configure once the feature is enabled.

Test Nginx Userdir

You can test this Nginx feature by creating an HTML document in your home directory and accessing it via a web browser.

First, run the command below to create a ‘public_html‘ document root in your home directory.

mkdir ~/public_html

Update the permissions on the directory to ensure they are secure.

chmod 711 $HOME
chmod 755 ~/public_html

Next, run the command below and add a basic HTML ‘index.html‘ file.

nano ~/public_html/index.html

Copy and paste the lines below into the file and save.

<html>
<title>My basic HTML page</title>
<body>
<p style="width: 100%; font-weight: bold; font-size: 60px; text-align: center;">
UserDir is enabled!
</p>
</body>
</html>

Save the file and exit.

Finally, open your web browser and browse the file in your home directory using the format below.

http://example.com/~richard/

Add the ~username after the server hostname or IP address.

That should do it~

Conclusion:

Enabling the Nginx Userdir feature on Ubuntu 24.04 provides users with a convenient way to host their personal web pages without requiring administrative access. Here are some key points to remember:

  • User-Friendly: Allows users to manage their web content from their home directory easily.
  • Configuration: Requires modification of the Nginx configuration file to include the necessary location block.
  • Permissions: It’s essential to set appropriate permissions for the public_html directory to ensure security.
  • Testing: A simple HTML file can be created to verify that the Userdir feature is working correctly.
  • Access: Users can access their web pages through a simple URL format (http://yourserver/~username).

By following these steps, users can effectively leverage Nginx’s capabilities to host their personal websites.

Comments

Leave a Reply

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

Exit mobile version