How to Enable Apache Userdir Module on CentOS

Step 1: Install Apache HTTP Server

Install the Apache

# yum install httpd

Use the systemd systemctl tool to start the Apache service:

 systemctl start httpd 

Enable the service to start automatically on boot:

systemctl enable httpd.service

Open up port 80 for web traffic:

firewall-cmd --add-service=http --permanent

Reload the firewall:

firewall-cmd --reload

Step 2: Enable Apache Userdirs

# vi /etc/httpd/conf.d/userdir.conf
UserDir enabled
UserDir public_html

<Directory "/home/*/public_html">
    ## Apache 2.4 users use following ##
    AllowOverride FileInfo AuthConfig Limit Indexes
    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
    Require method GET POST OPTIONS

## Apache 2.2 users use following ##
        Options Indexes Includes FollowSymLinks        
        AllowOverride All
        Allow from all
        Order deny,allow
</Directory>
systemctl restart httpd.service 

Step 3: Creating User Directories

# mkdir /home/centos/public_html
# chmod 711 /home/centos
# chown centos:centos /home/centos/public_html
# chmod 755 /home/centos/public_html

Step 4: Create testing file in Userdir

Create /home/centos/public_html/index.html file with the following content.

<html>
  <head>
    <title>This is the testing HTML </title>
  </head>
  <body>
    <h1>Success!!</h1>
  </body>
</html>

Step 5: Creating .htaccess

# sudo vi /home/centos/public_html/.htaccess

Open the .htaccess file and enter:

# serve custom error pages
ErrorDocument 404 "Oops! We can't find that file. Sorry."

Step 6: Config SELinux 

Also, set correct SELinux context for Apache homedirs (httpd_enable_homedirs).

# setsebool -P httpd_enable_homedirs true
# chcon -R -t httpd_sys_content_t /home/centos/public_html

Step 7 : Testing

Change to the user/public_html directory and display the contents of the directory and then display the .htaccess file

cd /home/centos/public_html
ls -al
cat .htaccess

Open a browser and display the index.html file

http://serverip/~centos/

Leave a Reply

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