Skip to content

Commit 6f5da7a

Browse files
authored
Nginx and Php-fpm with Laravel on Docker
How to run multiple services in one docker container. Running nginx and php-fpm processes in the same Debian container with Laravel (mysql, mariadb, sqlite).
1 parent b6aa96e commit 6f5da7a

25 files changed

+1271
-2
lines changed

Dockerfile

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# docker compose up --build -d
2+
# docker build -t demo/app:latest -f atomjot/app/Dockerfile demo/app
3+
# docker build --no-cache -t demo/app:0.1 .
4+
# docker run -p 8000:80 demo/app:0.1
5+
6+
FROM debian:bookworm AS php
7+
8+
LABEL maintainer="Atomjoy"
9+
10+
# Mysql init
11+
COPY ./mysql/init.sql /docker-entrypoint-initdb.d/init.sql
12+
13+
# Env php.ini
14+
ENV PHP_OPCACHE_ENABLE=1
15+
ENV PHP_OPCACHE_ENABLE_CLI=0
16+
ENV PHP_OPCACHE_VALIDATE_TIMESTAMP=1
17+
ENV PHP_OPCACHE_REVALIDATE_FREQ=1
18+
19+
# Install
20+
RUN apt-get update -y
21+
RUN apt-get install apt-transport-https -y
22+
23+
RUN apt-get install -y gnupg tzdata \
24+
&& echo "UTC" > /etc/timezone \
25+
&& dpkg-reconfigure -f noninteractive tzdata
26+
27+
RUN apt-get update \
28+
&& apt-get install -y curl zip unzip git supervisor sqlite3 openssl ssl-cert \
29+
nginx php8.2-fpm php8.2-cli php-opcache \
30+
php8.2-mysql php8.2-sqlite3 php8.2-pgsql \
31+
php8.2-curl php8.2-redis php8.2-memcached \
32+
php8.2-gd php8.2-imap php8.2-mbstring php8.2-tokenizer \
33+
php8.2-xml php8.2-zip php8.2-bcmath php8.2-soap \
34+
php8.2-intl php8.2-readline php8.2-xdebug \
35+
php-msgpack php-igbinary \
36+
&& apt-get -y autoremove \
37+
&& apt-get clean \
38+
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
39+
40+
WORKDIR /var/www/html
41+
42+
COPY --chown=www-data:www-data --chmod=2775 ./webapp /var/www/html
43+
44+
RUN rm -rf /etc/nginx/sites-enabled/default
45+
46+
RUN make-ssl-cert generate-default-snakeoil --force-overwrite
47+
48+
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
49+
50+
COPY ./php/php-fpm.conf /etc/php/8.2/fpm/php-fpm.conf
51+
52+
COPY ./php/www.conf /etc/php/8.2/fpm/pool.d/www.conf
53+
54+
COPY ./php/php.ini /etc/php/8.2/fpm/conf/99-docker-php.ini
55+
56+
RUN nginx -t
57+
58+
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
59+
60+
USER www-data
61+
62+
RUN composer update
63+
RUN composer dump-autoload -o
64+
65+
RUN php artisan cache:clear
66+
RUN php artisan config:clear
67+
RUN php artisan migrate
68+
RUN php artisan storage:link
69+
70+
USER root
71+
72+
# Allow services autostart
73+
# RUN echo "exit 0" > /usr/sbin/policy-rc.d
74+
75+
# Run php-fpm (required don't remove)
76+
CMD /etc/init.d/php8.2-fpm start && nginx -g "daemon off;"

README.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,47 @@
1-
# laravel-nginx-php-mysql-multiple
2-
How to run multiple services in one docker container. Running nginx and php-fpm processes in the same Debian container with Laravel (mysql, mariadb, sqlite).
1+
# Nginx and Php-fpm with Laravel on Docker
2+
3+
Running nginx and php-fpm processes in the same Debian container with Laravel (mysql, mariadb, sqlite). How to run multiple services in one docker container.
4+
5+
## Laravel project directory
6+
7+
```sh
8+
# Remove webapp dir and create new Laravel app
9+
composer create-project laravel/laravel webapp
10+
11+
# Or copy your Laravel project files to
12+
webapp
13+
```
14+
15+
## Config Mysql in files
16+
17+
```sh
18+
.env
19+
webapp/.env
20+
```
21+
22+
## Build
23+
24+
```sh
25+
# Build up
26+
docker compose up --build -d
27+
docker compose build --no-cache && docker compose up --force-recreate -d
28+
29+
# Show
30+
docker compose ps
31+
32+
# Interactive container terminal
33+
docker exec -it app_host bash
34+
docker exec -it mysql_host bash
35+
```
36+
37+
## Run Php-fpm and nginx in same docker container
38+
39+
How to run multiple services in one docker container.
40+
41+
```sh
42+
# Allow services autostart
43+
RUN echo "exit 0" > /usr/sbin/policy-rc.d
44+
45+
# Run php-fpm (required don't remove)
46+
CMD /etc/init.d/php8.2-fpm start && nginx -g "daemon off;"
47+
```

