How to route my php server through nginx using docker-compose?

550

This is my first time developing a docker application form scratch. I want to have all my services running from container while I edit the code from my local machine.

So I have a root folder just calledtest/ and i want to follow this tutorial https://auth0.com/blog/developing-restful-apis-with-lumen/. So i need composer, a web sever (nginx), and a database (mysql).

I have inside the root folder a docker-compose file as follows:

version: '2'

services:

  composer:
    image: composer
    container_name: composer
    volumes:
      - ./authors:/app
    restart: always
    tty: true
    command: bash

  php:
    image: php:fpm
    container_name: php
    restart: always
    tty: true
    working_dir: /var/www
    volumes:
      - ./authors:/var/www
    environment:
      - "DB_PORT=3306"
      - "DB_HOST=mysql"

  nginx:
    image: nginx
    container_name: nginx
    restart: always
    tty: true
    working_dir: /var/www
    volumes_from:
      - php
    volumes:
      - ./vhost.conf:/etc/nginx/conf.d/default.conf
    ports:
      - 8080:80

  mysql:
    image: mysql:5.7
    container_name: mysql
    restart: always
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: hmh
      MYSQL_USER: root
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - dbdata:/var/lib/mysql

#Volumes
volumes:
  dbdata:

And here is the configuration file used for nginx:

server {
    listen 80;
    index index.php index.html;
    root /var/www/public;

    location / {
        try_files $uri /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:8000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

The composer container is there so that I can jump into anytime I need and run some commands, while also being able to lift the project from it by runningphp -S localhost:8000 -t public this actually logs into the console that the server is running BUT when I go into localhost:8080 it is only showing the nginx welcome screen.

So I know nginx is working, composer lets me do all the work I need, but how do I route the php server through nginx? I can go inside the container of php, and start the server but when go to the URL it does not work. If it is not much to ask, I would like some explanation and not just the solution. Thanks.

890

Answer

Solution:

Since I couldn't configure nginx the way I want it, I use instead Apache2 server. Here is my compose file.

version: '3'
services:
  composer:
    build:
      context: .
      dockerfile: .docker/composer.dockerfile
    container_name: composer-php
    user: "${UID}:${GID}"
    volumes:
      - .:/app
    restart: always
    tty: true
    working_dir: /app
    command: bash
    environment:
      DB_CONNECTION: mysql
      DB_HOST: db
      DB_DATABASE: lumen
      DB_USERNAME: lumen
      DB_PASSWORD: lumen

  php:
    build:
      context: .
      dockerfile: .docker/php.dockerfile
    container_name: apache-php
    user: "${UID}:${GID}"
    depends_on:
      - db
    ports:
      - 8080:8080
    volumes:
      - .:/app
    restart: always
    tty: true
    working_dir: /app
    command: bash
    environment:
      DB_CONNECTION: mysql
      DB_HOST: db
      DB_DATABASE: lumen
      DB_USERNAME: lumen
      DB_PASSWORD: lumen

  db:
    image: mysql:5
    container_name: mysql-php
    restart: always
    tty: true
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: lumen
      MYSQL_USER: lumen
      MYSQL_PASSWORD: lumen
    ports:
      - "3306:3306"
    volumes:
      - dbdata:/var/lib/mysql

volumes:
  dbdata:

With this setup, I was able to follow this tutorial https://auth0.com/blog/developing-restful-apis-with-lumen/ with no problems to test my setup. I modify the official images just to instal pdo pdo_mysql, that is all that goes inside my dockerfiles for this setup.

With that in place, I can go inside the composer container, create the project, then get out of that container and move into the php container, cd into the project, then serve the porject.

22

Answer

Solution:

The problem here is that your server runs on port8000 but your docker only supports port80 via ports: - 8080:80

so does nginx: listen 80;

People are also looking for solutions to the problem: php - Why did not delete table records in Laravel 5?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.