From 98b90b1d4917591ca8337cb477657a1c21958145 Mon Sep 17 00:00:00 2001 From: Joseph Barreca Date: Fri, 26 Mar 2021 16:28:59 -0400 Subject: [PATCH 1/4] Sample for nginx-php-mysql Signed-off-by: Joseph Barreca --- nginx-php-mysql/README.md | 68 +++++++++++++++++++++++++++++ nginx-php-mysql/backend/Dockerfile | 1 + nginx-php-mysql/db/password.txt | 1 + nginx-php-mysql/docker-compose.yaml | 34 +++++++++++++++ nginx-php-mysql/proxy/Dockerfile | 2 + nginx-php-mysql/proxy/conf | 32 ++++++++++++++ nginx-php-mysql/src/index.php | 3 ++ 7 files changed, 141 insertions(+) create mode 100644 nginx-php-mysql/README.md create mode 100755 nginx-php-mysql/backend/Dockerfile create mode 100644 nginx-php-mysql/db/password.txt create mode 100644 nginx-php-mysql/docker-compose.yaml create mode 100755 nginx-php-mysql/proxy/Dockerfile create mode 100755 nginx-php-mysql/proxy/conf create mode 100644 nginx-php-mysql/src/index.php diff --git a/nginx-php-mysql/README.md b/nginx-php-mysql/README.md new file mode 100644 index 0000000..5c849ad --- /dev/null +++ b/nginx-php-mysql/README.md @@ -0,0 +1,68 @@ +## Compose sample application +### PHP server with an Nginx proxy and a MySQL database + +Project structure: +``` +. +├── backend +│   ├── Dockerfile +├── db +│   └── password.txt +├── docker-compose.yaml +├── proxy +│   ├── conf +│   └── Dockerfile +├── src +│   ├── index.php +└── README.md +``` + +[_docker-compose.yaml_](docker-compose.yaml) +``` +services: + backend: + build: backend + ... + db: + image: mysql:8.0.19 + ... + proxy: + build: proxy + ports: + - 80:80 + ... +``` +The compose file defines an application with three services `proxy`, `backend` and `db`. +When deploying the application, docker-compose maps port 80 of the proxy service container to port 80 of the host as specified in the file. +Make sure port 80 on the host is not already in use. + +## Deploy with docker-compose + +``` +$ docker-compose up -d +Creating nginx-php-mysql_db_1 ... done +Creating nginx-php-mysql_backend_1 ... done +Creating nginx-php-mysql_proxy_1 ... done +``` + +## Expected result + +Listing containers must show three containers running and the port mapping as below: +``` +$ docker ps +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +2244050972fc nginx-php-mysql_proxy "/docker-entrypoint.…" 51 seconds ago Up 49 seconds 0.0.0.0:80->80/tcp nginx-php-mysql_proxy_1 +75353040cb38 nginx-php-mysql_backend "docker-php-entrypoi…" 51 seconds ago Up 50 seconds 9000/tcp nginx-php-mysql_backend_1 +e54bd7e0c790 mysql:8.0.19 "docker-entrypoint.s…" 52 seconds ago Up 50 seconds 3306/tcp, 33060/tcp nginx-php-mysql_db_1 +``` + +After the application starts, navigate to `http://localhost:80` in your web browser or run: +``` +$ curl localhost:80 +

hello world in php!

