You are not logged in.

#1 2017-07-15 19:07:57

loadlover
Member
Registered: 2017-05-10
Posts: 103

How to pass a Bash shell environment variable through to Apache?

Hi all,

How does one pass a Bash shell environment variable through to Apache?

i.e. so that I can make use of its value in Apache's httpd.conf file.

Mucho tak,

Loady

Offline

#2 2017-07-15 19:15:43

adesh
Member
Registered: 2016-10-05
Posts: 167

Re: How to pass a Bash shell environment variable through to Apache?

Offline

#3 2017-07-15 19:28:54

loqs
Member
Registered: 2014-03-06
Posts: 17,321

Re: How to pass a Bash shell environment variable through to Apache?

How are you starting apache?

Offline

#4 2017-07-15 19:56:49

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: How to pass a Bash shell environment variable through to Apache?

You need to give a lot more information if you want useful help.  This sounds like an XY problem.  What are you actually trying to do?

Even if/when you pass an environment variable to the apache process, that doesn't mean you can magically use the variable in http.conf.  I don't know if http.conf is parsed by a shell interpreter - in fact I doubt it is as it can be used on various OSs.  But apache does have it's own concept of variables (at least two different concepts in fact) that can be used in the config file - but these do not work (exactly) like shell variables.

There are ways ways to use environment variables to influence how the config file is read (see httpd's -C and -D flags for hints on two approaches), but again this all really depends on what your intended outcome is.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2017-07-15 20:04:31

loadlover
Member
Registered: 2017-05-10
Posts: 103

Re: How to pass a Bash shell environment variable through to Apache?

Hmmm, still having some trouble here..

loqs wrote:

How are you starting apache?

I simply enabled it when I first installed Arch, i.e. with:

systemctl enable httpd.service

Anyway, I just tried the following: I first set up an env variable called IDENT_TEST in Bash. As such, IDENT_TEST=/stuff (I checked using the set command).

Next, I created ~/.pam_environment and included the following line:

APACHE_IDENT_TEST	DEFAULT=${IDENT_TEST}

Then I restarted Apache - systemctl restart httpd.service - and it's not working. APACHE_IDENT_TEST doesn't seem to be initialized correctly.. How to check?

Thanks!

Offline

#6 2017-07-15 20:13:46

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: How to pass a Bash shell environment variable through to Apache?

Where did you set IDENT_TEST?  You said you set in in "bash" - if httpd is not started from that same bash process, then it cannot inherit the variable.  Environment variables are not global - they are local to each environment and passed from parent to child process.  If you want a variable set in httpd's environment it needs to be set in the parent process that launches httpd.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2017-07-15 20:20:20

progandy
Member
Registered: 2012-05-17
Posts: 5,190

Re: How to pass a Bash shell environment variable through to Apache?

You are running the httpd service as a child of systemd, so you'll have to set the variable using "Environment" or "EnvironmentFile" inside your .service file or an overlay. It is also possible to temporarily set a systemd-wide variable with "systemctl set-environment" or permanently with "DefaultEnvironment" in system.conf(.d/), but that will be visible to all newly started services.

Still, the question remains. Why do you want or need to read a custom environment variable inside your apache configuration?

Last edited by progandy (2017-07-15 20:26:43)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#8 2017-07-15 20:34:24

seth
Member
Registered: 2012-09-03
Posts: 51,017

Re: How to pass a Bash shell environment variable through to Apache?

WTF?

I first set up an env variable called IDENT_TEST in Bash

How exactly?

I created ~/.pam_environment

And do you run httpd as "your" user?

included the following line

Where did you pick this nonsensical syntax?

I suggest you drop what you're trying to do and explain what you actually (ultimately) want to do.
If you're launching httpd as system service with the system boot (as you suggested on comment #5) you can pass it an environment variable using the "Environment=" key in the service definition, see https://www.freedesktop.org/software/sy … .exec.html
However, there's hardly a point in passing a somewhat static  variable to the httpd.conf - you could just put the value there.

==> Thus: what do you actually want to do?

@Trilby, httpd.conf indeed handles ${ENVVAR} - http://httpd.apache.org/docs/2.2/en/con … tml#syntax

Offline

#9 2017-07-15 20:37:04

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: How to pass a Bash shell environment variable through to Apache?

Yes, but not $ENVVAR, or ${ENVVAR:-default}, or ${ENVVAR/from/to} ...

There are *many* ways to use environment varianles in the config file, but they are all apache-specific as the file is not parsed by the shell - so the question on a "Bash shell environment variable" was problematic.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#10 2017-07-24 12:07:59

loadlover
Member
Registered: 2017-05-10
Posts: 103

Re: How to pass a Bash shell environment variable through to Apache?

"The values of shell environment variables can be used in configuration file lines using the syntax ${ENVVAR}. If "ENVVAR" is the name of a valid environment variable, the value of that variable is substituted into that spot in the configuration file line, and processing continues as if that text were found directly in the configuration file. (If the ENVVAR variable is not found, the characters "${ENVVAR}" are left unchanged for use by later stages in the config file processing.)"

This is what I was getting at. Excuse my imprecise language. I'm learning.

Offline

#11 2017-07-24 12:41:40

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: How to pass a Bash shell environment variable through to Apache?

So is this solved?  If so, please mark it as such.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

Board footer

Powered by FluxBB