How to Change Document Root Location


This article provides a guide for changing the document root location.

What is 'Document Root'?

The DocumentRoot is the top-level directory in the document tree visible from the web and this directive sets the directory in the configuration from which Apache2 or HTTPD looks for and serves web files from the requested URL to the document root.

By default, your account's document root directory on a shared hosting server is the /home/$USERNAME/public_html directory, where "$USERNAME" represents your cPanel account username. However, sometimes you may want to use a different directory as the document root. This is frequently the case if you want to try out a new application or test a website configuration.

You can change the document root directory by moving the contents of the public_html directory to another directory, and then creating a symbolic link that points public_html to the new document root directory.

Steps to Change Document Root Location

To change the DocumentRoot location, follow these steps:

  1. Log in to the server using SSH.
  2. At the command prompt, type the following command:
    mv ~/public_html ~/public_html_backup

    This command moves the public_html directory, and all of the files it contains, to the public_html_backup directory. You can use another name besides public_html_backup if you want.

    After you run this command, you must run the next command in step 3 as soon as possible for two reasons:

    1. First, your site is inaccessible as long as public_html is missing.

    2. Second, cPanel will automatically recreate the public_html directory if it detects that it is missing. If you have not run the command in step 3 before this occurs, your site will not function like you expect.

  3. Type the following command, replacing new_document_root with the name of the directory that you want to use as the new document root:
    ln -s ~/new_document_root ~/public_html

    Your site may be inaccessible until the server's cron job sets the correct file permissions and group ownership for the new document root directory. This cron job runs at twelve minutes past the hour on every hour evenly divisible by four (00:12, 04:12, 08:12, 12:12, 16:12, 20:12, all times in CST (GMT-05:00)).

    To minimize any possible downtime for your site, change the document root directory just before one of these scheduled times. After the cron job runs, the new document root directory file permissions are set to 750, and the group ownership is set to nobody, as shown in the following sample directory listing:

    drwxr-x--- 3 username nobody 4096 Mar 10 10:08 new_document_root/

    Use a web browser to test your web site. The browser should display content from the new document root directory.

    If your web site does not function correctly:

    Make sure that the new document root directory exists and contains web site files (such as index.html or index.php).

  4. Make sure you run the commands in Step 2 and Step 3 in quick succession. To verify that the symbolic link is correct, type the following command:

    ls -l ~/public_html

    You should see output similar to the following, where username represents your account username:

    /home/username/public_html -> /home/username/new_document_root
  5. If you do not see similar output, then the symbolic link is not set up correctly. Type the following command:

    rm -rf ~/public_html
  6. Then repeat steps 2 and 3.

Restoring the public_html directory

You can restore the original configuration and use the public_html directory as the document root again by reversing the procedure that you followed above. To do this, follow these steps:

  1. Log in to your account using SSH.
  2. At the command prompt, type the following:
    rm ~/public_html

    This command removes the symbolic link that redirects public_html to the modified document root directory.

  3. After you run this command, you must run the next command in Step 4 as soon as possible for two reasons.
    1. First, your site is inaccessible as long as public_html is missing.
    2. Second, cPanel will automatically recreate the public_html directory if it detects that it is missing. If you have not run the command in step 3 before this occurs, your site will not function like you expect.
  4. Type the following command, replacing source_directory with the name of the directory that you want to use as public_html. For example, this directory could be the public_html_backup directory that you created in the previous procedure, the new_document_root directory that you were using for testing, or an entirely different directory:
    mv ~/source_directory ~/public_html
  5. Use a web browser to test your web site. The browser should display content from the public_html directory.

Conclusion

You now know how to change Document Root location of your website in cPanel/Apache server.

  • apache, document root, symlink
  • 3 Kunder som kunne bruge dette svar
Hjalp dette svar dig?

Related Articles

Video: Create Email Address in cPanel

This video tutorial demonstrates how to create an email address using cPanel Shared Hosting...

Disabling mod_security in cPanel

Mod_security is an apache module that helps to protect your website from various attacks. It is...

Video: Setup Email Forwarder in cPanel

View our Step-by-Step Tutorial Video for Setting up an Email Forwarder in cPanel Shared Hosting.

Install an SSL Certificate Using the AutoInstaller Software in cPanel

This article will describe how to use AutoInstall to install an SSL certificate.The AutoInstall...

Video: Add MX Entry in cPanel

This video tutorial provides Step-by-Step instructions for adding an MX entry in cPanel Hosting...