2

I am using docker file for PHP Apache. I declare some environments variable in docker file but when I run the docker its only get Apache environment variable while my environment variable is not working. here is my docker file.

# Install     dependencies
From ubuntu:16.04
RUN apt-get update -y
RUN apt-get update && apt-get -y upgrade && DEBIAN_FRONTEND=noninteractive apt-get -y install \
    vim apache2 php7.0 php7.0-mysql php7.0-gd php7.0-mbstring libapache2-mod-php7.0 curl lynx-cur

RUN service apache2 stop

# Install app
RUN rm -rf /var/www/html/*
ADD . /var/www/html

# Configure apache
RUN a2enmod php7.0
RUN a2enmod rewrite
RUN chown -R www-data:www-data /var/www/html/backend/runtime /var/www/html/backend/web/assets /var/www/html/uploads
RUN sed -i '/<Directory \/var\/www\/>/,/<\/Directory>/ s/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf

ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2.pidd
ENV test myenv_variable_value

EXPOSE 80

RUN service apache2 start

CMD ["apache2", "-D", "FOREGROUND"]

While I can easily print APACHE_PID_FILE but when I tried test its not working. here is my PHP method

<?php
    getenv('APACHE_PID_FILE');
    $_ENV['test']
    getenv('test');
?>

These both method are not working in case of test env.

Thanks

4
  • What do you expect from this php snippet? Commented Aug 25, 2017 at 7:22
  • i want to access value of env Commented Aug 25, 2017 at 7:26
  • the snippet is incomplete and will not work, can you post the real snippet? What is the output of print_r($_ENV);? Commented Aug 25, 2017 at 7:30
  • no output with this Commented Aug 25, 2017 at 7:43

1 Answer 1

2

The problem is Apache doesn't expose environment variables which apache was launched with. You need to expose these environment variable using your config. Apache has a PassEnv directive that you need to use

https://httpd.apache.org/docs/current/mod/mod_env.html

PassEnv test

This will pass the environment test to your page. You need it for do it for every environment you want to expose.

Sign up to request clarification or add additional context in comments.

4 Comments

What version of apache?
check the version Server version: Apache/2.4.18 (Ubuntu) Server built: 2017-07-27T14:34:01
This need to go inside your apache config httpd.conf or apache2.conf based on the distribution and NOT in your Dockerfile or any command.
That is because Apache only allows certain environment variables to be exposed by default, that is why they work. It won't allow any other environment variable until unless you config tells it to have one. Makes sense?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.