How to change a document root ( docroot / webroot / where you put your files so they can be seen online) for a cPanel account is something that pops up from time to time. Or in my case a few times this week already, so I decided to write a quick little post on how to do this. For whatever reason cPanel does not allow an easy way to change a docroot. However you can specify a docroot if making a subdomain, even outside the public_html for the user. But cPanel does not have this option anywhere for the main user itself. This can be needed at times for development uses, just to point to a folder and not use the normal public_html, or avoid using rewrite/redirect rules, or fixing an issue that was caused by a manual install of something like an SSL on a subdomin.
As cpanel does not have a nice way to do this you will need to SSH into the server as root and make some modifications. Many people will assume you can just edit the httpd.conf file to change the docroot for a given domain. (EA3 /usr/local/apache/conf/httpd.conf or EA4 /etc/apache2/conf/httpd.conf/) But in a cPanel environment this will cause issues down the road as the apache config file is rebuild on a regular basis for many types of updates. So doing any hand edits to httpd.conf is a very bad idea in cpanel. Remember folks it is cPanel's way or the highway!
The proper file and path to edit would be:
Replace $user and $domain.com with the proper cpanel user and domain to change. If they have an SSL there will also be a $domain.com_SSL datafile. Edit this with your fav editor. The line you are looking for is:
You can now change this to whatever is needed. Save the file and exit. To ensure all is fine we will rebuild the apache config file using this information cPanel has stored and restart apache after a backup of the config:
You should now be using the new docroot for this domain. Now some people may just say modify the .htaccess and add some redirect/rewrite rules there. This will work if you are keeping the docroot in the public_html. But if you are placing it outside this location or just dont want to mess with creating rules which for many is confusing you can just do it this way and it will play well with cPanel.