Relocated web root on shared hosting with symlink

Last updated on
9 January 2024

Warning: Only use this method if you are an experienced Unix user.

The recommended method of running a Drupal site is with a "relocated document root":

[...] the files "index.php" and the "core" directory and so on are placed inside a subfolder named "web" rather than being placed next to "composer.json" and the "vendor" directory at the project root.

From Securing file permissions and ownership.

This can be a challenge on shared hosting, if you are not allowed to set the DocumentRoot, either via configuration or the GUI.

A workaround is placing the Drupal files in a folder outside the public_html folder, and instead serve the /web-folder via public_html using a symlink. This method requires command line access, via SSH or the browser.

Assuming that when you log in on your shared hosting, this is your home folder:

/var/www/example.org

Assuming this is the path to the public_html folder:

/var/www/example.org/public_html

  1. Place your Drupal folder here:
    /var/www/example.org/drupal10
  2. Rename the public_html folder:
    mv public_html public_html_OLD
  3. Create a symlink from public_html to web:
    ln -s /var/www/example.org/drupal10/web /var/www/example.org/public_html

Now, the files in /web will be served via the symlinked public_html-folder. Run Composer and Drush commands from the drupal10 folder, not public_html.

Help improve this page

Page status: No known problems

You can: