How to Enable Nginx Userdir on Ubuntu 24.04 Easily
Nginx Userdir on Ubuntu 24.04 lets you serve websites directly from a user’s home folder, usually a folder named public_html.
Configure Nginx by editing the default site file to add a location block for user directories. After saving the configuration, restart the Nginx service to apply the changes. Test by creating a `public_html` folder in your home directory and adding an `index.html` file.
This feature allows individuals to host their own web pages without needing full server control. It’s similar to how Apache handles user directories.
Enabling Nginx Userdir is handy for shared hosting or when you want to make it simple for people to put their websites online.
- Open the default Nginx site configuration file by running:
sudo nano /etc/nginx/sites-available/default - Add the following lines inside the
serverblock to set up user directories:Codelocation ~ ^/~(.+?)(/.*)?$ { alias /home/$1/public_html$2; index index.html index.htm; autoindex on; } - Save the file and close it.
- Restart Nginx to apply the changes with:
sudo systemctl restart nginx - Create a folder named
public_htmlin your home directory:mkdir ~/public_html - Set the correct permissions for this folder:
chmod 755 ~/public_html - Create a simple
index.htmlfile inside your new folder:nano ~/public_html/index.html - Add some basic HTML content, like:Code
<html><title>My Userdir Page</title><body><h1>Hello from my userdir!</h1></body></html>
- Save and close the
index.htmlfile. - You can now see your page by going to
http://your_ip_address/~your_username/in your browser.
Enable Userdir
Enabling Nginx Userdir on Ubuntu 24.04 lets you host websites directly from your home folder. You’ll modify the main Nginx configuration file to tell the server to look for web files inside each user’s ‘public_html’ directory. This involves opening the default Nginx site configuration and adding a few specific lines to make user directories work.
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;
}
.........
........sudo systemctl restart nginx
There is nothing else to configure once the feature is enabled.
Test Nginx Userdir
Testing your Nginx Userdir setup on Ubuntu 24.04 confirms that your website hosted from a home folder is accessible. You'll create a 'public_html' folder, set its permissions correctly, and place a basic HTML file inside. This simple test verifies that Nginx can find and display your page, showing that Nginx Userdir is working as expected.
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.
You can now view your personal web pages by opening your web browser and entering your IP address followed by your username and "userdir" in the address bar, for example: `http://your_ip_address/~your_username/userdir`.
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
locationblock.
- Configuration: Requires modification of the Nginx configuration file to include the necessary
-
- Permissions: It's essential to set appropriate permissions for the
public_htmldirectory to ensure security.
- Permissions: It's essential to set appropriate permissions for the
-
- 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).
- Access: Users can access their web pages through a simple URL format (
Nginx userdir lets you host personal websites by serving files from your home directory. This feature simplifies sharing your projects online after following the setup steps.
No comments yet — be the first to share your thoughts!