TechFanDan
TechFanDan

Reputation: 3478

PHP Docker container not processing files and serving the source instead

My PHP Docker container is not processing files and the source is being returned instead.

The following content is being returned instead of being executed:

<?php 
phpinfo();
?>

Output of docker ps, to show that both my containers are running and listening on their respective ports:

dan@server:~$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
e5e235112a35        php:7.2-fpm         "docker-php-entrypoi…"   23 minutes ago      Up 23 minutes       0.0.0.0:9000->9000/tcp   php
2196a8f251d3        httpd               "httpd-foreground"       5 days ago          Up 33 seconds       0.0.0.0:80->80/tcp       apache

Virtualhost configuration, notice the FilesMatch directive passing the PHP to the container.

<Directory /usr/local/apache2/htdocs/default>
    Options -Indexes
    AllowOverride All
    Require all granted
</Directory>

<VirtualHost 192.168.2.35:80>

    ServerName localhost

    ServerAdmin webmaster@localhost
    DocumentRoot /usr/local/apache2/htdocs/default

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

    LogLevel trace1

    ErrorLog logs/error.default.ca.log
    CustomLog logs/access.default.ca.log combined

</VirtualHost>

Apache log, showing that the file is being served by Apache.

192.168.2.30 - - [22/Dec/2018:02:40:49 +0000] "GET /default/index.php HTTP/1.1" 200 24

Edit

I'm attempting to run Apache, PHP and MariaDB in separate containers. I had Apache running first, and now, I'm attempting to attach PHP.

Eventually, I wanted to add Nextcloud, which IIRC, has a container without Apache where I could reuse my existing container.

My PHP Dockerfile

FROM php:7.2-fpm
RUN buildDeps=" \
        libmcrypt-dev \
        default-libmysqlclient-dev \
        libjpeg-dev \
        libldap2-dev \
        libmemcachedutil2 \
        libpng-dev \
        libpq-dev \
        libxml2-dev \
    " \
    && apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y $buildDeps $runtimeDeps \
    && pecl install mcrypt-1.0.1 && docker-php-ext-enable mcrypt.so \
    && docker-php-ext-install bcmath bz2 calendar iconv json intl mbstring mysqli opcache pdo_mysql soap zip \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install gd \
    && apt-get purge -y --auto-remove $buildDeps \
    && rm -r /var/lib/apt/lists/*

Upvotes: 3

Views: 12619

Answers (1)

thaJeztah
thaJeztah

Reputation: 29037

First, note that the official PHP image with Apache uses /var/www/html as its default webroot (see the documentation for the image)

The problem in your case likely is because you defined a virtualhost based on an IP-address;

<VirtualHost 192.168.2.35:80>

When docker starts a container, the container is assigned a random IP address on the internal (container-container) network. This IP-address won't match the IP-address of your host, and your container configuration should not rely on the IP-address (as it will change any time the container is started, and if you run multiple instances of your container, each will get its own IP-address).

Containers are not virtual machines, and in general will be handling a single service / website. Assuming your container will be running a single website / virtualhost, in which case you can use the same as the default configuration:

<VirtualHost *:80>

If you don't require special configuration, you could use the default configuration of the image, in which case this would work:

Create your index.php

cat > ./index.php -<<'EOF'
<?php 
phpinfo();
?>
EOF

Create your Dockerfile;

cat > ./Dockerfile -<<'EOF'
FROM php:7.3-apache
COPY index.php /var/www/html/
EOF

Build your image:

 docker build -t mywebsite .

Start your container; mapping port 80 of the container to port 4000 on the host;

docker run -d -p 4000:80 --name mywebsite-container mywebsite

Check the output in your browser, or using curl:

curl http://localhost:4000

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<style type="text/css">
body {background-color: #fff; color: #222; font-family: sans-serif;}
.....

Upvotes: 4

Related Questions