Ubuntu Apache2 : Change default DocumentRoot /var/www

By default the document root folder for apache2 in Ubuntu is /var/www. This were you can store your site documents. In order to change the default site location to a different one, /opt/mysite use the following method. A detailed steps to install LAMP on ubuntu is given here.

To do this, we must create a new site and then enable it in Apache2.

To create a new site:

Copy the default website as a starting point. sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/mysite

Edit the new configuration file in a text editor “sudo nano” on the command line or “gksudo gedit”, for example: gksudo gedit /etc/apache2/sites-available/mysite

Change the DocumentRoot to point to the new location. For example, /opt/mysite

Change the Directory directive, replace <Directory /var/www/> to <Directory /opt/mysite/>

You can also set separate logs for each site. To do this, change the ErrorLog and CustomLog directives. This is optional, but handy if you have many sites

Save the file

Now, we must deactivate the old site, and activate our new one. Ubuntu provides two small utilities that take care of this: a2ensite (apache2enable site) and a2dissite (apache2disable site).

sudo a2dissite default && sudo a2ensite mysite

Finally, we restart Apache2:

sudo /etc/init.d/apache2 restart

About Ajo Paul

A Monk who's yet buy his Ferrari!
This entry was posted in apache2, ubuntu and tagged , . Bookmark the permalink.

18 Responses to Ubuntu Apache2 : Change default DocumentRoot /var/www

  1. Rohit says:

    5th Link on the 1st page of Google Search ! Congratz !
    Exactly what I needed. Thx.

  2. do you really have to create another file in sites-available? why not just use the default one and just change the directory from /var/www to /home/user/Projects/whatever/public_html?

    honestly i did what i just said and it doesn’t work. gives me 403 forbidden. so i’m going to try your it you way.

    • alex casciani says:

      you can edit the configuration file, type

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

      then (for instance use /home/user1 as the directory)

      edit: the line that says something like:
      “DocumentRoot /var/www” to
      “DocumentRoot /home/user1″

      then edit the line that says
      “” to
      “”

      if you do just the first part and not the second, you will get 403 errors

  3. roni says:

    I need to edit DocumentRoot valve from command line. How can I do this?

  4. David says:

    I need extra help with running scripts following the change of the document_root directory. I successfully changed the directory, restarted apache and tried to run a test script upon which i get the “403 forbidden: you don’t have permission to access …….. “error.

    trying with default yield the desired result however. What can i do?

  5. yaya says:

    Thanks dude. its work =)

  6. Julio says:
    I tried all of these instructions but once im done it throws me a 403 forbidden:
    You don't have permission to access / on this server. Apache/2.2.20 (Ubuntu) Server at localhost Port 80.
    what should i do?
  7. sridhar says:

    thanks a lot. really relieved me of very anxious moments being a apache newbie.
    just wanted to state that all occurance of /var/www occuring in the default file have to be changed not just the first one. correct me if i am wrong.

  8. Samy says:

    Hi, I apache seems to need r_x permissions on the folder and files, else I get a 403 error. Just 644 didn’t work, i used “chmod 755 -R”.

  9. Sha says:

    Beautiful pic of the path on ur website!

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre user="" computer="" escaped="">