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

[6.0] Configure Joomla with environment variables #44894

Closed
wants to merge 53 commits into from

Conversation

voronkovich
Copy link
Contributor

@voronkovich voronkovich commented Feb 13, 2025

Pull Request for Issue #36898.

Summary of Changes

This PR adds an ability to configure Joomla using environment variables. To do this, the config provider has been changed to load all environment variables whose names start with JOOMLA_ (e.g. JOOMLA_DEBUG, JOOMLA_PASSWORD) and merge them into the config service.

Also, environment variables can be loaded from .env files, thankfully to symfony/dotenv.

Testing Instructions

  1. Create .env file in the site's root directory

  2. Set environment variables (inside the .env file):

    JOOMLA_OFFLINE=1
    JOOMLA_OFFLINE_MESSAGE='Joomla!'
  3. Reload site's home page and ensure that site is offline and the Joomla! message is shown

  4. Run the following command and ensure that the output is true:

    cli/joomla.php config:get offline
  5. Run the following command and ensure that the output is Joomla!:

    cli/joomla.php config:get offline_message

Actual result BEFORE applying this Pull Request

All should work.

Expected result AFTER applying this Pull Request

All should work.

Link to documentations

Please select:

  • Documentation link for docs.joomla.org:

  • No documentation changes for docs.joomla.org needed

  • Pull Request link for manual.joomla.org:

  • No documentation changes for manual.joomla.org needed

Why?

The 12-factor app methodology recommends to store config in the environment.

Also, it makes Joomla more cloud-friendly and simplifies running it inside containers.

For example, using environment variables it's very easy to configure separate database for testing:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/Unit/bootstrap.php" colors="false">
        <testsuites>
                <testsuite name="Unit">
                        <directory suffix="Test.php">./tests/Unit</directory>
                </testsuite>
                <testsuite name="Integration">
                        <directory suffix="Test.php">./tests/Integration</directory>
                </testsuite>
        </testsuites>
        <php>
                <env name="JOOMLA_DBHOST" value="localhost" force="true"/>
                <env name="JOOMLA_DBNAME" value="joomla_test" force="true" />
                <env name="JOOMLA_DBUSER" value="joomla_test_user" force="true" />
                <env name="JOOMLA_DBPASSWORD" value="joomla_test_password" force="true" />
        </php>
</phpunit>

Sorry, something went wrong.

@dgrammatiko
Copy link
Contributor

Nice one! One suggestion probably another array with key the JConfig key and value 1 if the value comes from the php file or 0 if the value comes from the env. the reason is that then in the admin configuration the form could check if each input is editable ie coming from the php file (or not ie coming from the env and then add a readonly attribute).

@voronkovich
Copy link
Contributor Author

@dgrammatiko, to check if the config value came from environment we can use the envs_config service:

/** @var Registry */
$envsConfig = $container->get('envs_config');

if ($envsConfig->has('debug')) {
    // 'debug' came from environment
}

@laoneo
Copy link
Member

laoneo commented Feb 20, 2025

Thanks for the pr. It's a nice idea. For me it is a bit too complex. Probably moving the loadEnvs function to the Config service provider is enough. No need to introduce a storage class with static functions.

@voronkovich
Copy link
Contributor Author

@laoneo, without storage and static methods we can't set debug, error_reporting and behind_loadbalanser, because they are configured during application loading in includes/framework.php. So, JOOMLA_DEBUG, JOOMLA_ERROR_REPORTING wouldn't work.

@laoneo
Copy link
Member

laoneo commented Feb 20, 2025

Then load them manually in the respective files.

@voronkovich
Copy link
Contributor Author

@laoneo, I've added the changes you requested.

@laoneo
Copy link
Member

laoneo commented Feb 21, 2025

Just an update here, before you spend more time on it. We are discussing this in the maintainers team if it fits into our current setup. There are are already other ways to customize the config, so this would be another one.

@tekvishal
Copy link

I have tested this item 🔴 unsuccessfully on 2466eae

I have tested this item, issue not resolved


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/44894.

@komalm
Copy link

komalm commented Feb 22, 2025

I have tested this item 🔴 unsuccessfully on 2466eae

