Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document limitation of uri option in non-global drush config file #6220

Open
gitressa opened this issue Feb 6, 2025 · 21 comments · May be fixed by #6223
Open

Document limitation of uri option in non-global drush config file #6220

gitressa opened this issue Feb 6, 2025 · 21 comments · May be fixed by #6223

Comments

@gitressa
Copy link
Contributor

gitressa commented Feb 6, 2025

Existing documentation

What are you attempting to do

Run drush user:login and automatically have the domain defined, via a configuration file, settings.php, or some other way.

The drush user:login command could be in the top 5 most used commands, yet you have to append the domain, like drush user:login -l https://example.org, which gets tiring ...

In Drupal 7 it was possible to set the default domain for a Drupal project in settings.php. DDEV sets a base URL by defining DRUSH_OPTIONS_URI in its Docker files, which works well.

However, setting a base URL on a production server with multiple Drupal 10 installations is difficult.

It would be great if a method to set the site URI for a Drupal 10 site was defined, I currently get this:

$ drush core:status
Drupal version   : 10.4.1                                                    
Site URI         : http://default
[...]

In what way is the existing documentation unclear or incomplete

I cannot find a clear list of steps on how to set the site URI for a Drupal 10 project, on a server hosting multiple Drupal 10 sites.

What should the documentation say instead?

It should contain a method on how to set the default domain for a Drupal 10 project.

As a follow up issue, a Drupal 11 issue could be created, suggesting that a new URI field was added under Basic Site settings (/admin/config/system/site-information) allowing the user to set the default domain. If it was added, I guess it could then also be set via settings.php, since it would then be an accessible variable.

@weitzman
Copy link
Member

weitzman commented Feb 6, 2025

You can specify this as a global option in a drush config file or in an equivalent environment variable

@weitzman weitzman closed this as completed Feb 6, 2025
@gitressa
Copy link
Contributor Author

gitressa commented Feb 6, 2025

Thanks for a fast answer.

On a server, I tried adding a web/sites/default/drush.yml file, which gets registered, but does not seem to take effect -- I still get http://default and not http://example.com after running drush st:

$ drush status --fields=drush-conf
Drush configs : /var/www/example.org/public_html/vendor/drush/drush/drush.yml 
                sites/default/drush.yml

$ cat web/sites/default/drush.yml 
#This is a Drush config file. Sites may override this config to change minimum PHP.
drush:
  php:
    minimum-version: 8.2.5
options:
  # Specify the base_url that should be used when generating links.
  uri: 'http://example.com'

$ drush st
Drupal version   : 10.4.1
Site URI         : http://default

PS. I was puzzled, when I saw in DDEV that Site URI was populated correctly ... but then realized, that's because it's defined in .ddev-docker-compose-base.yaml and .ddev-docker-compose-full.yaml via DRUSH_OPTIONS_URI.

@gitressa
Copy link
Contributor Author

gitressa commented Feb 6, 2025

Also, I just posted this in Allow defining a base URL since it looks to me like the base URL can be seen by Drupal 10 ... Why cannot Drush also see it then?

It just dawned on me that "Default front page" shows the actual base URL ...

