How to route my php server through nginx using docker-compose?
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.
Answer
Solution:
Since I couldn't configure nginx the way I want it, I use instead Apache2 server. Here is my compose file.
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.
Answer
Solution:
The problem here is that your server runs on port
8000
but your docker only supports port80
viaports: - 8080:80
so does nginx:
listen 80;