Tested on: Joomla! 5.3-dev
Environment: Joomla! 5.3-dev (fresh setup)
Web Server: Apache
PHP Version: PHP 8.3
Database: MySQL

Steps Followed for Testing:

  • Applied the patch via Patch Tester.
  • Edited .htaccess file and added the environment variable: SetEnv JOOMLA_OFFLINE 1
  • Restarted the web server : sudo systemctl restart apache2
  • Opened the Joomla! frontend to check if the site was set to offline mode

Issue Observed:

  • The site did not switch to offline mode.
  • No errors were logged in Joomla! or the server error logs.
  • Environment variables were not detected by Joomla!

Expected Behavior:

  • Joomla! should recognize the JOOMLA_OFFLINE environment variable.
  • The site should display the "Site is offline" message if the variable is set to 1.

Actual Behavior:

  • Joomla! continues running normally, ignoring the JOOMLA_OFFLINE setting.
  • No difference observed before and after applying the patch.

Additional Notes:

  • Clearing Joomla! cache and restarting the server did not resolve the issue.

This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/44894.

@komalm
Copy link

komalm commented Feb 22, 2025

I have tested this item ✅ successfully on 2466eae

Sincerely apologize for my previous negative feedback regarding the issue. It was provided before properly applying the patch, and I realize now that it may not have reflected the actual results accurately.

After correctly applying the patch and retesting, here are my updated findings:

Tested on: Joomla! 5.3-dev
Environment: Joomla! 5.3-dev (fresh setup)
Web Server: Apache
PHP Version: PHP 8.3
Database: MySQL

Steps Followed for Testing:

  • Applied the patch via Patch Tester.
  • Edited .htaccess file and added the environment variable: SetEnv JOOMLA_OFFLINE 1
  • Restarted the web server : sudo systemctl restart apache2
  • Opened the Joomla! frontend to check if the site was set to offline mode

Issue Observed:

  • The site switch to offline mode.
  • No errors were logged in Joomla! or the server error logs.

Expected Behavior & Actual Behavior are same:

  • Joomla! should recognize the JOOMLA_OFFLINE environment variable.
  • The site should display the "Site is offline" message if the variable is set to 1.

Additional Notes:

  • Clearing Joomla! cache and restarting the server did not resolve the issue.

Final Result:

  • Patch works as intended; Joomla correctly recognizes the JOOMLA_OFFLINE variable, and the site goes offline as expected. No issues found.

This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/44894.

muhme and others added 16 commits February 28, 2025 07:33
The `CYPRESS_CACHE_FOLDER` environment variable must be set within the `sudo` command.
* [Cypress] PHP Backend Notice com_media/Files.cy.js

Fixing all Joomla backend PHP notices from Cypress test file `api/com_media/Files.cy.js` and doing some refactoring.

Fixed PHP notices are:
```
[Sat Feb 22 18:28:44.019593 2025] [php:notice] [pid 31:tid 31] [client 10.0.0.1:64714] PHP Notice:  exif_imagetype(): Error reading from /var/www/html/files/test-image-1.jpg! in /var/www/html/libraries/src/Helper/MediaHelper.php on line 93
[Sat Feb 22 18:28:44.022074 2025] [php:notice] [pid 31:tid 31] [client 10.0.0.1:64714] PHP Notice:  getimagesize(): Error reading from /var/www/html/files/test-image-1.jpg! in /var/www/html/libraries/src/Image/Image.php on line 177
[Sat Feb 22 18:28:44.258619 2025] [php:notice] [pid 31:tid 31] [client 10.0.0.1:64714] PHP Notice:  exif_imagetype(): Error reading from /var/www/html/files/test-dir/test-image-1-subfolder.jpg! in /var/www/html/libraries/src/Helper/MediaHelper.php on line 93
[Sat Feb 22 18:28:44.259092 2025] [php:notice] [pid 31:tid 31] [client 10.0.0.1:64714] PHP Notice:  getimagesize(): Error reading from /var/www/html/files/test-dir/test-image-1-subfolder.jpg! in /var/www/html/libraries/src/Image/Image.php on line 177
[Sat Feb 22 18:28:47.177357 2025] [php:notice] [pid 31:tid 31] [client 10.0.0.1:64714] PHP Notice:  exif_imagetype(): Error reading from /var/www/html/files/test-dir/todelete.jpg! in /var/www/html/libraries/src/Helper/MediaHelper.php on line 93
[Sat Feb 22 18:28:47.178099 2025] [php:notice] [pid 31:tid 31] [client 10.0.0.1:64714] PHP Notice:  getimagesize(): Error reading from /var/www/html/files/test-dir/todelete.jpg! in /var/www/html/libraries/src/Image/Image.php on line 177
[Sat Feb 22 18:28:47.564807 2025] [php:notice] [pid 31:tid 31] [client 10.0.0.1:64714] PHP Notice:  exif_imagetype(): Error reading from /var/www/html/images/test-dir/todelete.jpg! in /var/www/html/libraries/src/Helper/MediaHelper.php on line 93
[Sat Feb 22 18:28:47.565288 2025] [php:notice] [pid 31:tid 31] [client 10.0.0.1:64714] PHP Notice:  getimagesize(): Error reading from /var/www/html/images/test-dir/todelete.jpg! in /var/www/html/libraries/src/Image/Image.php on line 177
```

