07 July, 2011

Change the DocumentRoot of Apache webserver (/var/www) in Debian / Ubuntu

The document root (also called as the web root) of the Apache web server is located at the /var/www directory by default. Document root is the place where we find all the web pages and server scripts. These files are loaded on the browsers when we type in a particular url to point to that server. 

The /var/www directory doesn't allow access for a user who doesn't have the root permission. It's a headache task to switch to the root mode by typing in the root password everytime even though you know the password, since you may have to change your webpages or scripts during its development stage  very frequently. So a better idea to avoid this problem is to change the default Document root directory to some other location which doesn't require root permission for access.


Change it dude !!!
- - - - - - - - - - - - - - - - - -#

Inorder to change the document root,

#1. Make a directory somewhere inside your home directory to use as the Document root. (not necessarily be home... you can make it anywhere). I named it 'www'

$ mkdir /home/jo/www

#2. Now we have to edit the one of the configuration file of the Apache webserver. The file is located @  /etc/apache2/sites-available/default (requires root permission to edit it).

$ sudo gedit /etc/apache2/sites-available/default

The file will be opened up in the gedit text editor. Now you have to edit the DocumentRoot directive in this file. Replace the /var/www with the /path/to/your/new/document/root in the lines 4 and 9 as shown in the figure below.


#3. Restart the apache webserver.

$ sudo /etc/init.d/apache2  restart



Now test it !!!
- - - - - - - - - - - - - - - - - -#

#1. Create a html/php file inside the Document Root directory (mine is @ /home/jo/www).

#2. Take the terminal and navigate to the Document Root directory (using cd command). Use chmod command given below to allow read+write+execute permission to all.

$ chmod 777 *

#3. Take your web browser and type in http://localhost and hit the enter key! The apache server will load the contents of your Document Root directory as in the figure.


Hope you have done it right!!!


3 comments:

red eyed monster on Sunday, December 04, 2011 said...

I did this and it works... but now it wont show .html files.

red eyed monster on Sunday, December 04, 2011 said...

I did this and it worked... but now I can't see .html files nor will it display html commands properly...

jiNtU jAcoB on Monday, December 05, 2011 said...

Weather those html files are copied to document root or manually created there?. Sometimes the permission poblems may not let you see the files which are there in the document root. Try the chmod command to allow the view permision for those files.

What you mean by html commands?

Post a Comment

speak out... itz your time !!!

 

Terminal Diary | techblog Copyright © 2009 Gadget Blog is Designed by jintu jacob Powered by Blogger