docker-compose.yml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
services:
2+
3+
# Php, nginx service
4+
app:
5+
tty: true
6+
container_name: app_host
7+
restart: unless-stopped
8+
build:
9+
context: .
10+
target: php
11+
dockerfile: ./Dockerfile
12+
working_dir: /var/www # bash root
13+
ports:
14+
- 8000:80
15+
networks:
16+
- internal
17+
depends_on:
18+
- mysql
19+
volumes:
20+
- ./logs/nginx:/var/log/nginx/
21+
- ./logs/php:/var/log/fpm-php.www.log
22+
# - ./logs/supervisor:/var/log/supervisor/supervisord.log
23+
# - myapp:/home/test/myapp
24+
25+
# Database service
26+
mysql:
27+
tty: true
28+
container_name: mysql_host
29+
image: mysql:8.0
30+
ports:
31+
- 7000:3306
32+
restart: unless-stopped
33+
environment:
34+
- MYSQL_ALLOW_EMPTY_PASSWORD=${DB_ALLOW_EMPTY_PASSWORD}
35+
- MYSQL_DATABASE=${DB_DATABASE}
36+
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
37+
networks:
38+
- internal
39+
# volumes:
40+
# - ./mysql:/docker-entrypoint-initdb.d/
41+
# - db-data:/var/lib/mysql
42+
43+
# Database service
44+
# mariadb:
45+
# tty: true
46+
# container_name: mysql_host
47+
# image: mariadb
48+
# ports:
49+
# - 7000:3306
50+
# restart: unless-stopped
51+
# environment:
52+
# - MARIADB_ALLOW_EMPTY_PASSWORD=${DB_ALLOW_EMPTY_PASSWORD}
53+
# - MARIADB_DATABASE=${DB_DATABASE}
54+
# - MARIADB_ROOT_PASSWORD=${DB_PASSWORD}
55+
# networks:
56+
# - internal
57+
# # volumes:
58+
# # - ./mysql:/docker-entrypoint-initdb.d/
59+
# # - db-data:/var/lib/mysql
60+
61+
# Docker volumes
62+
volumes:
63+
# Storage
64+
myapp:
65+
# Db data
66+
db-data:
67+
driver: local
68+
driver_opts:
69+
device: ./volume/mysql
70+
type: none
71+
o: bind
72+
# Or
73+
# db-data: ~
74+
75+
networks:
76+
internal:
77+
driver: bridge

docker/supervisord.conf

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Use in Dockerfile
2+
# COPY ./supervisord.conf /etc/supervisord.conf
3+
# CMD ["/usr/bin/supervisord", "-n"]
4+
# CMD ["supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf", "-n"]
5+
6+
[supervisord]
7+
user=www-data
8+
nodaemon=true
9+
logfile=/dev/null
10+
logfile_maxbytes=0
11+
12+
[unix_http_server]
13+
file=/tmp/supervisor.sock
14+
chmod=0755
15+
username=www-data
16+
password=
17+
18+
; inet (TCP) server disabled by default
19+
[inet_http_server]
20+
port=127.0.0.1:9001
21+
username=www-data
22+
password=
23+
24+
# [program:nginx]
25+
command=nginx
26+
autostart=true
27+
autorestart=true
28+
user=root
29+
priority=1
30+
numprocs=1
31+
stopasgroup=true
32+
killasgroup=true
33+
stdout_logfile=/dev/stdout
34+
stdout_logfile=/dev/fd/1
35+
stdout_logfile_maxbytes=0
36+
stderr_logfile=/dev/stderr
37+
stderr_logfile_maxbytes=0
38+
39+
[program:php-fpm]
40+
command=php-fpm8.2
41+
autostart=true
42+
autorestart=true
43+
user=root
44+
priority=2
45+
numprocs=1
46+
stopasgroup=true
47+
killasgroup=true
48+
stdout_logfile=/dev/stdout
49+
stdout_logfile=/dev/fd/1
50+
stdout_logfile_maxbytes=0
51+
stderr_logfile=/dev/stderr
52+
stderr_logfile_maxbytes=0

