Run PHP application in Apache server using PHP-FPM

拜拜、爱过 提交于 2019-12-11 07:33:35

问题


We have a php application which was running fine using Docker Image :

php:7.2-apache-stretch

We now have to make use of the below image for better performance and make the application work as it was before .

php:7.2.17-fpm-stretch

As this image does not have apache in it . I updated the Dockerfile starting from the installation of apache2 and related packages based on various forums .

There is many other steps . I have just added the instructions which I have updated in the Dockerfile.

 FROM php:7.2.17-fpm-stretch

RUN apt-get update && apt-get install -y apache2 wget

RUN cd /tmp && wget http://mirrors.kernel.org/ubuntu/pool/multiverse/liba/libapache-mod-fastcgi/libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb && dpkg -i libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb;apt-get install -f

RUN a2enmod actions proxy_fcgi fastcgi
COPY 000-default.conf /etc/apache2/sites-available/000-default.conf
COPY info.php /var/www/html/info.php
COPY run.sh /app/run.sh
# EXPOSE 9000
RUN chmod 755 /app/run.sh
CMD "/app/run.sh"

The info.php contains <?php phpinfo( ); ?> . In the run.sh script , we start the php-fpm service and apache2 as below

php-fpm -D

/usr/sbin/apachectl -D FOREGROUND

previously I was trying to access the app from the port which was mapped to 9000 ( fpm ) . When I accessed the correct port where apache was running , I was able to view info.php .

The Content in the vhost.conf file.

<FilesMatch \.php$>
  SetHandler "proxy:fcgi://localhost:9000"
</FilesMatch>


回答1:


I got it working making a few modifications.

I used this slightly modified dockerfile:

FROM php:7.2.17-fpm-stretch

RUN apt-get update; apt-get install -y apache2 wget

RUN cd /tmp && wget http://mirrors.kernel.org/ubuntu/pool/multiverse/liba/libapache-mod-fastcgi/libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb && dpkg -i libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb;apt-get install -f


RUN a2enmod actions proxy_fcgi fastcgi

COPY run.sh /app/run.sh
RUN chmod 755 /app/run.sh
CMD "/app/run.sh"

I also added the following snippet (the same modification you did) to /etc/apache2/sites-available/000-default.conf:

<FilesMatch \.php$>
  SetHandler "proxy:fcgi://localhost:9000"
</FilesMatch>

Here is the output I get:

[09-Apr-2019 21:23:06] NOTICE: fpm is running, pid 9
[09-Apr-2019 21:23:06] NOTICE: ready to handle connections
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

Also, did you try building the dockerfile without the cache? Sometimes, caching can cause issues with package installation (see here for more details). To do a clean build of an image, just use:

docker build --no-cache 



回答2:


I got your new configuration working using this dockerfile:

FROM php:7.2.17-fpm-stretch

RUN apt-get update && apt-get install -y apache2 wget

RUN cd /tmp && wget http://mirrors.kernel.org/ubuntu/pool/multiverse/liba/libapache-mod-fastcgi/libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb && dpkg -i libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb;apt-get install -f

RUN a2enmod actions proxy_fcgi fastcgi
COPY 000-default.conf /etc/apache2/sites-available/000-default.conf
COPY info.php /var/www/html/info.php
COPY run.sh /app/run.sh
RUN chmod 755 /app/run.sh
EXPOSE 80
CMD "/app/run.sh"

The command I ran was:

docker run -P -d --rm <php-image>

The exposed ports are:

0.0.0.0:32773->80/tcp, 0.0.0.0:32772->9000/tcp

I was able to access info.php using http://localhost:32773/info.php




回答3:


I was able to access the php page . Everything was working fine . But I was looking in the wrong direction . When I run the container .

docker run -P -d --rm php:test-fpm

The output was

82071c9ff023        php:test-fpm                    "docker-php-entrypoi…"   2 seconds ago       Up 1 second         0.0.0.0:32778->80/tcp, 0.0.0.0:32777->9000/tcp   practical_mclean

I was accessing localhost:32777/info.php . But I should I accessed the 32778 where apache is exposed and localhost: 32778/info.php worked !!! .

Is there a way to avoid the port mapping of 9000. ???



来源:https://stackoverflow.com/questions/55598226/run-php-application-in-apache-server-using-php-fpm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!