How to Change Default Apache ‘DocumentRoot’ Directory in Linux

Apache web server is probably the most used web server across platforms including different Linux distributions and Windows. The web server is used to deliver web content and can serve many queries at once.

It is often the preferred choice by professionals for building different web projects. Having at least basic knowledge of this web server is vital for any young professional who wants to start a career as a Linux system administrator.

In this short tutorial, you are going to learn how to modify the root directory for Apache web server. For the purpose of this tutorial, we will use Ubuntu/Debian and RHEL/CentOS/Fedora based installations of the web server.

However the paths and directives are practically the same for other distributions as well, so you will be able to apply the learned in different OSes as well.

To make the necessary changes you need to modify the DocumentRoot directive of the web server. This is the directory from which Apache will read the contents that the visitor will access over browser. Or in other words, this is the directory that forms the tree of directories that will be accessible over web.

The default DocumentRoot for Apache  is:

/var/www/html
or
/var/www/

These paths are described in the Apache’s configuration file.

Ubuntu/Debian

/etc/apache2/sites-enabled/000-default
/etc/apache/apache2.conf

CentOS/RHEL/Fedora

/etc/httpd/conf/httpd.conf

To change the document root for your Apache web server simply open the corresponding file with your favourite text editor and search for DocumentRoot.

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"

After that change the path to the new target directory and make sure that Apache is able to read/write in that directory. Once you have modified the DocumentRoot, save the file and restart apache with:

# systemctl restart apache     [For SystemD]
# service httpd restart        [For SysVinit]    

Final thoughts

The change of default document root is an easy task that can be completed in a couple of minutes. When making such changes is important to make sure that you do not make any typos and make sure to always restart Apache after making changes to its configuration file.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Marin Todorov
I am a bachelor in computer science and a Linux Foundation Certified System Administrator. Currently working as a Senior Technical support in the hosting industry. In my free time I like testing new software and inline skating.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

11 Comments

Leave a Reply
  1. Hi,

    I have a question. I do not want to change the default setting in httpd.conf of DocumentRoot “/var/www/html” but I also want web files to be in DocumentRoot “/home/www/html”

    How do I do that?

    Reply
  2. If you install ispconfig make site server name (hosname -f) with ispconfig.

    Delete 000-default.conf

    Create symlink.

    # ln -s  /etc/apache2/sites-available/server100.sytes.net.vhost /etc/apache2/sites-enabled/000-default.conf
    

    Restart apache.

    # service apache2 restart
    
    Reply
  3. It doesn’t help i’m trying different path from DocumentRoot “/var/www/html” to /usr/share/tomcat/myApps its not worked its needs to more shape before publishing.

    Reply
  4. Point 1 is pretty known stuff use netstat and some other commands, however it will not show all that is moved over the connection. Some things could be hidden from netstan thing…

    Point 2, and somebody bad could gain access to root sudo stuff,… I had issues with that thing, and I have tracked them to some place in africa… but after that I was not able to track them further ,…

    @ Marin Todorov I will try to use point 3 , …but chmod will not allow your file to be modified … but when moved on Windows and …then will be opened… to … well, not nice stuff….

    Reply
    • It sounds to me like a bad web server configuration or something like that. Try running Apache as separate user/group that way it will not be able to write outside of the given directory. While you might be familiar with some of the Apache security concepts, I would recommend you having a look at https://www.tecmint.com/apache-security-tips/ as it my contain information that can help you improve your server’s security.

      Reply
  5. Now I would have few questions.

    1. How would I see every connection on my ps if I use netstat command?

    2. Is is possible to see the content of the root, in order to prevent some bad stuff being installed on your Linux device, as well as having chance to observe firmware into more detail.

    3. How to prevent somebody to copy some files from your pc….

    Reply
    • @Dusko,

      Here are the answers to your questions.

      1. Yes, you can use netstat command to list all active connections to your VPS, if you don’t know how to check, here is the advanced guide on netstat command with their practical examples

      20 netstat Command to Monitor Network Connections

      2. Unless, until someone have physical access to system, then only can someone able to install softwares…

      3. Just enable Firewall on the system and open only required ports and also if possible protect your system with grub password..

      Reply
      • I would like to add something to points 2 and 3.

        2 – Only users with root/sudo access can install additional packages on your system

        3 – If you want to protect files from being copied you can use chmod and chown utility so that only your user/group can access the files you want to protect

        Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.