html/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Redirect to public directory.

html/public/favicon.ico

318 Bytes
Binary file not shown.

html/public/index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
phpinfo();

logs/nginx/access.log

Whitespace-only changes.

logs/nginx/app_access.log

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
172.19.0.1 - - [30/Sep/2024:14:44:07 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
2+
172.19.0.1 - - [30/Sep/2024:14:44:11 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
3+
172.19.0.1 - - [30/Sep/2024:14:45:08 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
4+
172.19.0.1 - - [30/Sep/2024:14:45:10 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
5+
172.19.0.1 - - [30/Sep/2024:14:45:11 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
6+
172.19.0.1 - - [30/Sep/2024:14:45:12 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
7+
172.19.0.1 - - [30/Sep/2024:14:45:12 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
8+
172.19.0.1 - - [30/Sep/2024:14:45:12 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
9+
172.19.0.1 - - [30/Sep/2024:14:45:12 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
10+
172.19.0.1 - - [30/Sep/2024:14:58:13 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
11+
172.19.0.1 - - [30/Sep/2024:14:58:15 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
12+
172.19.0.1 - - [30/Sep/2024:14:58:16 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
13+
172.19.0.1 - - [30/Sep/2024:14:58:16 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
14+
172.19.0.1 - - [30/Sep/2024:14:58:16 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
15+
172.19.0.1 - - [30/Sep/2024:14:58:17 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
16+
172.19.0.1 - - [30/Sep/2024:14:58:17 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
17+
172.19.0.1 - - [30/Sep/2024:15:16:13 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
18+
172.19.0.1 - - [30/Sep/2024:15:16:16 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
19+
172.19.0.1 - - [30/Sep/2024:15:16:17 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
20+
172.19.0.1 - - [30/Sep/2024:15:16:17 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
21+
172.19.0.1 - - [30/Sep/2024:15:16:18 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
22+
172.19.0.1 - - [30/Sep/2024:15:16:18 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
23+
172.19.0.1 - - [30/Sep/2024:15:16:18 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
24+
172.19.0.1 - - [30/Sep/2024:15:29:54 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
25+
172.19.0.1 - - [30/Sep/2024:15:29:56 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
26+
172.19.0.1 - - [30/Sep/2024:15:29:56 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
27+
172.19.0.1 - - [30/Sep/2024:15:29:56 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
28+
172.19.0.1 - - [30/Sep/2024:15:33:52 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
29+
172.19.0.1 - - [30/Sep/2024:15:33:54 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
30+
172.19.0.1 - - [30/Sep/2024:15:33:54 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
31+
172.19.0.1 - - [30/Sep/2024:15:33:54 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
32+
172.19.0.1 - - [30/Sep/2024:15:33:55 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
33+
172.19.0.1 - - [30/Sep/2024:15:33:55 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
34+
172.19.0.1 - - [30/Sep/2024:15:33:55 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
35+
172.19.0.1 - - [30/Sep/2024:15:33:55 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
36+
172.19.0.1 - - [30/Sep/2024:15:33:55 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
37+
172.19.0.1 - - [30/Sep/2024:15:33:56 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"
38+
172.19.0.1 - - [30/Sep/2024:15:33:56 +0000] "GET / HTTP/1.1" 200 10294 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"

logs/nginx/app_error.log

Whitespace-only changes.

logs/nginx/error.log

Whitespace-only changes.

logs/nginx/host.access.log

Whitespace-only changes.

logs/nginx/host.error.log

Whitespace-only changes.

mysql/init.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CREATE DATABASE IF NOT EXISTS laravel CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
2+
CREATE DATABASE IF NOT EXISTS laravel_testing CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
3+
4+
GRANT ALL PRIVILEGES ON DATABASE laravel TO root;
5+
GRANT ALL PRIVILEGES ON DATABASE laravel_testing TO root;
6+
7+
-- GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY 'toor' WITH GRANT OPTION;
8+
-- GRANT ALL PRIVILEGES ON *.* TO root@127.0.0.1 IDENTIFIED BY 'toor' WITH GRANT OPTION;
9+
10+
-- CREATE USER docker;
11+
-- CREATE DATABASE docker;
12+
-- GRANT ALL PRIVILEGES ON DATABASE docker TO docker;

0 commit comments

Comments
 (0)