Enable userdir Apache module on Ubuntu Linux and other Debian based distributions

Apache logoLately I've spent a lot of time on web programming using open source based technologies like PHP, MySQL and PostgreSQL. This works really great on Linux because every tool you need for web programing is right there a few keystrokes away using your favorite distribution and package manager. One essential thing you need to setup for web programming is localhost web server serving files inside your home directory. I've wrote about one method of making Apache web server work this way in my following article:

LAMP development in your home directory with suPHP module

In this article I will show you how to do the same thing in another way by enabling Apache module called userdir on Debian based distributions like Ubuntu. The official definition of userdir is that "this module allows user-specific directories to be accessed using the http://example.com/~user/ syntax". So lets get started...

In this article I will assume you have Apache web server fully installed. If you haven't already done this please follow the link from the beginning of this article and come back to enable userdir Apache module.

For starters we need to enable this module using following command:

sudo a2enmod userdir

Now we will configure Apache module userdir by editing userdir.conf file like this:

sudo nano /etc/apache2/mods-enabled/userdir.conf

Next you should replace the contents of that configuration file with the following code:

<IfModule mod_userdir.c>
        UserDir public_html
        UserDir disabled root
        <Directory /home/*/public_html>
		AllowOverride All
		Options MultiViews Indexes SymLinksIfOwnerMatch
			# Apache <= 2.2:
		        Order allow,deny
		        Allow from all
		        # Apache >= 2.4:
		        #Require all granted
		<LimitExcept GET POST OPTIONS>
			# Apache <= 2.2:
		        Order deny,allow
		        Deny from all
			# Apache >= 2.4:
			#Require all denied

With this setup Apache web server would serve HTML files from public_html directory of every user on your pc. This HTML files would be accessible using the http://example.com/~user/ syntax. Serving PHP files from user's public_html directoriy is disabled by default for security reasons. If you want to enable PHP processing when using userdir this is what you need to do. First you edit following Apache configuration file in this way:

sudo nano /etc/apache2/mods-available/php5.conf

Now you need to comment out a few lines from <IfModule mod_userdir.c> to the next </IfModule> so you get something like this:

<IfModule mod_php5.c>
    <FilesMatch "\.ph(p3?|tml)$">
	SetHandler application/x-httpd-php
    <FilesMatch "\.phps$">
	SetHandler application/x-httpd-php-source
    # To re-enable php in user directories comment the following lines
    # (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
    # prevents .htaccess files from disabling it.
    #<IfModule mod_userdir.c>
    #    <Directory /home/*/public_html>
    #        php_admin_value engine Off
    #    </Directory>

Last thing you just restart Apache web server and create public_html directory inside your home directory like this:

sudo service apache2 restart
mkdir /home/$USER/public_html

That's it and every user on your PC should be able to serve PHP pages from his /home/username/public_html directory, and that pages should be accessible on address. Now you can develop for web using public_html to store and test your PHP and HTML code. Have fun coding!


A quality focused Magento specialized web development agency. Get in touch!

