A website document root is the folder in which the files of the websites are stored. When you are hosting multiple subdomains, you will need to add them in different folders for each. Normally the document root is public_html for a cpanel main domain. As cPanel does not provide any tools for changing the document root easily, Changing the document root to a different folder is an essential task for us. Here’s a step-by-step guide to help you change the document root for a main domain in cPanel.
Prerequisites
- To change the document root of your main cPanel domain, you’ll need root access to the server. This is because the changes require updating the configuration files. If you don’t have root access, contact your web hosting provider for assistance.
Important: Always back up your existing configuration before making any changes.
For example, let’s assume we have a cPanel domain example.com, whose document root is currently located in /home/example/public_html
1. Access the server via SSH and Locate the Current Document Root
The default document root for the main domain is typically in the public_html folder. Before making any changes, confirm where your current website’s files are stored to avoid the mistake.
2. Create a New Directory (Optional)
If you want to change the document root to a new directory, create a new directory inside public_html (or any other desired location). For example, you could create a folder called new_folder in the public_html directory using the mkdir command. Skip this step if the directory already exists.
#mkdir /home/example/public_html/new_folder
3. Update the Configuration Files
After setting the new document root, modify the location in the “documentroot” to the new location for the configuration file, this requires editing the following files
/var/cpanel/userdata/cPanelUsername/domain.tld
/var/cpanel/userdata/cPanelUsername/domain.tld_SSL
According to our example:
/var/cpanel/userdata/example/example.com
/var/cpanel/user data/example/example.com_SSL
4. Remove the existing cache files:
Remove the existing cache files of the main domain using the rm command:
#rm /var/cpanel/userdata/example/example.com.cache
#rm /var/cpanel/userdata/example/example.com_SSLcache
5. Rebuild the cache files and the Apache configuration
After making the necessary changes, Rebuild the cache files and the Apache configuration to regenerate the cache files and Apache configuration with the changes.
After making the necessary changes, run the following scripts to rebuild the cache files and update the Apache configuration. This ensures that the new document root is applied correctly.
#/scripts/updateuserdatacache
#/scripts/rebuildhttpdconf
Then, Restart the web server (Apache) for the changes to take effect.
#/scripts/restartsrv_apache
Conclusion
Changing the document root in cPanel is a simple yet powerful way to manage where your website files are stored and served. Whether a new folder is being moved for security reasons or the site’s structure is being reorganized, these steps can be followed to get it done quickly and efficiently
Need help changing the cPanel Document Root for the main cPanel Domain ? Contact us for server support and troubleshooting assistance.