How to pass Docker Compose variables to Apache and PHP? - php

Let's say I have a variable TEST: test passed in docker-compose.yml to a container. From the shell inside the container I can see by echoing that the variable really is passed. However, Apache (2.2) does not seem to receive it. For example, PassEnv TEST fails:
Starting httpd: [Thu Nov 30 18:22:28 2017] [warn] PassEnv variable TEST was undefined
Also $_ENV["TEST"] stays empty.
mod_env is loaded properly.
How do I get Apache to read the variables passed via docker-compose.yml, and how do I pass those to PHP?
What are the settings in Apache and PHP that block the passing of system environment variables?
I'm running CentOS 6.9, Apache 2.2, and PHP 5.3.3.

Related

docker image for php is not working on browser for windows 10

I just started to learn docker and tried to create a php image with apache using docker file. As per my understanding I am able to create image using the docker file and after building image I tried to run that image in the form of container and it looks fine to me but when I tried to run that on the browser using "172.17.0.2" IP address its showing The connection was reset issue and i tried a lot but not able to resolve this issue. I am attaching few images for to clearly mention my issue.
Dockerfile:
FROM php:7.2-apache
COPY src/ /var/www/html/
and the screen of windows pwershell showing
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message [Sat Oct 13 17:42:22.816118 2018] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.25 (Debian) PHP/7.2.10 configured -- resuming normal operations [Sat Oct 13 17:42:22.817371 2018] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
Any help would be appreciated, thanks.
I am able to resolve the issue now, there were two problems:
(1) I was trying to reach an incorrect IP the correct was 127.0.0.1
(2) I wasn't running windows power shell as admin to run docker commands.
One more thing if anyone still faces the same issue than make sure you configured to access shared drive for docker containers through docker interface.
I also worked through the https://hub.docker.com/_/php apache example. I solved the issue by clarifying ownership of the php sources by adding a third line to the Dockerfile:
FROM php:7.2-apache
COPY src/ /var/www/html/
RUN chown -R www-data:www-data /var/www/html

How to restart apache2 without terminating docker container?

I am using as a base the php docker container with the tag:
php:5.6-apache
When I try to restart the apache2 inside the container, the container stops:
root#phalconapp:/var/www/html# service apache2 restart
Restarting web server: apache2Terminated
root#phaclonapp:/var/www/html#
me#myLocalComputer:
How to restart apache2 without stopping the container?
I want to play around with the container and customize it, before I put my changes into the dockerfile. I want to install some extension and for them to work I need to restart apache for the changes to take effect.
This is the log file via:
Attaching to dltasklight_phlaconapp_1
phlaconapp_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
phlaconapp_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
phlaconapp_1 | [Mon May 30 10:19:24.556154 2016] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.10 (Debian) PHP/5.6.22 configured -- resuming normal operations
phlaconapp_1 | [Mon May 30 10:19:24.556181 2016] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
phlaconapp_1 | [Mon May 30 10:21:11.754993 2016] [mpm_prefork:notice] [pid 1] AH00169: caught SIGTERM, shutting down
dltasklight_phlaconapp_1 exited with code 0
If you use apache as the primary service to keep your running container, you can NOT reboot it. Simply because you built the image and sets the CMD with it.
The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.
Try to reload without restart a service:
/etc/init.d/apache2 reload
My solution to this was to exit my bash shell into the container, and just restart the container outside of Docker. Because Apache is set as the primary service, this also restarts Apache, and doesn't crash the container.
docker restart <container>
sudo docker kill --signal="USR1" your_appache_container
Other signals that you can use to achieve the following :
Stop Now Signal: TERM
Graceful Restart Signal: USR1
Restart Now Signal: HUP
Graceful Stop Signal: WINCH
From: this website
I want to customize the container, I need to install some extension
and for them to work I need to restart apache for the changes to take
effect.
This is against the Docker's immutable infrastructure principle. IMHO, you are using the docker container similar to a full blown VM. Instead, I would suggest you to treat the docker image as the final artifact and version it.
Note: This is just my humble opinion, you may have a valid usecase which I am not aware of, which I am curious to find out.
Inside docker, I use a command like this:
exec /usr/sbin/httpd -D FOREGROUND
But before going, does your apache fails while doing the restart? Thats how it exits? In that case, please do make it run by setting right configurations and having a look at those logs.
One method you can try is login to the container (to bash) and you could always have a docker commit at a point where it works. You can then change your base container image to that.
I had workarounds for similar situations by building new images from these committed ones using different ENTRYPOINT's in my Dockefile.

