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

Created on 13 Mar 2017  路  16Comments  路  Source: magento/magento2

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"

distributed-cd Clear Description Confirmed Format is valid Ready for Work Reproduced on 2.1.x Reproduced on 2.2.x Reproduced on 2.3.x

Most helpful comment

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

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

`

All 16 comments

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.

but this is just a workaround...

It will be better if it was automatic...

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

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

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

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.

@harrigo: can you try this workaround: https://github.com/magento/magento2/issues/5321#issuecomment-310153777, it used to work at the time of writing, but it's been a while since I last tested this, so not sure anymore.

@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

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.

@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.

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.

Any update? :)

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.


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

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

`

Was this page helpful?
0 / 5 - 0 ratings