cPanel – change root directory with these easy steps
Your website defines the personality of your online business. Many website owners require a lot of customization, features and tweaks to get their desired site.
In our role as Outsourced Tech Support services for web hosts, account related issues or requests are one common category of tasks that we handle.
cPanel root directory aka Document root – Why do you need to change it?
Document root aka cPanel root directory is the default directory in which your website files are stored. These files show up when you access the site via browser.
When you create a domain in your cPanel/WHM server, the document root for that domain would be ‘/home/user/public_html’ directory by default.
While this is usually fine for most website owners, during certain special scenarios, they may need to change this folder to some other directory.
These include installing some third party applications which have custom data folders, configuring URL shorteners, testing different sets of applications or configurations for your site.
cPanel – change root directory in these 4 ways
There are 4 ways to change cPanel root directory and the option we choose varies based on the website’s requirement.
1. Using Addon domains
cPanel offers a feature for website owners to create ‘Addon domains’ from their control panel. Addon domain is a domain that points to a subfolder in the main domain.
Addon domains are configured for customers who prefer to create multiple domains within a single account. They look like independent domains to visitors.
The document root for a domain depends on the type of a domain. For main domain, it is ‘/home/user/public_html’ and for addon domain, it is ‘/home/user/public_html/sub-folder’.
But cPanel offers a feature where website owners can change the default document root for addon domain during creation or after creating it from the cPanel.
To change the root directory of Addon domain, in the cPanel -> “Domains” section, click the Addon Domain and update the new document root in the popup window.
2. Creating symlinks
At times, you may already have another directory which you need to set as your document root. This is usually applicable while testing a new site.
To change cPanel root directory in such cases, we backup the current public_html and then create a symbolic link from the desired directory to public_html.
The folder permissions and ownership and related links in pages have to be properly configured in such scenarios, for the site to work fine after the change.
3. Setting redirects
A commonly used technique for changing cPanel root directory is to set redirects from the public_html to the desired folder which contains the files to be displayed.
Setting redirects also help to avoid duplication, especially if the same content has to be shown in different sites or subdomains.
The redirection is set by adding a ‘.htaccess’ in the folder to be redirected and then adding appropriate rewrite URL rules to point to the desired directory.
4. Updating cPanel files
cPanel/WHM does not have an option to change the document root of the main domain via any interface. By default it is the public_html folder in the user home directory.
In certain rare scenarios, we have customers who want their document root to be changed to another folder than public_html.
This has to be done from the backend by updating the cPanel user account file at /var/cpanel/userdata/username/domainname .
The following parameters are updated with the desired directories.
documentroot: /home/steinknu/public_html scriptalias: - path: /home/steinknu/public_html/cgi-bin/
For sites with SSL installed, the configuration file for SSL is also updated accordingly.
This file is available at the location /var/cpanel/userdata/username/domain_SSL .
After making the changes, the user data cache is updated and Apache configuration file is rebuilt to reflect the changes in the corresponding files.
The Apache and PHP-FPM services are restarted and we verify the relevant configuration files to ensure that they are all updated properly.
Account customization and feature updates are common requests that come from customers of cPanel/WHM server owners. Today we saw how our Dedicated Hosting Engineers help website owners change cPanel root directory.