Unable to get environment variables #1111
Replies: 24 comments
-
@akshaybabloo What application language are you using? if you use python, just import package "os".
same thing for Go, using |
Beta Was this translation helpful? Give feedback.
-
Note that for PHP in order to have access to environment, the |
Beta Was this translation helpful? Give feedback.
-
I am using Python and Django, when I use |
Beta Was this translation helpful? Give feedback.
-
Also, I am using PowerShell Core in Ubuntu 18.04 |
Beta Was this translation helpful? Give feedback.
-
I just installed PowerShell to test this, and it works here.
Try the following:
If this doesn't works maybe it's something wrong on your setup. |
Beta Was this translation helpful? Give feedback.
-
@akshaybabloo How do you start unitd? |
Beta Was this translation helpful? Give feedback.
-
This is what I have in my docker file
|
Beta Was this translation helpful? Give feedback.
-
@akshaybabloo And where you have set the environment variables for the docker? |
Beta Was this translation helpful? Give feedback.
-
The above code is from $Env:DANGO_SECRET_KEY = "<key>"
Import-Module ./Some-More-Keys.psm1 # This file has more $Env:Key="Value"
# ....
unitd
touch /var/log/access.log
curl -X PUT --data-binary @nginx-unit-config.json --unix-socket /var/run/control.unit.sock http://localhost/config/
tail -f /var/log/access.log -f /var/log/unit.log |
Beta Was this translation helpful? Give feedback.
-
@akshaybabloo Please make sure that you have access to these environment variables without Unit, e.g. by running a simple Python script instead of unitd. |
Beta Was this translation helpful? Give feedback.
-
I am able to. Before using Nginx Unit I was running gunicorn and I never had an issue with that |
Beta Was this translation helpful? Give feedback.
-
I have the same issue. I am evaluating Nginx Unit as a replacement for Gunicorn as the WSGI server for a Flask application and from what I can observe it seems the system environment is not available (through Python's os API). The Flask application has been configured to exit, when required environment variables are not defined. As a result, configuring Nginx Unit fails immediately (with "error": "Failed to apply new configuration."). In the Unit logs, I see the Flask application balking at the missing environment variables. We use the common pattern to convey configuration to the application through environment variables—in the end we run it as a Docker container with environment settings. I haven't tried to Dockerize the app yet, but if setting the Unit config will be necessary as part of the build process, I don't see how this can work. A working config environment will only be available at the time we run the image. |
Beta Was this translation helpful? Give feedback.
-
Hi @dannmartens How are you trying to set the environment variables? |
Beta Was this translation helpful? Give feedback.
-
Do you mean |
Beta Was this translation helpful? Give feedback.
-
Hi @tiago4orion, It's not surprising that in my dev set-up, the environment of the dev user is not available to unitd under systemd. For that to work, I'll need to create a config file from a template and a script which derives the actual config file by replacing the needed environment variables— then curl it in purposely to match this dev environment. The process model is completely different in our current Docker image, which starts the WSGI server (Waitress, Gunicorn, Netius, ...) directly as the entrypoint. Environment which is passed on via -e with Docker is available to both the root, as well as any other unprivileged user. I haven't Dockerized the app yet with unitd, but a unitd application under systemd might be able to see those. When I run unitd directly in the dev environment, unprivileged, the Python3 application does see the environment. But now I can't configure the application, unless I use a second terminal to curl it in. What's the best practice for this kind of use case? Perhaps unitd is not intended to be used like this? |
Beta Was this translation helpful? Give feedback.
-
@dannmartens Take a look here: https://unit.nginx.org/installation/#initial-configuration By using the official Unit image, you can drop configurations inside the Let me know if this solves your problem. |
Beta Was this translation helpful? Give feedback.
-
I have issue with mith my Flask app on Unit and I suspect that the environment variables are the reason of it. The error in unit.log Environment variables are in .env file (I use python-dotenv also). Database related variable looks like same error appears in case of |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm testing the use of Unit to run our Symfony app and despite everything looking good in local dev, upon push to the dev server (which is a container setup configured via massive amounts of env vars) our app defaulted to .env file placeholder values. I saw some threads and info around and set I've glanced over what's passed to the container and everything seems normal / the same it was on our previous fcgi container. If this can have unintended consequences I'll be glad to be corrected. I now also wonder how it worked on the fpm container. Passing the pile of 50+ env vars to the container by using envsubst to poppulate a massive unit.json file was the alternative solution if this didn't work, but that would be incredibly annoying for what should be a simple thing to do. Eagerly awaiting comments on the topic from other people |
Beta Was this translation helpful? Give feedback.
-
Hi @p4block, Thank you for reaching out with your issue. To help diagnose and potentially resolve this problem, we would need a minimal use case that can reproduce the issue. Could you please provide us with the following information: The relevant portion of your Symfony configuration related to environment variables, including any code that reads or sets these values. Thank you in advance. |
Beta Was this translation helpful? Give feedback.
-
@andrey-zelenkov Don't worry about it, as far as I know there is no issue just a lack of documentation and difference in behavior. I've dialed it down to a functional difference between fpm and unit,
Those config files set I'll post my things for reference by other people: unit.json
Dockerfile
|
Beta Was this translation helpful? Give feedback.
-
Hi @p4block I am using Unit with CakePHP and Symfony for more than 4 year and in my PHP Admin settings I am always setting some defaults {
"admin": {
"memory_limit": "2G",
"variables_order": "EGPCS",
"expose_php": "0"
},
"user": {
"display_errors": "0"
}
} The see: #969 (comment) I agree we should start adding a section to our documentation where we start from the Language and not the application. Like Unit for PHP Developers should have a bunch of this information and some overview about why is Unit different from PHP-FPM and a setup with Cheers Timo |
Beta Was this translation helpful? Give feedback.
-
Although I like the unit-ful configuration and may switch to it later, right now I am simply using the default paths to configure PHP.
The default php.ini files (prod and dev one) shipped by php inside I'll also share my app specific settings for good measure, I hope this thread gives good value to future passerby.
app.ini.prod
PS: if you could share how you're running your entire setup I would be very thankful |
Beta Was this translation helpful? Give feedback.
-
That would definitely be a welcome addition. Best, Jeff |
Beta Was this translation helpful? Give feedback.
-
Moving this into a discussion to put all out new ideas and comments here! The topic about how Units PHP implementation differs from FPM or others is important to us and we will start working on better documentation and fixes. |
Beta Was this translation helpful? Give feedback.
-
By default, Nginx Unit is not able to get the environment variables unless they are set through
environment
. Is there a way to get the environment variable from the system environment?Beta Was this translation helpful? Give feedback.
All reactions