30 thoughts on “Enable userdir Apache module on Ubuntu Linux and other Debian based distributions

  1. Neville Cox

    I have just installed Ubunter server 11.04 with lamp and can’t load files to my web page as I can’t see the public_html file to point it to.

    So if I follow what you have stated above it will work?
    So where do I start say my user name is bob, where do I start entering the commands

    # sudo a2enmod userdir

    Do I start at the root as SU? as I’m a newbee to this


  2. Marko Author

    If you have Ubuntu server you start with “sudo a2enmod userdir” after you login to your console (Ubuntu server has no GUI only CLI (Command Line Interface). So when your server is turned on it first asks for your login name and password. Once Ubuntu greets you with welcome message you just enter “sudo a2enmod userdir” and follow the rest of this article. You have to have LAMP server installed first (you can follow Ubuntu Netbeans and LAMP server with Xdebug as non-root user or any other tutorial from Google to install LAMP). Cheers!

    Edit: On Ubuntu if you want to do something that requires root privileges you just prefix that command with “sudo” for CLI apps and “gksudo” for GUI apps. On other Linux distros you need to first enter “su“, and then you become permanently root until you resign root privileges with “exit” command. Also if you don’t have GUI you wouldn’t use gedit (it is GUI program), you would use CLI text editor like nano (“sudo nano” instead of “gksudo gedit”).

  3. Neville Cox

    Thanks for that, it looks like it will work.
    Only problem is I go to the# cd /etc/apache2/mods-enabled and then type in vi userdir.conf and the con fig file opens ok, I make the changes but when I go to save changes I get a error

    “userdir.conf” E166: Can’t open linked file for writing
    Press ENTER or type command to continue.

    Many Thanks


  4. Marko Author

    @Neville Cox

    Hi! When you do “vi userdir.conf” you should do that like root using sudo like this “sudo vi userdir.conf”. Vi is complaining that it can’t write to file because it is owned by root and Vi is running as non root (haven’t been started with sudo). In the article I’m using GUI editor “gedit” and I’m starting it with “gksudo” which is GUI version of “sudo”. Also it would be easier for you to use “nano” editor, but Vi is the most powerful one.

  5. Neville Cox

    Sorry for the inconvenience but I was able to get both .conf files edited with no problems.
    But when you say create public_html directory inside your home directory like this:
    mkdir /home/$USER/public_html, I typed this at the root prompt
    # mkdir /home/Bill/public_html, when you say home what do you mean, Sorry about this but I think this is the last step I need
    The user mane is bill

  6. Marko Author

    @Neville Cox

    No inconvenience, I like to help when I can πŸ˜‰ Linux environment has some environment variables defined by the default. One of those is $USER so when you use $USER somewhere it would be replaced with users login name. This is the same as name of users home directory inside home directory. So instead of writing in this article “mkdir /home/your_user_name/public_html” and pointing out that every user should replace “your_user_name” with his name I’ve simply wrote “mkdir /home/$USER/public_html”. To see every environment variable you can use command “printenv”, you will see that one of them is USER=Bill so you could also write “mkdir /home/Bill/public_html” it would have same effect as “mkdir /home/$USER/public_html”.

  7. Neville Cox

    When I added this user bill i used the commands
    # cd /var/www
    # mkdir bill
    #useradd bill -p “my-root password” -d /var/www/bill -s /bin/false
    # chown bill bill
    # passwd bill
    then entered the user password. So I would enter this command at the www level is that Like
    www # β€œmkdir /home/Bill/public_html”. Is that correct.

    Thanks Marko

  8. Marko Author

    @Neville Cox

    You shouldn’t create new user with home on /var/www. The whole point of this article is that you can serve web pages without access to /var/www as regular user created during Ubuntu installation. Idea is that you make Apache web server serve pages from every users public_html directory (owned by user whose that home directory is).

    As for creating directories you should cd into the directory where you want to create new directory (cd /home/Bill/ in this case). You can also make “mkdir” to create all path of directories with -p switch like this “mkdir -p /home/Bill/public_html” but without Bill user account creating Bill inside “/home” would do you no good. “/home/Bill/” will be created when you create new user using “adduser” command (Notice that “useradd” and “adduser has subtle difference). On Debian based distributions it is recommended that you use “useradd” instead of “adduser” command like I’ve done in this article.

    What I would recommend is that you do fresh Ubuntu install and choose Desktop Ubuntu because it has GUI (that is the only difference between Server and Desktop). Then you do this article logged in as Bill user created during installation. If you don’t want to do that you need to remove Bill user you created using “useradd”and creating same user using “adduser” but “useradd” could create a mess on Debian based distros so things might not even then work. Hope you make it, any problems just post here and I will help when I can, good luck πŸ˜‰

  9. Neville Cox

    Many thanks Marko
    So far every one els I ask seem to not be intrested and most of the time the given a bumb steer and so far I’v had to reload a number of times with no luck.
    So I thank you once again. I will try the the Desktop version.
    If I do this do I need to install Lamp, vsftpd, telnet, webmin, Webalizes and phpmyadmin after the install or dose it come with the package?


  10. Marko Author

    @Neville Cox

    The problem with Linux community is that the more Linux user knows the less he wants to share because he had to work hard to know that much because other users that know stuff don’t want to share for the same reason. It is a circle I’m hoping to break πŸ˜‰

    As for the software you ask yes it is all there and can be installed using apt-get or any other similar GUI or CLI tool. The difference between Server and Desktop is only in default installed software. By adding and removing software from software repositories (sudo apt-get install) you can easily turn Desktop to Server and the other way around. Server by default has no GUI but if you don’t host to many users on very old PC GUI doesn’t mind at all, on the other hand it helps if you aren’t very skilled with CLI.

    Edit: Most packages you need will be installed using “sudo apt-get program_name”. On Ubuntu secure telnet equivalent would be SSH. openssh-client is installed by default, and server is installed using “sudo apt-get install openssh-server” then you ssh to your pc using something like “ssh Bill@IP_ADDRESS”. For hosts in local network you do “ssh Bill@hostname.local“.

  11. Neville Cox

    Thanks for that I have installed again as server only with openssh and no lamp this time and I’ going to go from there with your if you can, once I have got it working, I too will be out there helping others.
    So I think the next thing to do is to install Lamp but I think there is a bit more than that.

    Thanks Mate

    Where are you located


  12. Neville Cox

    I have once again maid a clean install of Ubuntu 11.04, this time I didn’t install LAMP just the base Ubuntu Server with open ssh

    The install went well no problems, I then installed Putty on to my windows machine, so I ssh to my server from my windows machine all ok, next I setup a password for root, ok.

    Then as you sugested I used the aptitude command to update and upgrade all ok.

    So far so good

    Now what do you think is the next step ? Do i install LAMP or something els ?
    The next item is the ftp server which would you use and what configuration should I make / change, by the way I do want to make this server live over the web for my Wife to display her art.

    Thanks Mate

  13. LostScene

    Hello i have been following the steps of your article using Linux Mint 12 and everything ok πŸ™‚

    Thank you for the article it’s really usefull.

    Have a nice day πŸ™‚

  14. Eric

    I like this article and it works very well for ubuntu or mint desktops in the 10-11 range. I wish you had a printer friendly version, however… πŸ™

  15. Johanna

    helpful for me, too.
    unfortunately i get permission error when accessing the folder by browser, even from localhost and logged in as user.
    any suggestions?

  16. Forseti


    Your article was very helpful for me, many thanks for it!
    However it was not sufficient – Apache gave me 403 Forbidden error.

    It turned up that one needs to set proper permissions on userdir directory and also on every directory on higher level up to his home directory. In case of ‘userdir devel/www’ (/home/username/devel/www) it means on: ‘www’, ‘devel’ and ‘username’.

    The ownership should be username:www-data with group permission to read. In my case I chose to set ‘www’ permissions to read and write. One can set it all using Nautilus – just don’t use ‘Apply permissions to included files’ button.

    @Marko: Thanks again for your article.

    1. Marko Author

      I usually add my self to www-data group and set permissions on my “www” directory to 775. This way both apache running as www-data user my self have full control to “www” directory.

  17. maegashira

    Thanks a lot Marko, it was driving me crazy until I read your article about userdir! Thank you very much, helps a lot.

  18. Stas Kogut

    I get “404 Not Found” error. I read here and on Debian Wiki. All should be right, but it’s not working.
    Where could I be mistaken?

    Thank you.


Leave a Reply

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