+``` + +Stop and remove the containers +``` +$ docker-compose down +``` diff --git a/nginx-php-mysql/backend/Dockerfile b/nginx-php-mysql/backend/Dockerfile new file mode 100755 index 0000000..b8b661e --- /dev/null +++ b/nginx-php-mysql/backend/Dockerfile @@ -0,0 +1 @@ +FROM php:8.0.3-fpm-buster \ No newline at end of file diff --git a/nginx-php-mysql/db/password.txt b/nginx-php-mysql/db/password.txt new file mode 100644 index 0000000..cea6d05 --- /dev/null +++ b/nginx-php-mysql/db/password.txt @@ -0,0 +1 @@ +db-q5n2g \ No newline at end of file diff --git a/nginx-php-mysql/docker-compose.yaml b/nginx-php-mysql/docker-compose.yaml new file mode 100644 index 0000000..d94581e --- /dev/null +++ b/nginx-php-mysql/docker-compose.yaml @@ -0,0 +1,34 @@ +version: "3.8" +services: + backend: + build: backend + secrets: + - db-password + depends_on: + - db + volumes: + - ./src:/var/www/html # this part seems repetitive (jb) + db: + image: mysql:8.0.19 + command: '--default-authentication-plugin=mysql_native_password' + restart: always + secrets: + - db-password + volumes: + - db-data:/var/lib/mysql + environment: + - MYSQL_DATABASE=example + - MYSQL_ROOT_PASSWORD_FILE=/run/secrets/db-password + proxy: + build: proxy + ports: + - 80:80 + depends_on: + - backend + volumes: + - ./src:/var/www/html # this part seems repetitive (jb) +volumes: + db-data: +secrets: + db-password: + file: db/password.txt diff --git a/nginx-php-mysql/proxy/Dockerfile b/nginx-php-mysql/proxy/Dockerfile new file mode 100755 index 0000000..058d4d8 --- /dev/null +++ b/nginx-php-mysql/proxy/Dockerfile @@ -0,0 +1,2 @@ +FROM nginx:1.19.8-alpine +COPY conf /etc/nginx/conf.d/default.conf \ No newline at end of file diff --git a/nginx-php-mysql/proxy/conf b/nginx-php-mysql/proxy/conf new file mode 100755 index 0000000..e0f5acf --- /dev/null +++ b/nginx-php-mysql/proxy/conf @@ -0,0 +1,32 @@ +server { + location ~ /.well-known { + allow all; + } + + location ~ /\.ht { + deny all; + } + + root /var/www/html; + + index index.php index.html; + + location / { + try_files $uri $uri/ /index.php?$args; + } + + location ~ [^/]\.php(/|$) { + fastcgi_split_path_info ^(.+?\.php)(/.*)$; + if (!-f $document_root$fastcgi_script_name) { + return 404; + } + + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; + + fastcgi_pass backend:9000; + fastcgi_index index.php; + } +} \ No newline at end of file diff --git a/nginx-php-mysql/src/index.php b/nginx-php-mysql/src/index.php new file mode 100644 index 0000000..cc450d5 --- /dev/null +++ b/nginx-php-mysql/src/index.php @@ -0,0 +1,3 @@ +hello world in php!'; +?> \ No newline at end of file From 05008c0b54e679ddbb26eebd64c50efd11bb57c6 Mon Sep 17 00:00:00 2001 From: Joseph Barreca Date: Tue, 6 Apr 2021 13:30:44 -0400 Subject: [PATCH 2/4] Refactor directory and image location - Removed Dockerfile for php image + Added php image into docker-compose file -> Moved source for nginx/php to backend directory Signed-off-by: Joseph Barreca --- nginx-php-mysql/backend/Dockerfile | 1 - nginx-php-mysql/{src => backend}/index.php | 0 nginx-php-mysql/docker-compose.yaml | 6 +++--- 3 files changed, 3 insertions(+), 4 deletions(-) delete mode 100755 nginx-php-mysql/backend/Dockerfile rename nginx-php-mysql/{src => backend}/index.php (100%) diff --git a/nginx-php-mysql/backend/Dockerfile b/nginx-php-mysql/backend/Dockerfile deleted file mode 100755 index b8b661e..0000000 --- a/nginx-php-mysql/backend/Dockerfile +++ /dev/null @@ -1 +0,0 @@ -FROM php:8.0.3-fpm-buster \ No newline at end of file diff --git a/nginx-php-mysql/src/index.php b/nginx-php-mysql/backend/index.php similarity index 100% rename from nginx-php-mysql/src/index.php rename to nginx-php-mysql/backend/index.php diff --git a/nginx-php-mysql/docker-compose.yaml b/nginx-php-mysql/docker-compose.yaml index d94581e..a87b41b 100644 --- a/nginx-php-mysql/docker-compose.yaml +++ b/nginx-php-mysql/docker-compose.yaml @@ -1,13 +1,13 @@ version: "3.8" services: backend: - build: backend + image: php:8.0.3-fpm-buster secrets: - db-password depends_on: - db volumes: - - ./src:/var/www/html # this part seems repetitive (jb) + - ./backend:/var/www/html # this part seems repetitive (jb) db: image: mysql:8.0.19 command: '--default-authentication-plugin=mysql_native_password' @@ -26,7 +26,7 @@ services: depends_on: - backend volumes: - - ./src:/var/www/html # this part seems repetitive (jb) + - ./backend:/var/www/html # this part seems repetitive (jb) volumes: db-data: secrets: From 763efc67f0979e27bda324a8b73fa3df6d75c800 Mon Sep 17 00:00:00 2001 From: Joseph Barreca Date: Tue, 6 Apr 2021 13:49:31 -0400 Subject: [PATCH 3/4] Update README.md provides an accurate representation of the recent file structure updates Signed-off-by: Joseph Barreca --- nginx-php-mysql/README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/nginx-php-mysql/README.md b/nginx-php-mysql/README.md index 5c849ad..03f9dab 100644 --- a/nginx-php-mysql/README.md +++ b/nginx-php-mysql/README.md @@ -5,15 +5,13 @@ Project structure: ``` . ├── backend -│   ├── Dockerfile +│   ├── index.php ├── db │   └── password.txt ├── docker-compose.yaml ├── proxy │   ├── conf │   └── Dockerfile -├── src -│   ├── index.php └── README.md ``` @@ -21,7 +19,7 @@ Project structure: ``` services: backend: - build: backend + image: php:8.0.3-fpm-buster ... db: image: mysql:8.0.19 From 4c0bdea11accfbe8118412d46ce66ab3c479d7cb Mon Sep 17 00:00:00 2001 From: Joseph Barreca Date: Wed, 7 Apr 2021 19:43:10 -0400 Subject: [PATCH 4/4] nit: Missing empty line in the EOF Signed-off-by: Joseph Barreca --- nginx-php-mysql/backend/index.php | 2 +- nginx-php-mysql/db/password.txt | 2 +- nginx-php-mysql/proxy/Dockerfile | 2 +- nginx-php-mysql/proxy/conf | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/nginx-php-mysql/backend/index.php b/nginx-php-mysql/backend/index.php index cc450d5..6b20e8f 100644 --- a/nginx-php-mysql/backend/index.php +++ b/nginx-php-mysql/backend/index.php @@ -1,3 +1,3 @@ hello world in php!'; -?> \ No newline at end of file +?> diff --git a/nginx-php-mysql/db/password.txt b/nginx-php-mysql/db/password.txt index cea6d05..c7960ee 100644 --- a/nginx-php-mysql/db/password.txt +++ b/nginx-php-mysql/db/password.txt @@ -1 +1 @@ -db-q5n2g \ No newline at end of file +db-q5n2g diff --git a/nginx-php-mysql/proxy/Dockerfile b/nginx-php-mysql/proxy/Dockerfile index 058d4d8..5d3383a 100755 --- a/nginx-php-mysql/proxy/Dockerfile +++ b/nginx-php-mysql/proxy/Dockerfile @@ -1,2 +1,2 @@ FROM nginx:1.19.8-alpine -COPY conf /etc/nginx/conf.d/default.conf \ No newline at end of file +COPY conf /etc/nginx/conf.d/default.conf diff --git a/nginx-php-mysql/proxy/conf b/nginx-php-mysql/proxy/conf index e0f5acf..56c1ffd 100755 --- a/nginx-php-mysql/proxy/conf +++ b/nginx-php-mysql/proxy/conf @@ -29,4 +29,4 @@ server { fastcgi_pass backend:9000; fastcgi_index index.php; } -} \ No newline at end of file +}