Yes, you can enable Apache’s UserDir module on RHEL, CentOS, and Fedora by editing the userdir.conf file and adjusting SELinux booleans if needed. This guide walks through the exact steps for current versions of these distributions, including how to set per-user public directories and troubleshoot common issues.
What is the UserDir Module?
Apache’s mod_userdir lets each user on a system publish files from a directory inside their home folder, typically ~/public_html. When enabled, a request like http://server/~username/ serves files from that user’s public directory. This is a simple way to host personal web pages without giving users access to the main document root.
On RHEL-family systems, the module is compiled into Apache but disabled by default. The configuration file /etc/httpd/conf.d/userdir.conf contains the directives controlling it, and it is included automatically when Apache starts.
Prerequisites
Before enabling UserDir, ensure Apache is installed and running. On Fedora, the package name is httpd; on RHEL and CentOS 8+ it is also httpd (on CentOS 7 it was httpd as well). Install it with:
sudo dnf install httpd # Fedora/RHEL 8+
sudo yum install httpd # CentOS 7
Start and enable the service:
sudo systemctl enable --now httpd
Verify it is running with systemctl status httpd.
Step-by-Step: Enable UserDir
Edit the configuration file /etc/httpd/conf.d/userdir.conf with a text editor as root. The file contains a commented example. Uncomment or add the following lines:
UserDir enabled
UserDir public_html
The UserDir enabled directive turns on the module globally. The second line sets the name of the directory inside each user’s home that will be served. You can also enable it for specific users only, e.g., UserDir enabled alice bob.
Next, ensure the directory block is present. The default file usually includes:
<Directory "/home/*/public_html">
AllowOverride FileInfo AuthConfig Limit Indexes
Options MultiViews Indexes SymLinksIfOwnerMatch
Require all granted
</Directory>
This grants access to the public_html directories. If you want more restrictive access, change Require all granted to something like Require host example.com.
Save the file and test the configuration:
sudo apachectl configtest
If the output says Syntax OK, reload Apache:
sudo systemctl reload httpd
Create a User’s Public Directory
For each user who wants a website, create the directory and set proper permissions:
sudo mkdir /home/username/public_html
sudo chown username:username /home/username/public_html
sudo chmod 755 /home/username
The home directory must be traversable (execute permission for others) so Apache can reach the public_html folder. The public_html itself should be readable and executable by others (e.g., 755). Add an index file:
echo '<h1>Hello from username</h1>' | sudo tee /home/username/public_html/index.html
Now test by visiting http://server/~username/ in a browser. If you get a 403, check SELinux (next section).
SELinux Configuration (RHEL/CentOS/Fedora)
These distributions enforce SELinux by default. The relevant boolean is httpd_enable_homedirs, which is off by default. Enable it temporarily:
sudo setsebool -P httpd_enable_homedirs 1
The -P flag makes the change persistent across reboots. Also, the user’s home directory and public_html need the correct SELinux file context. Apache will only serve files labeled with httpd_user_content_t. To set this, run:
sudo semanage fcontext -a -t httpd_user_content_t "/home/username/public_html(/.*)?"
sudo restorecon -Rv /home/username/public_html
If semanage is not installed, install it with sudo dnf install policycoreutils-python-utils (or policycoreutils-python on CentOS 7).
If you still get permission errors, check the audit log: sudo grep httpd /var/log/audit/audit.log for denials.
Common Problems and Fixes
- 403 Forbidden: Often SELinux or file permissions. Verify booleans and file contexts as above. Also ensure the home directory has execute permission for others (
chmod 755 /home/username). - 404 Not Found: The user directory might not be enabled for that user, or the public_html directory is missing. Check the spelling and that the directory exists.
- Apache won’t start: Syntax error in userdir.conf. Run
apachectl configtestand fix any reported issues. - UserDir not working for root: By default, UserDir is disabled for the root user. You can enable it by adding
UserDir enabled rootbut it’s not recommended for security. - Symbolic links not followed: The
Optionsdirective in the Directory block may needFollowSymLinksif you use symlinks inside public_html.
Security Considerations
Allowing users to publish web content can introduce risks. Users might accidentally expose sensitive files if they place them in public_html. Limit what users can do by adjusting the AllowOverride and Options directives. For example, disable CGI execution unless needed:
Options MultiViews Indexes SymLinksIfOwnerMatch
RemoveHandler .cgi
Also, consider using Require all denied and then Require user username for specific users if you want to restrict access. Always keep the module disabled if you don’t need it.
Alternatives to UserDir
If you need more control or virtual hosting, consider using separate VirtualHosts per user. This allows each user to have their own domain or subdomain, and you can set up PHP or other modules per user. Another option is to use a web-based file manager or a CMS that handles user uploads. UserDir is best for simple static pages or small personal sites.
Frequently Asked Questions
Why do I get a 403 error even after setting permissions?
Most often it’s SELinux. Check the boolean httpd_enable_homedirs and the file context. Run ls -Z /home/username/public_html to see the context; it should show httpd_user_content_t. If not, run the restorecon command.
Can I use a different directory name than public_html?
Yes. Change the UserDir directive in userdir.conf to any name, e.g., UserDir web. Then create that directory in each user’s home and update the Directory block path accordingly.
How do I enable UserDir for only one user?
Use UserDir enabled username instead of just enabled. You can list multiple users separated by spaces.
Does this work on CentOS 7 with Apache 2.4?
Yes, the steps are the same. CentOS 7 uses Apache 2.4 and the same configuration file location. The only difference is the package manager (yum instead of dnf).
Can I run PHP scripts in public_html?
Yes, if PHP is installed as an Apache module (mod_php) or via FastCGI. You may need to add AddHandler php-script .php or ensure the PHP module is loaded. On Fedora, install php and php-fpm and configure accordingly.
How do I disable UserDir again?
Edit userdir.conf and comment out or remove the UserDir enabled line, then reload Apache. That effectively disables the module.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Photoshop vs Lightroom AI Features Compared
- DNF vs DNF5: Which Package Manager?
- Car Trends Making Cars Worse: 7 Downsides
- Nostalgic Classic Games Revamped on G2A?
- Nvidia RTX 4000 Super GPUs: Full Specs & Buying Guide
- Korean SUV Making BMW and Audi Owners Question Their Choices?