Skip to content

Commit

Permalink
Add a switcher for PHP that enables or disables xDebug
Browse files Browse the repository at this point in the history
  • Loading branch information
ityetti committed Nov 29, 2024
1 parent 0cd2ae0 commit 8ca1067
Show file tree
Hide file tree
Showing 16 changed files with 1,994 additions and 35 deletions.
File renamed without changes.
9 changes: 4 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Ignore Mac DS_Store files
.DS_Store
# Ignore PHPStorm folder
.idea
# Ignore Magento files
magento2
# Ignore Magento files
.docker-sync
.docker-sync
data
letsencrypt
.env
52 changes: 36 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
### Traefik + Nginx + Redis + PHP-FPM + MySQL + XDebug + Mailpit + RabbitMQ + OpenSearch + Varnish

The docker stack is composed of the following containers
- traefik
- nginx
- php-fpm
- redis
- mysql
- mailpit
- rabbitmq
- opensearch
- opensearch-dashboard
- varnish

| Name | Version |
|----------------------|---------|
| traefik | 3.2 |
| nginx | 1.22 |
| php-fpm | 8.2 |
| php-fpm-xdebug | 3.2.2 |
| redis | 7.0 |
| mysql | 8.0.34 |
| mailpit | 1.21 |
| rabbitmq | 3.11 |
| opensearch | 2.5.0 |
| opensearch-dashboard | 2.5.0 |
| varnish | 7.3 |

### Container traefik
Starts a reverse proxy and load balancer for project<br>
Expand All @@ -27,7 +31,12 @@ Mounts the folder magento2 from the project main folder into the container volum
### Container php-fpm
Builds from the php-fpm folder.<br>
Mounts the folder magento2 from the project main folder into the container volume `/home/magento`.<br>
This container includes all dependencies for Magento 2 (Also contain composer, code sniffer, xDebug etc.).<br>
This container includes all dependencies for Magento 2.<br>

### Container php-fpm-xdebug
Builds from the php-fpm-xdebug folder.<br>
Mounts the folder magento2 from the project main folder into the container volume `/home/magento`.<br>
This container includes all dependencies for Magento 2 (also contain xDebug).<br>

### Container redis:
Starts a redis container.<br>
Expand Down Expand Up @@ -74,9 +83,10 @@ Starts a varnish container.<br>
Opens up port: `6081`

## Setup
Edit your `.env` file in root folder, change `PROJECT_NAME` and `PROJECT_VIRTUAL_HOST`:<br>
Copy your `.env.sample` to `.env` file in root folder, and change `PROJECT_NAME` and `PROJECT_VIRTUAL_HOST`:<br>
`PROJECT_NAME` - help you to create simple and clear container names.<br>
`PROJECT_VIRTUAL_HOST` - it is your main url address.<br>

For example:

PROJECT_NAME=magento2
Expand All @@ -96,13 +106,19 @@ To the run installation process use next commands.<br>
Create new project:

