Monday, May 16, 2011

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

 ~*~ From the Part 1 ~*~

Make user that you have installed php5 in your system. If not execute following commands in terminal.

 sudo apt-get install php5  
 sudo a2enmod php5  

first line install the php5 and second line enables PHP in apache server. Then restart apache using,

 sudo /etc/init.d/apache2 restart  

At this point, Apache and PHP are installed and ready to go.

I’m using ubuntu lucid distribution,
A recent update to the Lucid distribution, however, requires a slight change to /etc/apache2/mods-available/php5.conf to re-enable interpretation in users' home directories, but previous distributions do not require this change.

Open give file using your favorite text editor. Here I am using gedit.

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

Comment out (or remove) the following lines:
 <ifmodule mod_userdir.c="">  
     <directory *="" home="" public_html="">  
       php_admin_value engine Off  

After doing this change, restart apache using,
 sudo /etc/init.d/apache2 restart  

At this point, PHP should be successfully installed and working.

But this method will enable PHP for all user directories. For security reason this method is not a good practice. One thing can do is enable only for one user directory. To do this, follow this procedure.

Do not edit /etc/apache2/mods-available/php5.conf file and create a file (as root) called /etc/apache2/conf.d/php-in-homedirs.conf with the following contents:
 <ifmodule mod_userdir.c="">  
     <directory $username="" home="" public_html="">  
       php_admin_value engine On  

Simply replace the $USERNAME with the user name of the user you wish to allow PHP access to. Also note that the <directory> section may be repeated as many times as is necessary. Save the file, and restart Apache with a

 sudo /etc/init.d/apache2 restart  

And PHP should only be enabled for the users listed in this file.

~*~ End of Tutorial ~*~

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 ~*~

Wednesday, May 4, 2011

Welcome To the "Try it Now" Blog

Welcome to Try it Now
Hi, I am Hasitha Aravinda from Sri Lanka. This is my second blog, which is completely dedicated for Computer Science Stuffs. I hope to share my knowledge, personal experience with you using this blog. 

Please Read my Blog posts and try it yourself. Let me know when you success or fail and give me your Feedback/suggestions (both good and bad ) . Those things will encourage me to blog new things.


Also Remember that I take no responsibility,  damage of your any physical devices or anything bad (e.g. crashes, loss of data, any damage) that may happen if you use the information or code in this Blog.

Don't afraid. I'll do my best to make sure bugs are fixed and that the content is as accurate as possible. In short, have some common sense and use this at your own risk.  :)

Happy Coding....