Ubuntu Apache2 : Change default DocumentRoot /var/www 55

By default the document root folder for apache2 in Ubuntu is /var/www. This is where 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 (make sure there is no space in your new folder name; /opt/my\ site/ will not work)

Change DocumentRoot /var/www to  DocumentRoot /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 service apache2 restart
Please like & share:

55 thoughts on “Ubuntu Apache2 : Change default DocumentRoot /var/www

  1. Reply Rohit Apr 13,2011 1:14 pm

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

  2. Reply Japa Alekhin (WP Theme Ninja) May 2,2011 9:56 pm

    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.

    • Reply alex casciani Sep 27,2011 6:26 am

      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

      • Reply ill Jun 20,2012 6:38 am

        > then edit the line that says
        > “” to
        > “”

        I am getting 403 errors, can you tell me what line exactly I need to edit? There seems to be nothing in your quotes.

        • Reply Los Jul 14,2012 1:40 am

          There is another line a few lines down that says:

          &ltDirectory /var/www/>

          This line has a trailing frontslash. I dunno if that is important or not.

          • Reply kajsa Apr 10,2013 1:16 am

            Yes, the trailing frontslash is significant. Without it, I get the 403. With it, I can see my index.html file.

  3. Reply roni Jul 6,2011 1:31 am

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

  4. Reply David Sep 22,2011 4:33 am

    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?

    • Reply Ajo Paul Sep 22,2011 10:48 am

      ensure the apache user has access to the newly created directory.

      • Reply David Sep 23,2011 5:27 am

        Tried a bunch of commands from different sites and now i get the error: 500 ( Internal server error).
        Could you please point me to a website that gives reliable information on how to set permission to the documentroot?

        Thanks.

  5. Reply yaya Oct 7,2011 9:07 am

    Thanks dude. its work =)

  6. Reply Julio Oct 16,2011 2:25 am
    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. Reply sridhar Oct 20,2011 4:09 pm

    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. Reply Samy Oct 25,2011 8:42 pm

    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. Reply Sha Nov 30,2011 11:12 am

    Beautiful pic of the path on ur website!

  10. Reply saber Apr 1,2012 2:30 am

    thank ajo
    this help me to resolve my problem.
    i have a question about mysql : how to set users in mysql open source that install in ubuntu server to force them to change password in specific day ?

  11. Reply v Apr 13,2012 8:07 pm

    GET A JOB !!! :…

    default wp template … u didn’t even changed the basic layout …. neither the basic image …. or the basic menu ……..and in addition the proposed configuration is ….. i’d better not say… dunno how u got on the top but i hope ppl will realize ho useless this site is and how useless solutions u purpose.

    Please consider to close the site and to go to working in a cotton field.
    U only made me loose time …. and time is money …. plase do not spam any more on the web.
    Thanks.

  12. Reply george sanji muhammed Apr 20,2012 7:11 pm

    Your last step is the old way of restarting apache2
    latest is “service apache2 restart” …..

  13. Reply balaks80 May 20,2012 2:55 am

    Thank you ! exactly what i needed.

  14. Reply Zia Jun 13,2012 9:38 pm

    It was nice documentation, but my problem is not yet solved, my folder is in /home/user/folder and now how i should give this paths to the /etc/apache2/site-availble/mysite
    I have give like this : DocumentRoot “” and <Directory "
    so when I restart the apache it said that these path is wrong. would you please help me. Thanks in advance

  15. Reply jim Jun 24,2012 4:00 am

    Ajo Paul, I commend you for your effort to help others (myself included) and hope that you don’t get discouraged by someone like v…

  16. Reply vyll Jul 3,2012 3:07 pm

    i did this and got the above error,any help?

    nuaha@nuaha-Satellite-C660:~$ sudo service apache2 restart
    * Restarting web server apache2 Warning: DocumentRoot [/nuaha/mysite] does not exist
    … waiting Warning: DocumentRoot [/nuaha/mysite] does not exist
    [ OK ]
    nuaha@nuaha-Satellite-C660:~$ gksudo gedit /etc/apache2/sites-available/mysite
    nuaha@nuaha-Satellite-C660:~$ sudo service apache2 restart
    * Restarting web server apache2 Warning: DocumentRoot [/nuaha/mysite] does not exist
    … waiting Warning: DocumentRoot [/nuaha/mysite] does not exist
    [ OK ]

  17. Reply Felipe Tavares Jul 17,2012 8:21 am

    Thanks man! It’s exactly what I’m looking for!

  18. Reply Pershing Aug 14,2012 6:03 am

    I changed the line in sites-available/default to my new directory. Saved and restarted apache. Worked great. Problem is that default.conf reverts back to “DocumentRoot /var/www” everytime I restart/ reboot the machine. How do I prevent this?

    • Reply Ghezal Ahmad Aug 14,2012 6:53 pm

      Thanks for the good documentations, but my problem was not solved here, I had solved it. Thanks

  19. Reply Pershing Aug 28,2012 6:32 am

    Nevermind my previous post. I followed your instructions. Worked Great! THANKS!!

  20. Reply yash Sep 8,2012 8:08 pm

    I am still getting the 403 forbiddon error..tried with all the set of permissions mentioned in the blog…
    what did i do:
    editd the default file in avialable-sites to point to some folder in my home directory
    and then change the permissions…
    I need to get this done at the earliest..
    HELP ME PLEASE !!!

  21. Reply yash Sep 8,2012 8:41 pm

    solved !!! 😀
    done by changing the user that apache is running from www-data to your own account.
    paste the following lines in your httpd.conf file:

    IfModule !mpm_netware_module>
    #
    # If you wish httpd to run as a different user or group, you must run
    # httpd as root initially and it will switch.
    #
    # User/Group: The name (or #number) of the user/group to run httpd as.
    # It is usually good practice to create a dedicated user and group for
    # running httpd, as with most system services.
    #
    User ###PUT_YOUR_USERNAME_HERE###
    Group ###PUT_YOUR_GROUPNAME_HERE###

  22. Reply Syukrie Sep 27,2012 11:23 am

    why i can change the default to phpmyadmin..??

  23. Reply Faur Ioan-Aurel Dec 29,2012 7:35 pm

    If you still have problems with 403 error , you should try this:
    open with your favourite editor as a sudo this file: /etc/apache2/httpd.conf

    By default it should be a blank file. Now in this file add this line: User

    it’s the user with which you are loged in on your machine.

  24. Reply Leon Nguyen Jan 9,2013 5:00 pm

    Oh !! Thanks so much !! This is what i need !

    Sorry. I’m come from Viet Nam and rather poor english.

  25. Reply mujaffar Jun 25,2013 6:25 pm

    I was getting same forbidden error (In windows machine for wamp), after lot of r & d I found that there is need to change default virtual host in httpd-vhosts.conf file which is present in C:\wamp\bin\apache\Apache2.2.17\conf\extra

    From :

    ServerAdmin webmaster@localhost
    DocumentRoot “c:/wamp/www”
    ServerName localhost
    ErrorLog “logs/localhost-error.log”
    CustomLog “logs/localhost-access.log” common

    To:

    ServerAdmin webmaster@localhost
    DocumentRoot “E:/wamp/www”
    ServerName localhost
    ErrorLog “logs/localhost-error.log”
    CustomLog “logs/localhost-access.log” common

    Hope linux will need same change to accomplish this

    Thanks

  26. Reply Sunil Sep 5,2013 4:15 pm

    Hi guys,
    Can you please tell me how to enable multiple virtual host in localhost(Ubuntu)

    I want three localhost similar directories with 3 different root path.

    Can you suggest where to change it..

  27. Reply Kayce Basques Apr 18,2014 5:30 am

    Worked for me. Thanks.

  28. Reply oliver_me03 Apr 30,2014 2:51 pm

    Hi, using Linux Mint 13 and stuck with this:
    ERROR: Site mysite does not exist!

    Kindly help.
    Thanks.

  29. Reply Valerius Oct 6,2015 2:44 am

    Hi,
    I tried it and does not work for me.
    I continue to get the message 403 Forbidden.
    Any help will be appreciated.
    Thanks,
    Valerius

  30. Reply francisco cano Dec 10,2015 12:42 pm

    Error: cannot effectuate ‘stat’ on «/etc/apache2/sites-available/default»: No such file or directory

    I’ve browse to that folder and there’s two files, one called 000-default and another one called 000-default-config or something like that. Any ideas?

Leave a Reply to yash Cancel Reply

  

  

  


*