Skip to content
emacomPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

21 Commits

Folders and files

Repository files navigation

phpbox

Local PHP shops and apps on Docker, with snapshots. create installs the chosen platform version, reset restores it to a snapshot.

Platform Versions Details
prestashop 8.x, 9.x platforms/prestashop
laravel all on Packagist, PHP 8.0+ platforms/laravel
magento 2.4.3+ platforms/magento
mageos 1.0+ (Magento 2.4.6+) platforms/mageos
shopware 6.6+ platforms/shopware
symfony all on Packagist, PHP 8.0+ platforms/symfony
woocommerce all with PHP 8.0+ platforms/woocommerce

Versions, installation, reset, copy and import of each platform are described in its directory. Adding a platform: docs/adding-a-platform.md.

Requirements

  • Linux (x86_64 or arm64)
  • Docker with the docker compose plugin (v2)
  • bash, curl, rsync, python3
  • mmilewski/php-fpm:7.3+ images on Docker Hub or locally
  • vm.max_map_count >= 262144 for OpenSearch

Installation

git clone <repo> ~/phpbox
ln -s ~/phpbox/phpbox ~/.local/bin/phpbox
cp ~/phpbox/phpbox.conf.example ~/phpbox/phpbox.conf

The symlink in ~/.local/bin runs phpbox from any directory. The script resolves it with readlink -f, so boxes/, cache/ and phpbox.conf stay in the repository directory.

Quick start

phpbox create prestashop 8.2.1                 # exact version
phpbox create prestashop 9                     # latest stable 9.x
phpbox create mageos latest
phpbox create magento 2.4.8-p3 --name client
phpbox create woocommerce 10 --wp 6.8.3 --lang en

phpbox clone <platform> git@gitlab.example:client/project.git --branch develop
phpbox db-import project ~/dumps/project.sql.gz

Addresses and credentials: phpbox info <name>.

Commands

Command Description
create <platform> <version> [options] Installation. Version: full, prefix (8.2, 9) or latest
clone <platform> <git-repo> [options] Project from a repository: git clone + composer install, no database
add <platform> <path> [options] Existing project directory as a box, without copying it. Options as in clone, without --branch. See "Existing project directory"
copy <source> <name> Copy of an environment with its current database, new host and database port
db-import <name> <dump> [--strip "<list>"] Database import and configuration for the local environment, then snapshot. --strip: tables imported empty, see "Import without some tables"
db-import <name> --configure-only Repeats the configuration of the loaded database (post-load hook, platform p_after_import, post-import hook, snapshot) without importing again, e.g. after a failed setup:upgrade
reset <name> [--db-only] Restores the database (and files) to the snapshot. Project from clone: database only
snapshot <name> The current state becomes the new reset point
start <name> [--wait] Starts the box and returns once db, php and web run. Search engines and RabbitMQ finish starting in the background, see "Start". --wait: also wait until every service is healthy
stop <name> Stops the box, volumes stay
service add|remove <name> <service[:tag]> Adds or removes a service, recreates the containers and lets the platform update its configuration (Magento env.php)
service disable|enable <name> <service> Switches off or on another compose service of the box, e.g. one from .phpbox/compose.yml. See "Disabled services"
stores <name> [--from <box>] Rewrites the store host maps of the box (Magento: <code>.<name>.localhost per website or store with its own base URL) and reloads nginx. Without --from from the box database, which also gets the new base URLs. --from <box>: the store codes of another box, the database stays untouched, e.g. a box sharing the database of another one
up / down Start / stop the whole phpbox. down frees the ports and remembers running environments, up resumes them
list [--size] [--json] Environments and their status. --size adds the disk usage of boxes/<name> (du, about 0.5 s per box). --json: see "Public interface"
info <name> [--json] Addresses, credentials, platform, PHP version, disk size of boxes/<name>, volumes with size and containers with image and state
capabilities Features of this phpbox as JSON, for scripts and plugins
shell <name> [service] Bash in the PHP container (your UID, composer, wp in PATH) or in the given service, e.g. node
exec <name> [--service S] <command...> Command in the PHP container (or service S) without a TTY, in /var/www/html. For scripts and agents, e.g. phpbox exec shop bin/magento cache:flush
db <name> MariaDB console
logs <name> Container logs
remove <name> [-y] Removes the environment with its database and files
versions <platform> Available versions with their PHP range
create option Description
--name N Environment name, defaults to the platform prefix + version digits. phpmyadmin and mailpit are reserved
--lang xx Shop language (pl → pl_PL)
--php X.Y PHP image version, defaults to the highest supported by the platform
--<service>[:tag] Service from docker/services/, e.g. --redis or --redis:7.4-alpine. Also available in clone

