Skip to content

Commit 7be7690

Browse files
authored
Nginx and Php-fpm with Laravel on Docker
Running php-fpm and nginx processes in the same container with Laravel.
1 parent 2638f92 commit 7be7690

25 files changed

+1041
-2
lines changed

Dockerfile

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Webserver
2+
FROM php:8.2-fpm-bookworm AS php
3+
4+
# Mysql init
5+
COPY ./mysql/init.sql /docker-entrypoint-initdb.d/init.sql
6+
7+
# Env
8+
ENV PHP_OPCACHE_ENABLE=1
9+
ENV PHP_OPCACHE_ENABLE_CLI=0
10+
ENV PHP_OPCACHE_VALIDATE_TIMESTAMP=1
11+
ENV PHP_OPCACHE_REVALIDATE_FREQ=1
12+
13+
# RUN usermod -u 1000 www-data
14+
15+
# Apt https
16+
RUN apt-get update -y
17+
RUN apt-get install apt-transport-https -y
18+
19+
# Libs
20+
RUN apt-get install -y unzip zip nano libpq-dev libcurl4-gnutls-dev nginx
21+
RUN apt-get install -y libicu-dev libmariadb-dev zlib1g-dev libwebp-dev libxpm-dev libjpeg-dev libpng-dev libjpeg62-turbo-dev libfreetype6-dev
22+
23+
# Extensions php
24+
RUN docker-php-ext-install pdo pdo_mysql bcmath curl opcache intl gettext gd
25+
RUN docker-php-ext-configure gd --enable-gd --with-webp --with-xpm --with-jpeg --with-freetype
26+
RUN docker-php-ext-install -j$(nproc) gd
27+
28+
# Redis
29+
RUN pecl install redis-5.3.7 \
30+
&& pecl install xdebug-3.2.1 \
31+
&& docker-php-ext-enable redis xdebug
32+
33+
# Memcached
34+
RUN apt-get update && apt-get install -y libmemcached-dev libssl-dev zlib1g-dev \
35+
&& pecl install memcached-3.2.0 \
36+
&& docker-php-ext-enable memcached
37+
38+
# Disable default server
39+
RUN rm -rf /etc/nginx/sites-enabled/default
40+
41+
WORKDIR /var/www/html
42+
43+
COPY --chown=www-data:www-data --chmod=2775 ./webapp /var/www/html
44+
45+
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
46+
47+
COPY ./php/www.conf /usr/local/etc/php-fpm.d/www.conf
48+
49+
COPY ./php/php.ini /usr/local/etc/php/php.ini
50+
51+
RUN nginx -t
52+
53+
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
54+
55+
RUN php artisan cache:clear
56+
RUN php artisan config:clear
57+
RUN php artisan migrate
58+
RUN php artisan storage:link
59+
60+
COPY --chown=www-data:www-data --chmod=2775 ./entrypoint.sh /var/www/entrypoint.sh
61+
62+
RUN chmod +x /var/www/entrypoint.sh
63+
64+
ENTRYPOINT [ "/var/www/entrypoint.sh" ]

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,24 @@
1-
# docker-laravel-nginx
2-
Running php-fpm and nginx processes in the same container with Laravel.
1+
# Nginx and Php-fpm with Laravel on Docker
2+
3+
Running php-fpm and nginx processes in the same container with Laravel.
4+
5+
## Config Mysql in files
6+
7+
```sh
8+
.env
9+
webapp/.env
10+
```
11+
12+
## Build
13+
14+
```sh
15+
# Build up
16+
docker compose up --build -d
17+
18+
# Show
19+
docker compose ps
20+
21+
# Interactive container terminal
22+
docker exec -it app_host bash
23+
docker exec -it mysql_host bash
24+
```

docker-compose-production.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
services:
2+
3+
# Php, nginx service
4+
app:
5+
tty: true
6+
container_name: app_host
7+
restart: always
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+
# - myapp:/home/test/myapp
23+
24+
# Database service
25+
mysql:
26+
tty: true
27+
container_name: mysql_host
28+
image: mysql:8.0
29+
ports:
30+
- 7000:3306
31+
restart: unless-stopped
32+
environment:
33+
- MYSQL_ALLOW_EMPTY_PASSWORD=${DB_ALLOW_EMPTY_PASSWORD}
34+
- MYSQL_DATABASE=${DB_DATABASE}
35+
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
36+
networks:
37+
- internal
38+
# volumes:
39+
# - ./mysql:/docker-entrypoint-initdb.d/
40+
# - db-data:/var/lib/mysql
41+
42+
# Docker volumes
43+
# volumes:
44+
# # Storage
45+
# myapp:
46+
# # Db data
47+
# db-data:
48+
# driver: local
49+
# driver_opts:
50+
# device: ./volume/mysql
51+
# type: none
52+
# o: bind
53+
# # Or
54+
# # db-data: ~
55+
56+
networks:
57+
internal:
58+
driver: bridge

docker-compose.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
services:
2+
3+
# Php, nginx service
4+
app:
5+
tty: true
6+
container_name: app_host
7+
restart: always
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+
- myapp:/home/test/myapp
23+
24+
# Database service
25+
mysql:
26+
tty: true
27+
container_name: mysql_host
28+
image: mysql:8.0
29+
ports:
30+
- 7000:3306
31+
restart: unless-stopped
32+
environment:
33+
- MYSQL_ALLOW_EMPTY_PASSWORD=${DB_ALLOW_EMPTY_PASSWORD}
34+
- MYSQL_DATABASE=${DB_DATABASE}
35+
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
36+
networks:
37+
- internal
38+
# volumes:
39+
# - ./mysql:/docker-entrypoint-initdb.d/
40+
# - db-data:/var/lib/mysql
41+
42+
# Docker volumes
43+
volumes:
44+
# Storage
45+
myapp:
46+
# Db data
47+
db-data:
48+
driver: local
49+
driver_opts:
50+
device: ./volume/mysql
51+
type: none
52+
o: bind
53+
# Or
54+
# db-data: ~
55+
56+
networks:
57+
internal:
58+
driver: bridge

docker/supervisord.conf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Use in Dockerfile
2+
# COPY ./docker/supervisord.conf /etc/supervisord.conf
3+
# CMD ["/usr/bin/supervisord", "-n"]
4+
5+
[program:php-fpm]
6+
command=/opt/remi/php70/root/usr/sbin/php-fpm -c /etc/php-fpm.conf
7+
;command=/usr/sbin/php70-fpm -c /etc/php-fpm.d
8+
stdout_logfile=/dev/stdout
9+
stdout_logfile_maxbytes=0
10+
stderr_logfile=/dev/stderr
11+
stderr_logfile_maxbytes=0
12+
13+
[program:nginx]
14+
command=/usr/sbin/nginx
15+
stdout_logfile=/dev/stdout
16+
stdout_logfile_maxbytes=0
17+
stderr_logfile=/dev/stderr
18+
stderr_logfile_maxbytes=0

entrypoint.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
if [ ! f "vendor/autoload.php" ]; then
4+
composer install --no-progress --no-interactions
5+
fi
6+
7+
if [ ! f ".env" ]; then
8+
echo "Creating .env file"
9+
cp .env.example .env
10+
fi
11+
12+
# Daemonize php-fpm
13+
php-fpm -D
14+
15+
# Enable nginx foreground
16+
nginx -g "daemon off;"

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.

0 commit comments

Comments
 (0)