Setting up LAMP with CentOS 6.x

I am having errors setting up my webserver on my vps. At the moment I have followed this tutorial: https://www.howtoforge.com/apache_php_mysql_on_centos_6.5_lamp
I am stuck on the /etc/init.d/httpd start part. When I run this command, I get the following output:
[root#localhost conf]# service httpd restart
Stopping httpd: [FAILED]
Starting httpd: [Mon Feb 23 21:27:02 2015] [warn] NameVirtualHost *:80 has no VirtualHosts
[FAILED]
And there is no more output. Prior to this I was getting this error:
[root#localhost etc]# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server's fully
qualified domain name, using localhost.localdomain for ServerName [FAILED]
I googled and managed to get this error to disappear by editting /etc/httpd/conf/httpd.conf - I simply uncommented the ServerName variable and changed the value to localhost. So it looks like:
ServerName localhost - In my mind this makes sense since localhost is the external ip server anyways. [NOTE]: Even if I change it to ServerName <external-ip> I still get the same output when restarting httpd.
Can anyone help me get past this step? I've been googling for about 1-2 hours. Can't find anything. Anyone had the same error and mind pointing me in the right direction?
UPDATE 1:
cat /var/log/httpd/error_log
The output is basically normally operation until it reaches this point at which it just spams the rest of the log with it
(2)No such file or directory: httpd: could not open error log file
/srv/www/java-applet/logs/error.log - Unable to open logs
So I guess the error occurs since it cannot find the specified log file? Where can I change this? Why is this even occurring if I have a default installation of httpd?
Update 2:
Nevermind, I just solved it by making the directory that it said it was missing. Lame...

Apache Webserver stopped working on windows7

I have an Apache 2.4.9 web-server running php5 on windows7. It had been working fine, but now it isn't running. I see nothing about php in the application logs, nor in the Apache logs.
I do not know how to troubleshoot php5 in windows. I cannot run any phpinfo(); scripts, but I can see where php is started in the Apache logs
[Mon May 12 17:26:49.832429 2014] [mpm_winnt:notice] [pid 1388:tid 528] AH00455: Apache/2.4.9 (Win32) PHP/5.5.11 configured -- resuming normal operations
I see no notice of PHP crashing though.
The page is http://rm362c.umecheme.maine.edu/
and you can see the php in the source now.
what steps can I take to troubleshoot and debug php5 as it pertains to Apache2.4 and Windows7?
Add below lines to php.ini to view any php error in your browser --
error_reporting = -1
display_errors = On
Remove above lines in your production environment (Source: http://php.net/manual/en/errorfunc.configuration.php#89648 ).
For more check http://php.net/manual/en/errorfunc.configuration.php .
If port 80 is blocked by Skype or some other applications you will not be able to run apache since http requires port 80 and https require port 443 open. or if you have IIS web server running it also could be reason.
Please go through below article for further details if its xampp on windows7
http://www.nextofwindows.com/how-to-troubleshoot-xampp-apache-not-running-on-windows-7/

PHP $_ENV vs Linux environment (Ubuntu)

In my PHP code I am trying to access my linux system environment variable that I export in /etc/profile.
When I type "printenv" in my terminal, I can see that the variable (called MEMCACHED_1) set.
But my PHP doesn't see that variable, it's neither in $_ENV array nor can I access it with getenv function.
In my php.ini file I set variables_order = "EGPCS". That added a lot of variables to $_ENV, but MEMCACHED_1 is still not there.
What php.ini exactly do you mean? There are /etc/php5/apache2/php.ini (configuration used by scripts run by Apache module) and /etc/php5/cli/php.ini (configuration used by command line scripts). The latter has variables_order = "GPCS" as default setting on Ubuntu.
If you are running your code from command line, try changing /etc/php5/cli/php.ini and setting variables_order = "EGPCS".
If you are using Apache module, you may want to access needed variables as $_SERVER instead of $_ENV, or call getenv() before as comments here suggest: http://php.net/manual/en/reserved.variables.environment.php
UPDATE
Another thing to remember: PHP processes started by Apache module are run with user www-data (or whatever your User variable in Apache configuration says). So, you should also check printenv for correct user:
sudo su -l www-data -c 'printenv'
-- this should have line USER=www-data or something similar (not root, not your login). Make sure MEMCACHED_1 is set there.

Categories