How can change the Root Directory by Using Symbolic Links

This article explains how to alter a shared server account’s document root directory. The default document root directory for your account is /home/username/public_html, where username is your account username. However, you might want to use a different directory as the document root on occasion. For example, if you want to try out a new application or test a website configuration before it goes live, this is frequently the case.

To modify the document root directory, This approach makes use of symbolic links and necessitates the use of SSH. 

Do not use the techniques below if you want to alter the document root directory for an addon domain. Instead, utilize cPanel to update the document root directory for the addon domain.

Using Symbolic Links

By copying the contents of the public_html directory to another directory and then creating a symbolic link that refers public_html to the new document root directory, you can alter the document root directory. Follow these steps to accomplish this:

  • Use SSH to access your account.
  • Type the following command at the command prompt:
mv ~/public_html ~/public_html_backup
  • This command copies the contents of the public_html directory, including all files, to the public_html backup directory. If you choose, you can call it something other than public_html backup.
  • You must perform the next command in step 3 as soon as possible after running this command for two reasons. First, as long as public_html is absent, your site is unreachable. Second, if the public_html directory is absent, cPanel will automatically recreate it. Your site will not perform as expected if you have not run the command in step 3 before this happens.

Replace new document root with the name of the directory you wish to use as the new document root in the following command:

ln -s ~/new_document_root ~/public_html

To test your website, use a web browser. Content from the new document root directory should be displayed in the browser.

If your website isn’t working properly, you should:

  • Ascertain that the new document root directory is present and contains web site files (such as index.html or index.php).
  • Make that the commands in steps 2 and 3 are executed in quick succession. Type the following command to see if the symbolic link is correct:
ls -l ~/public_html

You should get something that looks like this, where username is your account username:

/home/username/public_html -> /home/username/new_document_root

If the result isn’t similar, the symbolic link isn’t set up appropriately. Type the following command in the prompt:

rm -rf ~/public_html

Now, repeat steps 2 and 3.

Restoring the public_html Directory

By reversing the technique you followed above, you may get back to the original setup and utilize the public_html directory as the document root.

 Follow these steps to accomplish this:

  • Use SSH to access your account.
  • Type the following at the command prompt:
rm ~/public_html
  • The symbolic link that redirects public_html to the updated document root directory is removed with this command.
  • You must perform the next command in step 3 as soon as possible after running this command for two reasons. First, as long as public_html is absent, your site is unreachable. Second, if the public_html directory is absent, cPanel will automatically recreate it. Your site will not perform as expected if you have not run the command in step 3 before this happens.

Replace source directory with the name of the directory you want to use as public_html in the following command. This directory might be the public_html backup directory you established in the previous procedure, the new document root directory you used for testing, or something completely different:

mv ~/source_directory ~/public_html

To test your website, use a web browser. Content from the public html directory should be displayed by the browser.