Saturday, May 7, 2011

Creating a “Document Root” directory in Home directory on Ubuntu. ( Part 1 )


Generally Apache HTTP web server's document root is /var/www on Ubuntu/Debian Linux environment. (You can find the Document Root directive in /etc/apache2/sites-enabled/000-default.)

But in this post I am going to tell you how to create a document Root directory under your Home directory. This directory can be used same as /var/www directory. 


Since this directory is in your home directory you do not need to worry about permission issues. Also this might help for normal desktop user as well as web site developers to run/test their web sites or applications.

Here I’m using Apache Module userdir ( to do this. Mooshak is a one of web based tool that uses this Apache userdir.

Tested Environment:

Ubuntu 10.10
Apache 2.2.17

Step1: Installing Apache2 and PHP5

First we need to install apache2 and php5 in your machine. If they are already installed ignore this step.

Open Linux terminal and type;
 $sudo apt-get install apache2 php5  

If you want to make sure that, apache2 is installed correctly; open a web browser and go to link http://localhost/ . If page get loaded correctly with saying it's working, that means you have installed apache2 correctly.

Also you can check the PHP installation using the terminal, just type in the terminal 

 $php -version  
 If it is correctly installed, you can see the installed version PHP in terminal.

Step 2: Creating a directory “public_html”

Create a directory under home directory called “public_html”. Here we are going to use this directory store our web pages.  Use any GUI base tool (file browser) or terminal for this. It doesn't matter. But do not use sudo command for this.

In terminal fist go in to your home directory and type  

 $mkdir public_html  

Step 3: Enable Apache module userdir

Here we are enabling an Apache module, called userdir.

You can find the available apache module under, /etc/apache2/mods-available/ .  
Also you can find the module enabled under /etc/apache2/mods-enabled/.   

To enable module userdir, type in terminal;

 $a2enmod userdir  

Another Way:
This is an alternative way to do same thing. That is symlink /etc/apache2/mods-available/userdir.load and /etc/apache2/mods-available/userdir.conf files to /etc/apache2/mods-enabled/ directory.
To do this, type in terminal;

 $cd /etc/apache2/mods-enabled/  
 $sudo ln -s ../mods-available/userdir.conf userdir.conf  
 $sudo ln -s ../mods-available/userdir.load userdir.load  

Restart Apache2 to enable userdir module.

 $sudo /etc/init.d/apache2 restart   

Congratulation, now you have enabled public_html document root directory in your machine. Note that you can access your websites in your home directory by a URL in the form http://localhost/~USER_NAME. For example if your user account name is USER, then you can access your website(s) in your public_html  directory via, http://localhost/~USER.

Testing the public_html directory using a sample html.

Open your favorite text editor, type  

     <h1>Hello World&lt;/h1>  
     <p>This is the default web page for your user directory.</p>  

Then save it as index.html in public_html directory.

Then open your browser and enter URL of the your user directory.  You can see the web page that we have created.

In Next Tutorial > How to enable PHP support in public_html 

~*~ goto Part 2 ~*~