Score:0

Run composer installed drupal from root (/) and not /web directory

na flag

How do I run a composer-installed Drupal site from / instead of /web?

The default install with composer create-project drupal/recommended-project ./ will install Drupal in the /web directory. So when the site is uploaded to a host, the actual website root will be www.example.com/web. In most cases, the desired path to run the website is www.example.com/.

I have managed to do this, like this. And actually, I am asking if this is the definite correct way. (Clearly, I used my domain, not example.com.)

I have created a .htaccess file in the root with the following content.

RewriteEngine on
RewriteRule (.*) web/$1 [L]

I have uncommented the RewriteBase directive.

RewriteBase /web

I have added these lines in the web/sites/default/settings.php file.

if (isset($GLOBALS['request']) and
  '/web/index.php' === $GLOBALS['request']->server->get('SCRIPT_NAME')) {
  $GLOBALS['request']->server->set('SCRIPT_NAME', '/index.php');
}

I have also uncommented these lines in the web/sites/default/settings.php file to cover the trusted hosts issue.

$settings['trusted_host_patterns'] = [
  '^example\.com$',
  '^.+\.example\.com$',
];

Is this the correct way?

id flag
These questions seem similar: https://drupal.stackexchange.com/questions/294935/recommended-project-installs-drupal-into-the-web-subdirectory-but-hosting-so, https://drupal.stackexchange.com/questions/238550/how-to-remove-subdir-from-base-url-after-installing-site-with-composer-on-a-shar?rq=1, https://drupal.stackexchange.com/questions/260337/how-do-i-remove-web-from-the-url?noredirect=1&lq=1
pierostz avatar
na flag
Yeap...[This answer](https://drupal.stackexchange.com/questions/238550/how-to-remove-subdir-from-base-url-after-installing-site-with-composer-on-a-shar?rq=1) is pretty close where I am. But again even the poster of that answer is not sure about it. Really frustrating to be honest trying to achieve something so simple and critical.
id flag
The recommended-project is essentially this one file: https://github.com/drupal/recommended-project/blob/9.5.x/composer.json. Once used to set up a site initially, it is never updated without your knowledge. You can edit it as needed after that.
pierostz avatar
na flag
So the correct way is to edit the paths in the composer.json after the installation?
unusedspoon avatar
aq flag
Doesn't this pretty much cover exactly what you're asking for? https://www.drupal.org/docs/develop/using-composer/manage-dependencies#s-to-do-a-modified-install
Score:3
us flag

web is assumed to be the server document root directory. In some cases, the server document root directory is a different one, for example public_html.

You could either change the server settings to make the server use web as server document root directory, or change the commands executed, if you cannot do that.

As reported in Using Composer to Install Drupal and Manage Dependencies / To do a modified install, instead of running composer create-project drupal/recommended-project ./, you need to:

  • Run cd <directory containing the server document root directory>
  • Run composer create-project --no-install drupal/recommended-project ./
  • Change any reference to web found in the composer.json file with the server document root directory
  • Run composer install

For example, with the Linux distribution I am using (Endeavour OS), the default server documentation directory (indicated by httpd -S as Main DocumentRoot) is /srv/http. To install Drupal in that directory, I would execute the following commands.

cd /srv
composer create-project --no-install drupal/recommended-project ./

After, I would edit the composer.json file to change any occurrence of web with http. In the case the directory web is indicated with its full path, I would change that accordingly. Then, I would run composer install.

pierostz avatar
na flag
You are correct but there's an issue with this method. [See here](https://drupal.stackexchange.com/questions/315154/composer-modified-installation-with-changed-web-reference-in-composer-json-fail)
Score:0
cn flag

Is this the correct way?

No, it's not and it's a security risk.

Ideally, you would configure the web server to point the domain www.example.com to the sub-folder /var/www/my-project/web (or any other subfolder you have configured in composer.json). All web servers support this. If it's your own server configure a virtual host, hosting providers usually have a nice UI to manage these things.

Only if this is not possible use the legacy project:

composer create-project drupal/legacy-project .

It's important to use this project and not try to move the web folder yourself to root, because this project has extra protection for the vendor folder now being exposed in www.example.com/vendor.

This is the line that needs to be present in composer.json:

"require": {
    "drupal/core-vendor-hardening": "^10.0"

See https://github.com/drupal/legacy-project/blob/10.0.5/composer.json#L22

To be clear, the problem here is not renaming the subfolder, for example from /web to /public_html. It's about the original question, running Drupal without a subfolder from the web server's root.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.