Platform-specific options, e.g. --wp for WooCommerce, are in the platform README.

Services

A service is a docker/services/<name>.yml file. A box runs the services listed in SERVICES in its .env, each in a separate container of that box.

An entry <name>:<tag> sets the image tag. compose() passes it as <NAME>_TAG, e.g. REDIS_TAG. An entry without a tag uses the platform default, then the default in the service file.

SERVICES='opensearch:2.19.1 redis:7.4-alpine node:14-alpine'
Service Image Host in the box network Notes
redis redis:${REDIS_TAG:-7.2-alpine} redis No disk persistence
valkey valkey/valkey:${VALKEY_TAG:-8.1-alpine} valkey Redis replacement
rabbitmq rabbitmq:${RABBITMQ_TAG:-3.13-management-alpine} rabbitmq Panel at the address from phpbox info (guest / guest). Requires a -management tag. Healthcheck: AMQP port 5672 open (nc, or bash /dev/tcp in non-Alpine tags), because rabbitmq-diagnostics starts an Erlang VM that takes seconds and several CPU cores on every check
opensearch phpbox/opensearch-slim:${OPENSEARCH_TAG:-2.19.1}, built from opensearchproject/opensearch search Disk watermarks disabled. indexing_pressure.memory.limit 25% of the 512 MB heap (default 10%, about 51 MB): a full reindex of a large catalog, e.g. Elasticsuite with 70 000 products, got 429 rejected_execution_exception. Bundled plugins (security, ML, k-NN, alerting, SQL…) removed, only SEARCH_PLUGINS installed: the node starts in about 60% of the time. OpenSearch 1.x keeps the performance analyzer, which its entrypoint requires. See "Search plugins"
elasticsearch phpbox/elasticsearch:${ELASTICSEARCH_TAG:-7.17.25}, built from elasticsearch search Disk watermarks disabled, plugins from SEARCH_PLUGINS. See "Search plugins"
memcached memcached:${MEMCACHED_TAG:-1.6-alpine} memcached 256 MB
cron image and volumes of the php service (extends) — Runs the platform P_CRON every 60 s
node node:${NODE_TAG:-22-alpine} — Tool container with the box code and the host UID. Entry: phpbox shell <name> node
varnish varnish:${VARNISH_TAG:-7.6-alpine} varnish HTTP cache between the proxy and the platform. See "Varnish"
xdebug mmilewski/php-fpm-xdebug:<PHP_VERSION> in place of the php image — Xdebug 3, XDEBUG_MODE=debug, start_with_request=trigger, client host.docker.internal:9003, PHP_IDE_CONFIG=serverName=<name>. Remove the service to get the plain image back. The plain php image has Xdebug loaded but XDEBUG_MODE=off
playwright mcr.microsoft.com/playwright:${PLAYWRIGHT_TAG:-v1.49.1-noble} — Browser tests in the network of php, PLAYWRIGHT_BASE_URL = box address. The tag must match the @playwright/test version of the project. Entry: phpbox exec <name> --service playwright npx playwright test
Situation Action
--<service>[:tag] flag on create or clone The service is added to SERVICES. A later tag of the same service replaces the earlier one
Service added later phpbox service add <name> <service[:tag]>: SERVICES saved, containers recreated, platform p_service_changed and the post-service-change hook run
Service removed phpbox service remove <name> <service>. The search engine from SEARCH stays
SEARCH in .env load_env adds its value to the services
Tag changed later Edit the tag in SERVICES and run phpbox start <name>. A downgrade does not start on the existing volume
New service A docker/services/<name>.yml file with image: <image>:${<NAME>_TAG:-<default>}. The --<name> flag works immediately

