28Oct2020
Read time: 4 mins
The WHM interface showing an open Terminal screen.

I build most of my web sites using Drupal 8 and more recently, Drupal 9. The recommended Drupal installation profile installs Drupal in a sub-directory in the documentroot so instead of the site being installed in 'public_html,' it is installed in 'public_html/web' by default. You can rename 'web' to whatever you want during installation but for this post we will use the default 'web' directory created during the installation of the Drupal 9 core-recommended profile using composer.

To change the documentroot using WHM/cPanel you will need to have access to your Apache web server configuration files and be comfortable navigating through directories in the terminal and using the vi editor, or some other text editor, to modify files on your web server.

Of course, your web server may be configured differently so the instructions below are just a guide. If you are not comfortable modifying these files yourself contact tech support at your web hosting company.

Why Change the documentroot

There are a number of reasons for why Drupal 8 and 9 are installed in a sub-directory.

  1. Installing in a sub-directory maintains a clean file structure with your Drupal files kept separate from your web server system files.
  2. You will be creating a private and a public directory as part of your Drupal installation and it is recommended, for security reasons that these directories, along with the vendor directory are not in the publicly accessible documentroot.

Modifying the documentroot - Dev vs. Production

Modifying the documentroot is not a problem in a local dev environment because you can easily set up virtual hosts to point to the directory the site is installed in. However, the live server is a different story because depending on your web host you may, or may not, have the ability to change the default documentroot.

In my case, I have 2 VPS servers and since I manage the servers I can make whatever changes I want to the configuration. However, I have customers whose sites are hosted on shared hosting services with other companies and they are not able to change the documentroot.

Change the documentroot in Apache using the WHM Terminal Screen

On my VPS server, in WHM, I make the changes in the Terminal. You will be modifying an existing account so the account must be created first. Once you have created the account you can modify, create the 'web' sub-directory in 'public_html,' or, if you are installing Drupal, the subdirectory will be created as part of the installation process so it will be there when you push the web site files to the live server. You are now ready to change the documentroot.

In your WHM account go to Server Configuration > Terminal. You should now be in the root directory so you need to navigate up one directory, and from there you should be able to navigate to any existing user account following the command below, replacing the 'username' with the cPanel account username for the account you want to modify:

cd ../var/cpanel/userdata/username

Find the following two lines in the username file:

documentroot: /home/username/public_html

path: /home/username/public_html/cgi-bin

Using the vi editor in the terminal, change the documentroot of your primary domain from 'public_html' to 'public_html/web' and the path to include the 'web' directory. The document root and path should now look like this:

documentroot: /home/username/public_html/web

path: /home/username/public_html/web/cgi-bin

Save the updated file.

Then delete the cache file for the domain:

# rm -vf /var/cpanel/userdata/username/example.com.cache

If the domain has an SSL certificate installed you will also need to modify the documentroot line in the example.com_SSL file in the same way as above:

# vim /var/cpanel/userdata/username/example.com_SSL

After your changes are complete save the file, and then delete the SSL_cache file for the domain:

# rm -vf /var/cpanel/userdata/username/example.com_SSL.cache

After that’s done, you need to rebuild Apache and restart the server using the following three commands:

# /scripts/updateuserdatacache

# /scripts/rebuildhttpdconf

# service httpd restart

You May Need to Change the Ownership of the Sub-Directory

One final change, I sometimes need to change the ownership of the 'web' folder. Permissions are set to 750, the same as the public_html folder, but I also change the ownership of the 'web' folder to nobody.

To make the change, you will need to be in the same directory as the 'web' folder, which is the 'public_html' directory for the account we're configuring. Execute the following command to go the 'public_html' directory:

# cd /home/username/public_html

Once you're in the 'public_html' directory you can change the ownership of the 'web' folder with the following command (remember to change 'username' to your cPanel account username):

# chown username.nobody web

That's it, the 'web' directory is now the documentroot instead of public_html.

John Moruzi, owner and web designer at WebFire Designs in Brantford, ON.

John Moruzi is a web designer, and the owner of WebFire Designs, a small web design firm in Brantford, ON. He focuses on building powerful Drupal web sites that engage customers and drive traffic. He looks forward to sharing tips and tricks he's learned building Drupal websites for more than 10 years.