I wonder why that variable is not then used in Drush and XML Sitemap (#2832471: Export the default base URL as configuration)?

From https://www.drupal.org/project/drupal/issues/3504766#comment-15976324

@weitzman
Copy link
Member

weitzman commented Feb 6, 2025

sites/default is fine for most drush config but not for --uri. you need to use a global location (or env variable), not a site-specific one (i.e the default dir). thats because uri is used by drupal to select which multi-site gets bootstrapped. Overall, DRUSH_OPTIONS_URI is usually a better approach, as evidenced by the fact that DDEV and web hosts like Patheon and Acquia use it.

'Default front page' is just mirroring back to you the host you are currently using. There is no stored Drupal setting or config for this.

@gitressa
Copy link
Contributor Author

gitressa commented Feb 6, 2025

I see, thanks for a swift and thorough explanation. To me, the question is then -- if Drupal can mirror back the host I am currently using, why cannot Drush also do this?

It would make every day use much easier for Drush users.

I do think also, that your explanation why I failed is important, since probably many others have tried, but also failed ... so perhaps it can be added to the Drush config doc page?

I will now try to get it working based on your explanation, which I am grateful for. But it does seem extra difficult, so an automatic detection of base URL would be an amazing new feature, if at all possible.

@weitzman
Copy link
Member

weitzman commented Feb 6, 2025

I see, thanks for a swift and thorough explanation. To me, the question is then -- if Drupal can mirror back the host I am currently using, why cannot Drush also do this?

Because Drupal is accessed via a web browser, which inherently presents its hostname. A CLI request does not.

Yes, more docs would be helpful. They should point to the documented env variables approach.

@gitressa
Copy link
Contributor Author

gitressa commented Feb 6, 2025

Thanks, I'll have a look at it. Perhaps you can reopen this issue, since it needs work?

Though, it's not clear to me in which file I am supposed to put DRUSH_OPTIONS_URI and how to make Drush discover it, reading https://www.drush.org/13.x/using-drush-configuration/#environment-variables ...

@weitzman
Copy link
Member

weitzman commented Feb 6, 2025

Environment variables are an operating system feature, unrelated to Drush.

@weitzman weitzman reopened this Feb 6, 2025
@weitzman weitzman changed the title How to define Site URI (default domain) for a Drupal 10 site? Document limitation of uri option in non-global drush config file Feb 6, 2025
@weitzman
Copy link
Member

weitzman commented Feb 6, 2025

I am happy to link to somewhere on the web that describes what env variables are and how to set them

@gitressa
Copy link
Contributor Author

gitressa commented Feb 6, 2025

Sure, that would be great. Thanks.

@gitressa
Copy link
Contributor Author

gitressa commented Feb 6, 2025

The easiest would be, if the minimal required steps to set this up was included in the documentation ...

But I am also all right with piecing it together, step-by-step ...

@gitressa
Copy link
Contributor Author

gitressa commented Feb 6, 2025

Like I wrote, also -- it's not just about setting up a environment variable, but how to connect it all, and have Drush discover it.

I add this, since I sense a " Let me Google that for you"-tone in your last comment, but I could be mistaken.

@weitzman
Copy link
Member

weitzman commented Feb 6, 2025

PHP already reads environment variables. So if you set a properly named env variable, Drush will discover it. No more work on the user's part is needed.

@gitressa
Copy link
Contributor Author

gitressa commented Feb 6, 2025

Ok, I am ready for a link, where I can read how to do it, and then try to make it work. I tried adding a web/.env file, but it didn't get picked up by Drush.

@weitzman
Copy link
Member

weitzman commented Feb 6, 2025

it didn't get picked up by Drush.

You mean that it didnt get picked up by your operating system. Drush is not involved in setting env variables - It only reads what from the environment you have configured elsewhere. You can see the env variables are set via the env shell command. Maybe https://www3.ntu.edu.sg/home/ehchua/programming/howto/Environment_Variables.html is helpful.

@gitressa
Copy link
Contributor Author

gitressa commented Feb 6, 2025

Thanks! Assuming that a lot of Drupal web sites are hosted on for example Debian, do you know how I would set it up in that environment? If you don't know, maybe someone else can help out with clearing this up?

Ideally, we can end up finding the minimal steps, which results in base URL being defined as an environment variable.

I will then add the steps to the Drush documentation page on drupal.org:
https://www.drupal.org/docs/develop/development-tools/drush

@gitressa
Copy link
Contributor Author

gitressa commented Feb 7, 2025

Is it something like the below example you mean? (Made in DDEV) That is not a great method, since it only works with a single Drupal installation present, and in a lot of set ups, there will be more than one Drupal 10 installation on the server.

user@laptop:~/dev/drupal$ ddev ssh
# insert DRUSH_OPTIONS_URI=https://example.org in ~/.bashrc and source it
user@drupal-web:~$ source ~/.bashrc 
user@drupal-web:~$ drush st
Drupal version   : 11.1.2                                     
Site URI         : https://example.org
[...]

@weitzman
Copy link
Member

weitzman commented Feb 7, 2025

Well, DDDEV handles this for you. It already sets DRUSH_OPTIONS_URI.

Your doc works for Unix. Mac shell is ZSH these days so its a different file. And Windows has its own permutations. And Docker based hosts have own ways to set env variables.

For the multiple codebases on same server situation, I have found https://direnv.net/ helpful. With this, your env variables can vary depending on the directory you are in when you issue a CLI request.

@gitressa
Copy link
Contributor Author

gitressa commented Feb 7, 2025

Yes I know, I just used DDEV as a proof of concept, showing the steps taken, so that others could easily replicate them.

The aim of this issue is to document how to set the base URL on a production server with multiple Drupal 10 installations. I didn't specify this in the Issue Summary, since I didn't think it wasn't relevant at that point, but I'll update the Issue Summary.

Thanks for the link to Direnv which works well, and we could add info about it to the documentation.

I installed Direnv and defined a new base URL in a .envrc file in the Drupal 10 folder on a server (Note: This is not really possible to test on DDEV, I think):

~/dev/drupal$ cat .envrc
export DRUSH_OPTIONS_URI=https://example.org

Test if it works:

~/dev/drupal$ echo ${DRUSH_OPTIONS_URI-nope}
https://example.org

~/dev/drupal$ drush st
Drupal version   : 10.4.1
Site URI         : https://example.org

@weitzman
Copy link
Member

weitzman commented Feb 7, 2025

I think direnv would work inside of ddev container. It would overwrite the DRUSH_OPTIONS_URI value provided by DDEV itself. I could see a multi-site wanting to use it.

gitressa added a commit to gitressa/drush that referenced this issue Feb 7, 2025
…multiple Drupal 10 installations

## The Issue

- Fixes drush-ops#6220
@gitressa
Copy link
Contributor Author

gitressa commented Feb 7, 2025

I tried in DDEV, but it got ignored, as far as I could tell ... but I didn't spend a lot of time experimenting with it.

I have created a PR which adds info about Direnv.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants