Home > Web Development > Enable userdir Apache module on Ubuntu Linux and other Debian based distributions

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 already wrote about one method of making Apache web server work this way in my following article:

Ubuntu Netbeans and LAMP server with Xdebug as non-root user

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:

gksudo gedit /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
		<Limit GET POST OPTIONS>
		        Order allow,deny
		        Allow from all
		</Limit>
		<LimitExcept GET POST OPTIONS>
		        Order deny,allow
		        Deny from all
		</LimitExcept>
        </Directory>
</IfModule>

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:

gksudo gedit /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>
    <FilesMatch "\.phps$">
	SetHandler application/x-httpd-php-source
    </FilesMatch>
    # 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>
    #</IfModule>
</IfModule>

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 http://127.0.0.1/~username/ address. Now you can develop for web using public_html to store and test your PHP and HTML code. Have fun coding!

Comments
  • Post a comment

  • Xin April 19, 2012 at 07:10:48

    Thank you for this article! It does help!

  • Johanna March 16, 2012 at 14:35:00

    hi,
    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?

  • fajarramd March 12, 2012 at 03:01:00

    Thanks. This helpfull for me :)

  • Andrew Moore February 27, 2012 at 06:15:00

    Thanks for your post!

  • Eric January 6, 2012 at 17:33:55

    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… :(

  • LostScene December 17, 2011 at 15:45:14

    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 :-)

  • Marko July 30, 2011 at 11:37:16

    @Neville Cox

    Great to hear that you encountered no problems. The order of setting up services Isn’t really important. For FTP I usually use vsftpd server but you have many other to choose, here’s the Ubuntu wiki for vsftpd:

    https://help.ubuntu.com/10.04/serverguide/C/ftp-server.html

    Wish you good luck, if you encounter any problems you know what to do ;)

  • Neville Cox July 30, 2011 at 10:41:36

    Marko
    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

  • Neville Cox July 30, 2011 at 02:37:17

    Thanks Mate
    I vive In a town called Mitcham in Victoria Australia Down south
    Nev

  • Marko July 29, 2011 at 12:41:49

    @Neville Cox

    I’m glad to be of assistance. When I’m online you can also contact me using my little WordPress plugin Quick Chat on TechyTalk.info sidebar or using contact form http://www.techytalk.info/contact/. Anyways I’m from little country called Croatia at the coast of Adriatic sea.

    Installing fully functional LAMP, ftp and ssh server isn’t easy especially secure one, but you’re doing great ;)

  • Neville Cox July 29, 2011 at 12:28:07

    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

    Nev

  • Marko July 28, 2011 at 12:47:21

    @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“.

  • Neville Cox July 28, 2011 at 10:49:24

    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?

    Neville

  • Marko July 28, 2011 at 10:33:15

    @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 ;)

  • Neville Cox July 28, 2011 at 09:48:44

    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

  • Marko July 28, 2011 at 09:38:48

    @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”.

  • Neville Cox July 28, 2011 at 09:31:46

    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

  • Marko July 28, 2011 at 09:12:33

    @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.

  • Neville Cox July 28, 2011 at 08:40:08

    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

    Neville

  • Marko July 27, 2011 at 12:59:47

    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”).

  • Neville Cox July 27, 2011 at 12:53:30

    Hi
    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

    Neville