compose() in src/docker.sh merges the files in this order: docker/compose.yml, platform P_COMPOSE, one file per entry in SERVICES, <code>/.phpbox/compose.yml and compose.*.yml, boxes/<name>/compose.yml and compose.*.yml.

Start

start and up do not wait for opensearch, elasticsearch and rabbitmq: php depends on them with condition: service_started. A box answers after a few seconds, while OpenSearch needs up to a minute to load its plugins. Until then, search and reindexing get a connection error; scripts that need the search engine run phpbox start <name> --wait first. create, clone, add, db-import, copy, reset, snapshot and service add|remove still wait for every healthcheck, because they run the platform installer or import right after the start.

Search plugins

SEARCH_PLUGINS lists the plugins installed into the opensearch and elasticsearch images, default analysis-icu analysis-phonetic. A bundled OpenSearch plugin that a project needs, e.g. opensearch-knn, goes on this list too; it is downloaded again on build. A list other than the default gets its own image tag with a suffix from the list, e.g. phpbox/opensearch-slim:2.19.1-247e0b29, so boxes with different lists do not share an image. The value from phpbox.conf goes to the box .env on create, clone and add. A box .env without the variable uses the default list.

SEARCH_PLUGINS="analysis-icu analysis-phonetic analysis-stempel"   # boxes/<name>/.env, then: phpbox start <name>

Disabled services

phpbox service disable <name> <service> stops and removes the container of a compose service and keeps it off on every start, without editing the project files. The list lives in DISABLED_SERVICES in the box .env. compose() gives each listed service the inactive profile phpbox-disabled in boxes/<name>/.compose-disabled.yml. stop, down and remove run docker compose --profile '*' down, so containers of disabled services are removed too.

Rule Reason
web, php, db, loopback cannot be disabled Core containers of every box
Services from SERVICES cannot be disabled, except the search engine from SEARCH Use service remove, which also reconfigures the platform. The search engine cannot be removed, but a box that uses the search engine of another box can switch its own off
No depends_on on a disabled service Compose refuses a dependency on a service with an inactive profile. php depends on the services from SERVICES, so .compose-disabled.yml marks that dependency required: false

phpbox service enable <name> <service> removes it from the list and starts it when the box is running.

Varnish

Request Path
Browser phpbox-proxy → web:80 → varnish:80 → web:8080 → php
Without the service phpbox-proxy → web:80 → php

docker/nginx/varnish.server.conf is mounted in web only with the service: it adds listen 8080 and sends requests on port 80 to Varnish. The VCL is boxes/<name>/varnish.vcl when it exists, otherwise docker/varnish/default.vcl (backend web:8080). Magento writes its own VCL, see the platform README.

Without disabled disk watermarks, OpenSearch 2.x sets a persistent cluster.blocks.create_index block above 95% disk usage and reindexing fails with 403. The search index is not part of the snapshot.

Architecture