All 8 x PHP notices are fixed. Instead of using 1-Byte files real images are used from Cypress fixtures.
Please consider there are still 4 x PHP warning to be fixed (I assume they need to be fixed in PHP API backend code) from `api/com_media/Files.cy.js` and more warnings and notices in the overall Joomla System Tests.

Some refactoring:
* Deleted 3 images in `tests/System/data/com_media`, 2 are not used and Cypress default is to use `fixtures` folder
* The image file names are counted through and the images show name and path in different colors.
* Second `test-dir` was named `test-dir2` to distinguish clearly.
* `afterEach()` is reduced to `after()`, as it is only a clean-up and not needed to execute the tests.
* The last 4 tests can use the prepared files too, as with `beforeEach` the files are restored before each test.

* Corrected Image Description to test-dir2

Used command:
```
convert -size 100x100 -gravity center -pointsize 11 xc:lightgreen -fill red -annotate +0+0 'Joomla\nSystem Tests\n\nimages/test-dir2\ntest-image-3.jpg' tests/System/fixtures/com_media/test-image-3.jpg
```

* Fix for Windows Path and Read-Only Overwrites

- Creating relative fixtures folder path now also works on Windows with backslashes as separators and a drive letter
- All overwrite files are now created with explicit read-write 0o666, as the default 0o444 creates read-only files, leading to PHP Warnings `Failed to open`

---------

Co-authored-by: Allon Moritz <allon.moritz@digital-peak.com>
Co-authored-by: Robert Deutz <rdeutz@googlemail.com>
)

* Remove unsed variable

* fix as reuested

* Add a comment

---------

Co-authored-by: Allon Moritz <allon.moritz@digital-peak.com>
Co-authored-by: Benjamin Trenkle <bembelimen@users.noreply.github.com>
Co-authored-by: Richard Fath <richard67@users.noreply.github.com>
# JOOMLA_OFFLINE=1
# JOOMLA_SITENAME='Joomla!'
#
# All available configuration values are listed in installation/configuration.php-dist.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it ok to reference this file as it is removed once a site has been installed? Should it maybe refer to a documentation page that is always available?

Co-authored-by: Brian Teeman <brian@teeman.net>
@HLeithner HLeithner changed the base branch from 5.3-dev to 6.0-dev March 4, 2025 17:18
@HLeithner HLeithner requested a review from rdeutz as a code owner March 4, 2025 17:18
@HLeithner
Copy link
Member

This pull request has been automatically rebased to 6.0-dev.

@HLeithner HLeithner changed the title [5.3] Configure Joomla with environment variables [6.0] Configure Joomla with environment variables Mar 4, 2025
@voronkovich voronkovich closed this Mar 4, 2025
@voronkovich voronkovich deleted the config-envs branch March 4, 2025 21:45
@voronkovich
Copy link
Contributor Author

I accidentally removed the branch while rebasing and the PR closed automatically. I will try to figure out how to reopen it later.

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

Successfully merging this pull request may close these issues.

None yet