2

I'm trying to run a Laravel app in my local environment via Docker. I want to setup separate containers for each service i.e. Apache, PHP, MySQL. I also want to keep Composer, Artisan and PHPUnit in separate containers as well. This is more for neatness than anything else.

All the containers spin up with no issues and I can access each one no problems via the 'docker-compose exec [container name] /bin/sh' command.

The problem I'm having is that the index.php in the public folder is not being executed correctly. Apache is just serving up the file contents.

I can't figure out what I'm doing wrong. I've tried using an Nginx container instead of Apache but I get the same issue. I'm guessing my Apache container does not recognize my PHP container.

Is there anything I'm doing wrong below?

My docker-compose.yml file is as follows:

version: '3.8' networks: cpw: name: cpw_network services: apache: build: context: . dockerfile: apache.dockerfile container_name: cpw_apache depends_on: - php - mysql ports: - 8080:80 - 8443:443 volumes: - ./:/var/www/html networks: - cpw php: build: context: . dockerfile: php.dockerfile container_name: cpw_php volumes: - ./:/var/www/html networks: - cpw mysql: image: mysql:5.7.32 container_name: cpw_mysql environment: MYSQL_DATABASE: cpw MYSQL_USER: laravel MYSQL_PASSWORD: secret MYSQL_ROOT_PASSWORD: secret networks: - cpw composer: image: composer:latest container_name: cpw_composer volumes: - ./:/var/www/html working_dir: /var/www/html networks: - cpw artisan: build: context: . dockerfile: php.dockerfile container_name: cpw_artisan volumes: - ./:/var/www/html working_dir: /var/www/html entrypoint: [ "php", "artisan" ] networks: - cpw phpunit: build: context: . dockerfile: php.dockerfile container_name: cpw_phpunit volumes: - ./:/var/www/html working_dir: /var/www/html entrypoint: [ "/var/www/html/vendor/bin/phpunit" ] networks: - cpw 

My apache.dockerfile is as follows:

FROM httpd:alpine ADD ./apache/httpd-vhosts.conf /usr/local/apache2/conf/extra/httpd-vhosts.conf RUN sed -i 's,#Include conf/extra/httpd-vhosts.conf,Include conf/extra/httpd-vhosts.conf,g' /usr/local/apache2/conf/httpd.conf RUN mkdir -p /var/www/html 

My php.dockerfile is as follows:

FROM php:7.4.12-fpm-alpine RUN mkdir -p /var/www/html RUN apk --no-cache add shadow && usermod -u 1000 www-data RUN docker-php-ext-install pdo pdo_mysql 

My httpd-vhosts.conf is as follows:

<VirtualHost *:80> ServerAdmin [email protected] DocumentRoot /var/www/html/public ServerName localhost ServerAlias localhost ErrorLog logs/localhost-error_log CustomLog logs/localhost-access_log common <Directory /var/www/html/public> AllowOverride All DirectoryIndex index.php index.php Options -Indexes Require all granted </Directory> </VirtualHost> 

Any help appreciated.

Regards, Stephen

6
  • What have you tried to debug the problem? I don't see any configuration that tells Apache to use the PHP container for anything Commented Feb 22, 2021 at 16:55
  • @NicoHaase, I've tried to use the Nginx container instead of Apache. Commented Feb 22, 2021 at 16:56
  • 1
    And where's the configuration for Apache that instructs the server to use PHP? Commented Feb 22, 2021 at 16:58
  • 1
    Does stackoverflow.com/questions/59862387/… help? Commented Feb 22, 2021 at 17:00
  • 2
    Just out of curiosity, what is the exact difference between this question and the one you posted earlier that got closed and you decided to delete ? If you decide to deliberately not follow the rules, you could at the very least rework your question to show us you have made some progress. Commented Feb 22, 2021 at 17:36

2 Answers 2

2

Thanks for @NicoHaase for pointing me in the right direction.

This is the piece I was missing:

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

More details here:

How to deploy php-fpm on docker container and apache/nginx on localhost (Ubuntu)

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

5 Comments

Thank you so much!! I've been trying to resolve the same issue for 3 days in a row, my mind almost melted!
And where does the name php-fpm-container come from?
where should I add these text?
@Simon php-fpm-container is the name of the php docker container, in this case it should be cpw_php.
@Mr.Kenneth The text should be added in httpd-vhosts.conf, under VirtualHost
0

This might also fix:

<FilesMatch \.(?i:php)$> SetHandler application/x-httpd-php </FilesMatch> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.