./scripts/composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.7-p3 /home/magento
Install project:
Install project (don't forget to change **--base-url** to yours):

./scripts/magento setup:install --base-url=https://magento2.test/ --db-host=mysql --db-name=magento_db --db-user=magento_user --db-password="PASSWD#" --admin-firstname=admin --admin-lastname=admin [email protected] --admin-user=admin --admin-password=admin1! --language=en_US --currency=USD --timezone=America/Chicago --use-rewrites=1 --opensearch-host=opensearch --opensearch-port=9200 --search-engine=opensearch

## Setting up Magento
To access the magento homepage, go to the following url: https://magento2.test<br>

## How to use xDebug
You could enable or disable xDebug with the next command: `./scripts/switch_mode [fpm|xdebug]`<br>
`fpm` - Enable container without xDebug <br>
`xdebug` - Enable container with xDebug <br>


Also, you can open:<br>
https://traefik.magento2.test - **Traefik Dashboard** (traefik/traefik123 for access)<br>
https://mail.magento2.test - **Mailpit**<br>
Expand All @@ -112,7 +128,7 @@ https://rabbit.magento2.test - **RabbitMQ** (guest/guest for access)<br>

## Feature Updates
- v1.0.0 - Stable release
- v1.0.1 - Updated to PHP 7.4.x, added docker-sync for MacOS users
- v1.0.1 - Updated to PHP 7.4.x, added docker-sync for macOS users
- v1.0.2 - Fix xDebug, add rabbitmq management, fix email sending
- v1.0.3 - Updated to PHP 8.1.x
- v1.0.4 - Fix xDebug for stable work
Expand All @@ -121,7 +137,11 @@ https://rabbit.magento2.test - **RabbitMQ** (guest/guest for access)<br>
- v1.0.7 - Add traefik, optimization for varnish, remove nginx-proxy
- v1.0.8 - Replace mailhog to mailpit
- v1.0.9 - Add n98-magerun2
- v1.1.0 - Add a switcher for PHP that enables or disables xDebug

## Branches
- master (for magento 2.4.6 and higher)
- m244 (for magento 2.4.4 and higher till 2.4.6)
| Name | Magento versions |
|----------------------|----------------------------------------|
| master | 2.4.6 and higher |
| m244 | 2.4.4 up to 2.4.6 |
| develop | like master with untested improvements |
18 changes: 15 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ services:
container_name: ${PROJECT_NAME}-nginx
volumes:
- ./magento2:/home/magento:cached
depends_on:
- php-fpm
networks:
- magento
labels:
Expand All @@ -57,9 +55,23 @@ services:
labels:
- "traefik.enable=true"
- "traefik.docker.network=magento"

php-fpm-xdebug:
build: ./php-fpm-xdebug
container_name: ${PROJECT_NAME}-php-fpm-xdebug
volumes:
- ./magento2:/home/magento:cached
- ./php-fpm/php.ini:/etc/php.ini
networks:
- magento
labels:
- "traefik.enable=true"
- "traefik.docker.network=magento"
environment:
PHP_IDE_CONFIG: "serverName=Docker"
XDEBUG_CONFIG: "client_host=host.docker.internal"
profiles:
- xdebug

redis:
image: arm64v8/redis:7.0
Expand Down Expand Up @@ -92,7 +104,7 @@ services:
- magento

mailpit:
image: axllent/mailpit
image: axllent/mailpit:v1.21
container_name: ${PROJECT_NAME}-mailpit
labels:
- "traefik.enable=true"
Expand Down
126 changes: 126 additions & 0 deletions php-fpm-xdebug/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
FROM arm64v8/php:8.2-fpm as php

ARG MAGENTO_ROOT=/home/magento

ENV PHP_MEMORY_LIMIT 4G
ENV PHP_VALIDATE_TIMESTAMPS 1
ENV DEBUG false
ENV MAGENTO_RUN_MODE default
ENV UPLOAD_MAX_FILESIZE 64M
ENV PHPRC ${MAGENTO_ROOT}/php.ini
ENV PHP_EXTENSIONS bcmath bz2 calendar exif gd gettext intl mysqli opcache pdo_mysql redis soap sockets sodium sysvmsg sysvsem sysvshm xsl zip pcntl

# Install dependencies
RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y --no-install-recommends \
apt-utils \
sendmail-bin \
sendmail \
sudo \
iproute2 \
git \
gnupg2 \
ca-certificates \
lsb-release \
software-properties-common \
libbz2-dev \
libjpeg62-turbo-dev \
libpng-dev \
libfreetype6-dev \
libgmp-dev \
libgpgme11-dev \
libicu-dev \
libldap2-dev \
libpcre3-dev \
libpspell-dev \
libtidy-dev \
libxslt1-dev \
libyaml-dev \
libzip-dev \
zip \
&& rm -rf /var/lib/apt/lists/*

# Install Mailpit
RUN bash -c "$(curl -sL https://raw.githubusercontent.com/axllent/mailpit/develop/install.sh)"
RUN echo "sendmail_path = /usr/local/bin/mailpit sendmail -S mailpit:1025" >> /usr/local/etc/php/conf.d/sendmail.ini

# Configure the gd library
RUN docker-php-ext-configure \
gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-configure \
opcache --enable-opcache

# Install required PHP extensions
RUN docker-php-ext-install -j$(nproc) \
bcmath \
bz2 \
calendar \
exif \
gd \
gettext \
gmp \
intl \
mysqli \
opcache \
pdo_mysql \
pspell \
shmop \
soap \
sockets \
sysvmsg \
sysvsem \
sysvshm \
tidy \
xsl \
zip \
pcntl

RUN pecl install -o -f \
gnupg \
mailparse \
msgpack \
oauth \
pcov \
raphf \
redis \
xdebug-3.2.2 \
xmlrpc-1.0.0RC3 \
yaml

# Install Composer
RUN apt-get update
RUN apt-get install -y wget
RUN wget -O composer-setup.php https://getcomposer.org/installer
RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer
RUN chmod +x /usr/local/bin/composer

# Install n98-magerun2
RUN wget https://files.magerun.net/n98-magerun2.phar \
&& chmod +x n98-magerun2.phar \
&& mv n98-magerun2.phar /usr/local/bin/n98-magerun2

COPY etc/php-fpm.ini /usr/local/etc/php/conf.d/magento.ini
COPY etc/php-xdebug.ini /usr/local/etc/php/conf.d/xdebug-settings.ini
COPY etc/php-pcov.ini /usr/local/etc/php/conf.d/pcov-settings.ini
COPY etc/php-fpm.conf /usr/local/etc/
COPY etc/php-gnupg.ini /usr/local/etc/php/conf.d/gnupg.ini

RUN groupadd -g 2000 magento && useradd -g 2000 -u 2000 -d ${MAGENTO_ROOT} -s /bin/bash magento

COPY docker-entrypoint.sh /docker-entrypoint.sh
RUN ["chmod", "+x", "/docker-entrypoint.sh"]

RUN mkdir -p ${MAGENTO_ROOT}

VOLUME ${MAGENTO_ROOT}

RUN chown -R magento:magento /usr/local /var/www /var/log /usr/local/etc/php/conf.d ${MAGENTO_ROOT}

ENTRYPOINT ["/docker-entrypoint.sh"]

WORKDIR ${MAGENTO_ROOT}

USER root

CMD ["php-fpm", "-R"]
24 changes: 24 additions & 0 deletions php-fpm-xdebug/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

[ "$DEBUG" = "true" ] && set -x

PHP_EXT_DIR=/usr/local/etc/php/conf.d

# Enable PHP extensions
PHP_EXT_COM_ON=docker-php-ext-enable

[ -d ${PHP_EXT_DIR} ] && rm -f ${PHP_EXT_DIR}/docker-php-ext-*.ini

if [ -x "$(command -v ${PHP_EXT_COM_ON})" ] && [ ! -z "${PHP_EXTENSIONS}" ]; then
${PHP_EXT_COM_ON} ${PHP_EXTENSIONS}
fi

# Set host.docker.internal if not available
HOST_NAME="host.docker.internal"
HOST_IP=$(php -r "putenv('RES_OPTIONS=retrans:1 retry:1 timeout:1 attempts:1'); echo gethostbyname('$HOST_NAME');")
if [[ "$HOST_IP" == "$HOST_NAME" ]]; then
HOST_IP=$(/sbin/ip route|awk '/default/ { print $3 }')
printf "\n%s %s\n" "$HOST_IP" "$HOST_NAME" >> /etc/hosts
fi

exec "$@"
25 changes: 25 additions & 0 deletions php-fpm-xdebug/etc/php-fpm.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[global]

error_log = /proc/self/fd/2
daemonize = no

[www]

; if we send this to /proc/self/fd/1, it never appears
access.log = /proc/self/fd/2

listen = [::]:9000

pm = dynamic
pm.max_children = 10
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6
pm.status_path = /status

clear_env = no

env[MAGE_MODE] = $MAGENTO_RUN_MODE

; Ensure worker stdout and stderr are sent to the main error log.
catch_workers_output = yes
6 changes: 6 additions & 0 deletions php-fpm-xdebug/etc/php-fpm.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
memory_limit = ${PHP_MEMORY_LIMIT}
opcache.enable = 1
opcache.validate_timestamps = ${PHP_VALIDATE_TIMESTAMPS}
user_ini.filename =
max_execution_time = 18000
max_input_time = 18000
1 change: 1 addition & 0 deletions php-fpm-xdebug/etc/php-gnupg.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extension = gnupg.so
2 changes: 2 additions & 0 deletions php-fpm-xdebug/etc/php-pcov.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pcov.enabled = 1
pcov.directory = ${MAGENTO_ROOT}
File renamed without changes.
Loading

0 comments on commit 8ca1067

Please sign in to comment.