phpbox                   # default configuration, loads src/*.sh, dispatches the command
src/core.sh              # messages, options, platform and .env loading, ports, shop address
src/docker.sh            # box compose, shared containers, box_up / box_down, tools
src/tls.sh               # mkcert, CA, box certificates
src/database.sh          # queries, dump, import, snapshot
src/commands.sh          # cmd_* in workflow order: create, clone, add, db-import, copy, reset, service…
src/strip-tables.awk     # db-import --strip: drops INSERT statements of the listed tables
src/composer_auth.py     # COMPOSER_AUTH from the host auth.json and keys from phpbox.conf
src/versions.py          # version and PHP selection from platform APIs
docker/shared.yml        # compose project "phpbox": proxy, Mailpit, phpMyAdmin, phpbox network
docker/compose.yml       # web (nginx) + php (mmilewski/php-fpm) + loopback (socat) + db (MariaDB)
docker/php.ini           # memory 2G, sendmail → Mailpit, date.timezone = SHOP_TZ
docker/proxy.conf        # phpbox-proxy: <name>.localhost → box web, phpmyadmin/mailpit.localhost
docker/phpmyadmin.php    # phpMyAdmin server list from boxes/*/.env
docker/services/<service>.yml  # see "Services"
docker/nginx/            # nginx files mounted by services, e.g. varnish.server.conf
docker/varnish/default.vcl     # VCL of the varnish service without a box VCL
platforms/<p>/           # platform.sh, nginx.conf, README.md
docs/                    # how phpbox works, adding a platform
tests/                   # tests: tests/run.sh
cache/                   # packages, composer and wp-cli cache, bin/{composer,wp,mailpit}, certs/
boxes/<name>/{.env,<name>/,snapshot/,nginx/}  # code in <name>/, boxes created earlier keep www/
boxes/.up                # boxes running before phpbox down
Container Role
web nginx:stable-alpine, configuration from platforms/<p>/nginx.conf, no host port. Every start runs nginx -t and stops with the nginx error instead of leaving the shop at 502
php mmilewski/php-fpm:<PHP_VERSION>, runs with the host UID/GID
loopback alpine/socat in the php network namespace. 127.0.0.1:HTTP_PORT → web:80, 127.0.0.1:HTTPS_PORT → phpbox-proxy:443, so the shop address works from the php container (WordPress cron, Action Scheduler, Magento self-calls)
db MariaDB, version matched to the platform. Database named after the box (- → _), user shop / shop. copy keeps the source database name
services from SERVICES Separate per environment, see "Services"
phpbox-proxy nginx:stable-alpine on HTTP_PORT and HTTPS_PORT, <name>.localhost and *.<name>.localhost → phpbox-<name>-web-1. Terminates TLS, the box receives HTTP with X-Forwarded-Proto
phpbox-mailpit One container for all environments. Receives mail from mail() and sendmail transports (mailpit sendmail -t in sendmail_path; Symfony Mailer accepts only -t or -bs). UI: https://mailpit.localhost or MAILPIT_PORT
phpbox-phpmyadmin phpmyadmin:5.2, one container for all environments, attached to the phpbox-<name>_default network together with phpbox-proxy. UI: https://phpmyadmin.localhost or PHPMYADMIN_PORT

phpbox-proxy, phpbox-mailpit and phpbox-phpmyadmin form the phpbox compose project from docker/shared.yml. Code walkthrough: docs/how-phpbox-works.md.

PHP images cover versions 7.3–8.5.

Reset

Step Mechanism
Database snapshot mariadb-dump → snapshot/db.sql.gz
File snapshot rsync --delete → snapshot/www/, without paths from p_snapshot_excludes
Database reset DROP DATABASE + dump import
File reset rsync --delete from the snapshot
Cleanup Platform p_clear_cache

A full reset overwrites code edited in boxes/<name>/<name>. Use reset --db-only in that case.

phpbox create prestashop 8.2 --name module-test
phpbox snapshot module-test     # after configuring the admin panel
phpbox reset module-test        # back to the snapshot state

Environment copy

copy creates an independent environment from the current state of another one. It allows several agents or people to work on one shop in parallel.

phpbox copy m249 m249-a
Step Mechanism
Database mariadb-dump of the source → import into the new container
Files rsync of the box code without paths from p_snapshot_excludes
Addresses Platform p_rehost <old-url>
Snapshot New, with the state after the copy

Existing project directory

add creates a box for code that already lives elsewhere, e.g. in an IDE project folder. The path goes to CODE_DIR in .env and is mounted at /var/www/html.

phpbox add magento ~/projects/shop --name shop --redis --rabbitmq
phpbox db-import shop ~/dumps/shop.sql.gz --strip "@log @sessions"
Topic Behaviour
Kind project, like clone: reset restores the database only
Dependencies composer install only when vendor/ is missing
PHP The highest version allowed by every package in composer.lock, see the platform README. --php X.Y overrides it
remove Removes the box, the database and boxes/<name>. The code directory stays
One directory A directory already used by another box is refused

Project configuration (.phpbox/)

A project can keep its box settings in git, in .phpbox/ of the code directory. Every file is optional and box files in boxes/<name>/ are applied after them.

