Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

httpd.conf - Different config files depending on environment in Apache

I am trying to move Apache from Linux servers to Openshift servers based on the Docker Image http:2.4, and the basics are working. But the original servers are in different environments and data centers, so I have to find a way to combine all the different variations of httpd.conf files into one file with conditionals.

I will have Linux environment variables in Openshift similar to this:

oc_env=dev # [or stage or prod]  # environment
oc_loc=dc1 # [or dc2]            # data center

Based on the combinations of these two variables, different settings have to be loaded, probably using include.

I have played around with <If> and Define, but so far I have not been able to get anything to work consistently the way I need it to. Here is my current test - mydir is NOT part of the final design, I'm only using it to test conditionals easier:

Define mydir "/usr/local/apache2/htdocs"  # set the default
<If "osenv('oc_env') == 'dev'">
  LogMessage "dev"
  Define mydir "/usr/local/apache2/htdocs/dev"  # override default
</If>
LogMessage "mydir is set to"
LogMessage ${mydir}
DocumentRoot ${mydir}

If I set oc_env (in Openshift, so it will show up in env within the Linux environment), here is what happens in the log file:

oc_env=dev

mydir is set to 
/usr/local/apache2/htdocs/dev
dev

oc_env=other

mydir is set to
/usr/local/apache2/htdocs/dev

So the Define command is executed a second time even if the conditional is false, and it is using the wrong definition.

I'm also confused about the order in which the entries show up (LogMessage "dev" shows at the end although it is earlier in the code).

In the end, I need to have a httpd.conf file with multiple areas where it needs to run a check like this:

# section 1
"if oc_env is dev and oc_loc is dc1 then load external config file dev_dc1_part1.conf else if ....."
[...]
# section 2
"if oc_env is dev and oc_loc is dc1 then load external config file dev_dc1_part2.conf else if ....."

The current file is pretty complex.

I'm probably on a completely wrong track with this. My next attempts will revolve around SetEnvIf, but after having worked on this for a few days now, I am running short on time, so I'm hoping someone can point me in the right direction.

Thanks!

--- update ---

SetEnvIf doesn't seem to be the right tool for the job, either.

So I tried Include, and now I'm more confused than ever...

<If "osenv('oc_env') == 'dev'">
  LogMessage "dev"
  Include /usr/local/apache2/conf/inc_dev.conf
    # file content: Define mydir "/usr/local/apache2/htdocs/dev"
</If>
<ElseIf "osenv('oc_env') == 'stage'">
  LogMessage "stage"
  Include /usr/local/apache2/conf/inc_stage.conf
    # file content: Define mydir "/usr/local/apache2/htdocs"
</ElseIf>
<Else>
  LogMessage "test"
  Include /usr/local/apache2/conf/inc_test.conf
    # file content: Define mydir "/usr/local/apache2/htdocs"
</Else>

LogMessage ${mydir}

The result:

/usr/local/apache2/htdocs
dev

How is this even possible? Is Include ALWAYS executed even if the If clause doesn't apply to it?

Please let me know what I can do to make this work.

question from:https://stackoverflow.com/questions/65891280/different-config-files-depending-on-environment-in-apache

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...