kostik kostik
kostik kostik

Reputation: 1

PHP-FPM Container doesn't interpreter PHP code from HTTPD Container

I typed a compose file which run a httpd like a web-server, nginx like a proxy to httpd, and php-fpm like a *.php files interpreter.

when I open localhost on browser I can look files on /var/www/html, but when I try to open any php file, I see only php code.

php-fpm started on 9000, and I check that by run

nmap -p 9000 php

from another httpd container, checking port return the positive message.

My HTTPD conf file contains the follow lines:

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

My custom httpd conf file:

<VirtualHost *:8080> DocumentRoot /var/www/html ServerName page.com

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

<Directory "/var/www/html">
    AllowOverride All
    Order allow,deny
    Allow from all
    Require all granted
</Directory>

Upvotes: 0

Views: 91

Answers (2)

kostik kostik
kostik kostik

Reputation: 1

compose:

version: '3.8'
services:
  php:
    image: costa/php:1
    volumes:
      - ./test.php:/var/www/html/test.php

  apache:
    image: costa/apache:1
    depends_on:
      - php
    volumes:
      - ./test.php:/var/www/html/test.php
      - ./wp_apache.php:/etc/httpd/conf.d/wp_apache.php
      - ./test.html:/var/www/html/test.html

  nginx:
    image: costa/nginx:1
    ports:
      - "80:80"
    depends_on:
      - apache
    volumes:
      - ./wp_nginx.conf:/etc/nginx/conf.d/wp_nginx.conf

#volumes:

nginx conf:

server {
    listen 80;
    server_name test.com;

    location / {
        proxy_pass http://apache:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

apache:

<VirtualHost *:8080>
    ServerAdmin [email protected]
    DocumentRoot /var/www/html
    ServerName test.com
    
    <FilesMatch \.php$>
        SetHandler "proxy:fcgi://php:9000"
    </FilesMatch>

    <Directory "/var/www/html">
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>

Upvotes: 0

datawookie
datawookie

Reputation: 6534

It would be useful to see your docker-compose.yml. However, here is a working setup that you can use to get started. I use this stack with only two services: NGINX and PHP/FPM.

🗎 docker-compose.yml

services:
  nginx:
    image: nginx:1.25
    container_name: nginx
    ports:
      - "80:80"
    volumes:
      - ./default.conf:/etc/nginx/conf.d/default.conf
      - ./app:/var/www/html
    depends_on:
      - php

  php:
    image: php:8.3-fpm
    container_name: php
    volumes:
      - ./app:/var/www/html

That's using the following volume mounts:

  • ./app:/var/www/html on the php service so that PHP/FPM can run over the PHP files;
  • ./app:/var/www/html on the nginx service for serving static files; and
  • ./default.conf:/etc/nginx/conf.d/default.conf on the nginx service for the NGINX configuration.

🗎 default.conf

server {
    listen 80;
    server_name localhost 127.0.0.1;

    root /var/www/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

That establishes a reverse proxy from port 80 to the PHP service running on port 9000.

├── app
│   ├── index.php
│   └── static.html
├── default.conf
└── docker-compose.yml

🗎 index.php

<?php
echo "Hello from PHP!";

🗎 static.html

<!DOCTYPE html>
<html>
<head>
    <title>Static File</title>
</head>
<body>
    <h1>Hello from a Static File!</h1>
</body>
</html>

enter image description here

Upvotes: 1

Related Questions