File Use
.phpbox/compose.yml, .phpbox/compose.*.yml Extra services or overrides of the box compose project. Relative paths resolve against docker/, use ${WWW_DIR} for project files
.phpbox/nginx/*.server.conf Included in the server block of the platform nginx.conf, e.g. redirects or extra locations
.phpbox/nginx/*.http.conf Included before the server block, e.g. map
.phpbox/nginx.conf Replaces the platform file of the project included in the server block (Magento: nginx.conf.sample), e.g. when composer install writes a sample nginx rejects. Applied by phpbox start <name>
.phpbox/hooks/<event> Hook script, see "Hooks"

boxes/<name>/nginx/*.server.conf and *.http.conf work the same way for one box.

Hooks

A hook is a bash script .phpbox/hooks/<event> in the code directory or boxes/<name>/hooks/<event>. Both run when present, project first. They run in the php container in /var/www/html with the host UID, like phpbox exec. A failing hook stops the command.

Event After
post-start start, up and add started the box
post-load db-import loaded the SQL, before the platform configuration (p_after_import). Also runs with --configure-only. For data fixes that the configuration needs, e.g. before Magento setup:upgrade
post-import db-import configured the database, before the snapshot
post-service-change service add or service remove and the platform p_service_changed
Variable Value
PHPBOX_EVENT, PHPBOX_NAME Event and box name
SHOP_URL Box address
SERVICES Service names, without tags
PHPBOX_ACTION, PHPBOX_SERVICE post-service-change only: add or remove and the service
DB_NAME, DB_USER, DB_PASS Database of the db host

Import without some tables

--strip lists tables whose rows are skipped, so they are created empty. * is a wildcard and @<group> is a group of the platform, e.g. @log for Magento. DB_STRIP in .env is the default list, --strip "" imports everything.

phpbox db-import shop dump.sql.gz --strip "@log @sessions report_*"

The filter in src/strip-tables.awk reads the decompressed stream and handles single-line and multi-line INSERT statements.

Public interface

Scripts and plugins may rely on the items below. A change to any of them is a breaking change.

Item Contract
phpbox list --json Array of the objects from info --json, without containers
phpbox info <name> --json name, platform, title, version, kind, php, status (running/stopped), url, admin (url, user, password, or null), code_dir, external_code, box_dir, services (name, tag), disabled_services, database (host, port, name, user, password, root_password, image), shared (phpmyadmin, mailpit), optional stores (type, code, url), containers (service, name, image, state), volumes (name, size)
phpbox capabilities {"capabilities": [...], "platforms": [{"name", "title", "strip_groups": {"<group>": [tables]}}], "services": [...], "shared": {"phpmyadmin", "mailpit"}}. Check a feature in capabilities instead of comparing versions
phpbox exec <name> [--service S] <command...> Command in the box without a TTY, in /var/www/html
Files .phpbox/ and boxes/<name>/ files from "Project configuration", hooks from "Hooks", boxes/<name>/varnish.vcl
Containers Compose project phpbox-<name>, containers phpbox-<name>-<service>-1, network phpbox-<name>_default

Configuration

phpbox.conf variable Default
HTTP_PORT 80
HTTPS_PORT 443
BASE_PORT 8080, first port slot
MAILPIT_PORT 18025, also https://mailpit.localhost
PHPMYADMIN_PORT 18026, also https://phpmyadmin.localhost
SHOP_LANG, SHOP_COUNTRY, SHOP_CURRENCY, SHOP_TZ pl, pl, PLN, Europe/Warsaw
ADMIN_USER, ADMIN_EMAIL, ADMIN_PASSWORD admin, admin@example.com, admin123
ADMIN_PATH admin, Magento admin path (backend.frontName). add takes it from an existing app/etc/env.php
SEARCH_PLUGINS analysis-icu analysis-phonetic, see "Search plugins"
MAGENTO_PUBLIC_KEY, MAGENTO_PRIVATE_KEY empty, required for magento
GITHUB_TOKEN empty, required for private https://github.com repositories in composer.json
UPDATE_CHECK 1, checks emacom/phpbox main once per 24 h in the background and prints a notice when the local checkout lacks the latest commit. 0 disables it

Values are written to boxes/<name>/.env on create. Changing phpbox.conf later does not affect existing environments.

Box .env variable Change after create
PHP_VERSION, SERVICES, PORT, DB_PORT, SEARCH_PLUGINS Applied by phpbox start <name>
ADMIN_PATH Shown by info at once, written to app/etc/env.php by the next db-import
DB_IMAGE Upgrade only. A MariaDB downgrade does not start on the existing volume
NAME, DB_NAME, SHOP_HOST Do not change. They define the box directory, compose project, volume, certificate and base_url
ADMIN_*, SHOP_LANG, SHOP_* No effect. Used only by create and db-import

Project from a repository

clone and db-import work for every platform. Database and file changes after import are described in the platform README.

phpbox clone <platform> git@gitlab.example:client/project.git --branch develop
phpbox db-import project ~/dumps/project.sql.gz
clone option Description
--branch B Branch to clone
--name N Environment name, defaults to the repository name
--php X.Y Forces the PHP version
--<service>[:tag] Service from docker/services/
Import step Change
Dump utf8mb4_0900_* collations → utf8mb4_unicode_ci, removed DEFINER, /*!80xxx */ comments and GTID_PURGED
Configuration Platform p_after_import
Snapshot Database only. reset does not touch files, so code and git history stay

