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

[2.x.x] Production mode is not detected in CLI => the uri for media and static are not good and contain pub #8868

Closed
spipu opened this issue Mar 13, 2017 · 16 comments
Assignees
Labels
Event: distributed-cd Distributed Contribution Day Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release

Comments

@spipu
Copy link

spipu commented Mar 13, 2017

This is a technical issue. It occurs on every edition (CE an EE) and every version (2.0.0 -> 2.1.5) of Magento.

When we are in Production Mode, and if we use the pub folder as main public folder, then all the static url and media url must not contain the pub part. That's normal.

It is not a automatic switch made on the magento mode value, but its depends on the index.php file that is used.

In the file ./pub/index.php, it overrides the directoryList's config (given to the bootstrap) with the following :

$params[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] = [
    DirectoryList::PUB => [DirectoryList::URL_PATH => ''],
    DirectoryList::MEDIA => [DirectoryList::URL_PATH => 'media'],
    DirectoryList::STATIC_VIEW => [DirectoryList::URL_PATH => 'static'],
    DirectoryList::UPLOAD => [DirectoryList::URL_PATH => 'media/upload'],
];

https://github.com/magento/magento2/blob/2.1/pub/index.php#L27

This configuration overriding is not present in the developer mode index.php :

https://github.com/magento/magento2/blob/2.1/index.php#L36

The pb is that when we are in production mode, if we ask for a static url or a media url in CLI, then the urls are not good and contain the pub part.

It is because, in CLi, there is no switch that will detect if we are in production or not, and if the directoryList configuration have to be overridden or not.

It could be in Magento\Framework\Console\Cli::getApplicationCommands at line 127:

https://github.com/magento/magento2/blob/2.1/lib/internal/Magento/Framework/Console/Cli.php#L127

=> For now, it is impossible to send emails in CLI if we are in production mode, because the website logo will not work...

Preconditions

  1. Magento 2.
  2. Directory ./pub set as server document root.
  3. Created custom console command that returns base media url.

Steps to reproduce

  1. Open any image on your Magento site. make sure its url has no "pub" in path.
  2. Run console command that returns base media url.

Expected result

Command output does not contain "pub"

Actual result

Command output contains "pub"

@scottsb
Copy link
Member

scottsb commented Apr 5, 2017

This also affects stock alert emails sent by cron, as both the CLI and cron.php entry points are missing the explicit directory path override that index.php has.

@scottsb
Copy link
Member

scottsb commented Apr 5, 2017

Finally found workaround:
http://magento.stackexchange.com/a/167869/567

@spipu
Copy link
Author

spipu commented May 12, 2017

but this is just a workaround...

It will be better if it was automatic...

@magento-engcom-team magento-engcom-team added the Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed label Sep 11, 2017
@magento-engcom-team magento-engcom-team added Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed 2.1.x Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development and removed Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed labels Nov 29, 2017
@magento-engcom-team
Copy link
Contributor

@spipu, thank you for your report.
We've created internal ticket(s) MAGETWO-84709 to track progress on the issue.

@magento-engcom-team magento-engcom-team added Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release labels Nov 29, 2017
@okorshenko okorshenko removed the 2.1.x label Dec 14, 2017
@magento-engcom-team
Copy link
Contributor

Additional use case: #9111

@magento-engcom-team magento-engcom-team added the Event: distributed-cd Distributed Contribution Day label Mar 19, 2018
@Tomasz-Silpion
Copy link
Member

There's no possibility to assign correct paths automatically as it was mentioned by @spipu
Running bin/magento from CLI we are not aware of nginx/apache document root.
It would be nice to have an easy access to the store config to change hardcoded DirectoryList::URL_PATHs as it is done in /pub/index.php but we are before ObjectManager initialization here.

Cloning bin/magento file to pub is not good enough solution so my proposal is to store custom document_root variable in application.config.php

#distributed-cd

@magento-engcom-team
Copy link
Contributor

@Tomasz-Silpion thank you for joining. Please accept team invitation here and self-assign the issue.

@harrigo
Copy link

harrigo commented Jun 6, 2018

This bug has shown up for me a few times in different situations and is starting to become quite annoying.

First noticed in sitemaps when generated via cron and now for an email module I am writing where images paths are incorrect. Has anyone a simple workaround for this yet? Not sure how to use workaround mentioned by @scottsb to set environment variables.

@hostep
Copy link
Contributor

hostep commented Jun 6, 2018

@harrigo: can you try this workaround: #5321 (comment), it used to work at the time of writing, but it's been a while since I last tested this, so not sure anymore.

@harrigo
Copy link

harrigo commented Jun 7, 2018

@hostep Yeah thanks for the tip think i tried that and not had much luck i will however give another go see how it goes.

Any idea how I set them environment variables for crontab? I have tried by setting them in /etc/environment and within crontab itself as stated here https://stackoverflow.com/questions/2229825/where-can-i-set-environment-variables-that-crontab-will-use

Am not actually sure of what $app_root should be set like and am assuming: /var/www/magento2/public_html/pub if pub my root trying to use this workaround: https://magento.stackexchange.com/questions/138305/mage-dirs-bootstrap-parameters-in-cli/167869#167869

@scottsb
Copy link
Member

scottsb commented Jun 7, 2018

Hi @harrigo. We're following each other around. In the workaround I suggest over on StackExchange, $APP_ROOT should be the root of your Magento install, not Magento's pub directory. So probably /var/www/magento2/public_html in your case.

@harrigo
Copy link

harrigo commented Jun 7, 2018

@scottsb haha seems so this is bugging me so just gone everywhere! I think my problem more now how to set that. I tried within etc/environment and within the scripts themselves however am at a loss and about to give up now. MAGE_DIRS[base][path]="/var/www/mage2/public_html" not working but that was just a guess based on you setting them in command line.

@magento-engcom-team
Copy link
Contributor

Hi @peterjaap. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if your want to validate it one more time, please, go though the following instruction:

  • 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 2. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 3. Verify that the issue is reproducible on 2.2-develop branch.

    Details- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x

  • 4. If the issue is not relevant or is not reproducible any more, feel free to close it.

@olivermontes
Copy link

Any update? :)

@nik4152 nik4152 self-assigned this Sep 27, 2019
@m2-assistant
Copy link

m2-assistant bot commented Sep 27, 2019

Hi @nik4152. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:

  • 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 2. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 3. If the issue is not relevant or is not reproducible any more, feel free to close it.


@nik4152
Copy link

nik4152 commented Sep 27, 2019

@spipu Hi you need add code to file
app/etc/env.php

<?php
return [
    ...
    'directories' => [
        'document_root_is_pub' => true
    ],
    ...
];

`

@nik4152 nik4152 closed this as completed Sep 27, 2019
magento-devops-reposync-svc pushed a commit that referenced this issue May 14, 2024
[Bengals Team] Test Fixes and Automation Tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Event: distributed-cd Distributed Contribution Day Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release
Projects
None yet
Development

No branches or pull requests

10 participants