The mindless ramblings of a bow shooting network engineer.
Apache and User Directories

Apache and User Directories

When I’m developing websites, I usually run a test website in  a user directory on one of my web servers instead of setting up a new virtual host under the main web server. There are a couple of reasons for this, the main one is I don’t have to change anything in my httpd.conf. The web server is already set up with the right config to allow content in user directories, so all I have to do is create a new user.

The one thing I always struggle with, is the correct permissions on the public_html folder in the user directory. I can never seem to remember the correct process, if you add to this all the complications of SELinux, I always end up googling the solution. There are plenty of good articles out there on the internet already, but I can never seem to find them when I need them. That’s why I decided to write this post…

As I already said, my apache servers are usually already set up to server content from user home directories, but I’ve included the steps here for completeness.

Edit the /etc/httpd/conf/httpd.conf file and find the UserDir section.

  1. Comment out the UserDir disabled line
  2. Uncomment the ‘UserDir public_html’ line.
  3. Also uncomment the whole ‘<Directory /home/*/public_html>’ section until the ‘</Directory>’.

When you’re finished, your httpd.conf should look like the one below:

 # UserDir disabled

 # To enable requests to /~user/ to serve the user's public_html
 # directory, remove the "UserDir disabled" line above, and uncomment
 # the following line instead:
 UserDir public_html

 <Directory /home/*/public_html>
    Options FollowSymLinks
    AllowOverride FileInfo

Now we need to make sure we have the correct permissions on the users home area. Let’s make sure the home area is only readable by the owner.

 # chmod 711 ~<username>

Now we’ll give apache the correct rights to read the users public_html folder.

# chmod 755 -R ~<username>/public_html/

Last of all we need to take care of SELinux to make sure apache can actually get the files from the user home directories.

# chmod 755 -R ~<username>/public_html/
# chcon -R -t httpd_sys_content_t ~<username>/public_html

If you made changes to the httpd.conf file, restart your web server.

#service httpd restart

That’s it, apache should now server pages from your user public_html folder.

I hope this saves you some of the headaches I’ve experience setting this stuff up…


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.