Private package access

Mechanism Source
SSH keys ~/.ssh mounted read-only at the same path
SSH agent $SSH_AUTH_SOCK at the time of clone, stored in boxes/<name>/compose.yml
auth.json From the repository and ~/.composer/auth.json or ~/.config/composer/auth.json
phpbox.conf MAGENTO_* → http-basic.repo.magento.com, GITHUB_TOKEN → github-oauth.github.com, override entries from the host auth.json

HTTPS

Every box runs on https://. Certificates are issued by a local CA from mkcert.

Element Mechanism
mkcert From PATH or downloaded to cache/bin/mkcert
CA The first run calls mkcert -install and asks for the sudo password. The CA lives in mkcert -CAROOT
Chrome and Firefox Require certutil: sudo apt install libnss3-tools, then mkcert -install
Box certificate cache/certs/<name>.pem for <name>.localhost and *.<name>.localhost. Browsers reject a *.localhost wildcard
Shared UI certificates cache/certs/phpmyadmin.pem and mailpit.pem, hosts from SHARED_HOSTS in phpbox
Certificate selection phpbox-proxy by SNI: map $ssl_server_name → /certs/<name>.pem
PHP sees HTTPS fastcgi_param HTTPS on when X-Forwarded-Proto is https
Trust in the php container cache/certs/ca-bundle.pem: host system CAs + mkcert CA. Set in SSL_CERT_FILE, openssl.cafile, curl.cainfo
Situation Action
Box created before HTTPS Database addresses stay http://. Switch: phpbox copy <box> <new>
phpbox-proxy with a different port configuration docker compose in shared_up recreates the container

cache/ can be deleted after phpbox down. Tools and certificates are recreated on phpbox up, the CA stays in mkcert -CAROOT.

Tests

tests/run.sh                         # unit: functions, versions, platforms, syntax, shellcheck (a few seconds)
tests/run.sh smoke                   # full Docker run for WooCommerce
tests/run.sh smoke prestashop 8.2.8  # full run for the chosen platform and version
Test Scope
unit parse_options, addresses, ports, .env, dump_fix, src/versions.py, src/composer_auth.py, platform contract, bash -n, shellcheck
smoke create → HTTPS → loopback → Mailpit → reset → copy → stop/start → down/up → remove. For WooCommerce also clone + db-import

smoke runs in a temporary directory on ports 18080/18443 and requires a stopped phpbox (phpbox down). Your boxes and phpbox.conf stay untouched, and no containers or volumes remain after the test.

Variable Meaning
PHPBOX_CONF Configuration file instead of phpbox.conf
PHPBOX_BOXES_DIR Boxes directory instead of boxes/

Multiple environments

Every shop has the address https://<name>.localhost. With HTTPS_PORT other than 443 the address includes that port.

Layer Isolation
Docker Separate compose project: containers, network, volumes, database port
Browser Separate host, so separate cookies
DNS *.localhost → loopback in Chrome, Firefox, curl and systemd-resolved, no /etc/hosts

phpbox-proxy listens on 127.0.0.1 and [::1] because the system resolves *.localhost to ::1.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages