This commit is contained in:
Joseph Barreca 2023-01-10 11:26:26 -08:00 committed by GitHub
commit 1a31b4d5d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 138 additions and 0 deletions

66
nginx-php-mysql/README.md Normal file
View File

@ -0,0 +1,66 @@
## Compose sample application
### PHP server with an Nginx proxy and a MySQL database
Project structure:
```
.
├── backend
│   ├── index.php
├── db
│   └── password.txt
├── docker-compose.yaml
├── proxy
│   ├── conf
│   └── Dockerfile
└── README.md
```
[_docker-compose.yaml_](docker-compose.yaml)
```
services:
backend:
image: php:8.0.3-fpm-buster
...
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
<h1>hello world in php!</h1>
```
Stop and remove the containers
```
$ docker-compose down
```

View File

@ -0,0 +1,3 @@
<?php
echo '<h1>hello world in php!</h1>';
?>

View File

@ -0,0 +1 @@
db-q5n2g

View File

@ -0,0 +1,34 @@
version: "3.8"
services:
backend:
image: php:8.0.3-fpm-buster
secrets:
- db-password
depends_on:
- db
volumes:
- ./backend:/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:
- ./backend:/var/www/html # this part seems repetitive (jb)
volumes:
db-data:
secrets:
db-password:
file: db/password.txt

View File

@ -0,0 +1,2 @@
FROM nginx:1.19.8-alpine
COPY conf /etc/nginx/conf.d/default.conf

32
nginx-php-mysql/proxy/conf Executable file
View File

@ -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;
}
}