Easily build PHP production environment based on Docker, integrating commonly used services such as OpenResty, PHP, MariaDB, Redis, Memcached.
git clone --depth 1 https://github.com/seatonjiang/monolith.git
Enter the project folder:
cd monolith/
Rename the environment configuration file (if you don't execute this command, the default configuration will be used):
cp env.example .env
Edit the .env
file and modify the configuration as needed:
vi .env
Configuration items:
# PHP version (supports 8.1-8.4)
PHP_VERSION=8.4-fpm-alpine
# MariaDB default database name
MARIADB_DATABASE_NAME=monolith
# phpMyAdmin access port
PHPMYADMIN_WEB_PORT=28080
Modify the configuration files in the secrets
directory:
mariadb-root-pwd
: MariaDB administrator password (username isroot
)mariadb-user-name
: MariaDB username (default isuser
)mariadb-user-pwd
: MariaDB user password
Tip: In production environments, be sure to change the default passwords, ensure strong passwords are used, and access with user-level permissions.
Build and run all containers in the background:
docker compose up -d
- Local environment:
http://localhost
- Online environment:
http://server IP address
Tip: The default site directory is
wwwroot/default
. For improved security in production environments, uncomment thereturn 403;
configuration indefault.conf
(services/openresty/conf.d/default.conf
), and delete or backup the default site directory. This prevents unauthorized access and potential security risks.
Project directory structure:
monolith
βββ data Data persistence directory
β βββ mariadb MariaDB data directory
β βββ redis Redis data directory
βββ logs Log storage directory
β βββ mariadb MariaDB log directory
β βββ openresty OpenResty log directory
β βββ php PHP log directory
β βββ redis Redis log directory
βββ secrets Secret configuration directory
β βββ mariadb-root-pwd MariaDB administrator password
β βββ mariadb-user-name MariaDB username
β βββ mariadb-user-pwd MariaDB user password
βββ services Service configuration directory
β βββ mariadb MariaDB configuration directory
β βββ memcached Memcached configuration directory
β βββ openresty OpenResty configuration directory
β βββ php PHP configuration directory
β βββ phpmyadmin phpMyAdmin configuration directory
β βββ redis Redis configuration directory
βββ wwwroot Web service root directory
β βββ default Default site directory
βββ compose.yaml Docker Compose configuration file
βββ env.example Environment configuration example file
# Build and run all containers in the background
docker compose up -d
# Build and run specific containers (without running phpMyAdmin)
docker compose up -d openresty php mariadb redis memcached
# Stop all containers and remove network
docker compose down
# Manage specific services (using PHP container as an example)
docker compose start php # Start service
docker compose stop php # Stop service
docker compose restart php # Restart service
docker compose build php # Rebuild service
During operations, docker exec -it
is often used to enter containers. Here are common commands:
# Enter running PHP container
docker exec -it php /bin/sh
# Enter running OpenResty container
docker exec -it openresty /bin/sh
# Enter running MariaDB container
docker exec -it mariadb /bin/bash
# Enter running Redis container
docker exec -it redis /bin/sh
# Enter running Memcached container
docker exec -it memcached /bin/sh
# Enter running phpMyAdmin container
docker exec -it phpmyadmin /bin/bash
You can optimize PHP performance by modifying the services/php/php.ini
file according to your actual situation. Below are the optimized contents:
# Execution time and memory limits
max_execution_time = 180 # Maximum script execution time (seconds)
memory_limit = 256M # Maximum memory available for PHP processes
max_input_time = 300 # Maximum time for each script to parse request data (seconds)
# Form and upload limits
max_input_vars = 5000 # Maximum number of input variables
post_max_size = 65M # Maximum POST data size
upload_max_filesize = 64M # Maximum upload file size
# Regional settings
date.timezone = Asia/Shanghai # Timezone setting
# Error handling
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT # Error reporting level
error_log = /var/log/php/error.log # Error log location
You can optimize MariaDB performance by modifying the services/mariadb/mariadb.cnf
file according to your actual situation. Below are optimization suggestions based on server resources:
# Small server (2GB memory)
innodb_buffer_pool_size=256M # InnoDB buffer pool size
tmp_table_size=128M # Maximum size for in-memory temporary tables
max_heap_table_size=128M # Maximum size for user-created memory tables
# Medium server (4GB memory)
innodb_buffer_pool_size=512M # InnoDB buffer pool size
tmp_table_size=256M # Maximum size for in-memory temporary tables
max_heap_table_size=256M # Maximum size for user-created memory tables
# Large server (8GB+ memory)
innodb_buffer_pool_size=2G # InnoDB buffer pool size
tmp_table_size=512M # Maximum size for in-memory temporary tables
max_heap_table_size=512M # Maximum size for user-created memory tables
# Performance monitoring (enable when needed in low-spec production environments)
performance_schema=ON
performance_schema_max_table_instances=400
You can optimize Redis performance by modifying the services/redis/redis.conf
file according to your actual situation. Below are the optimized contents:
# Network configuration
bind 0.0.0.0 # Allow Redis service access from any IP address, Redis service is only used internally, can use 0.0.0.0
# Persistence strategy
save 900 1 # At least 1 key modified within 900 seconds
save 300 10 # At least 10 keys modified within 300 seconds
save 60 10000 # At least 10000 keys modified within 60 seconds
# Security configuration
rename-command FLUSHALL "" # Disable command to clear all databases
rename-command EVAL "" # Disable command to execute Lua scripts
rename-command FLUSHDB "" # Disable command to clear current database
Name | Registry | Tag | Build Date |
---|---|---|---|
PHP 8.1 | ghcr.io/seatonjiang/php |
8.1-fpm-alpine | 2025-09-15 |
PHP 8.2 | ghcr.io/seatonjiang/php |
8.2-fpm-alpine | 2025-09-15 |
PHP 8.3 | ghcr.io/seatonjiang/php |
8.3-fpm-alpine | 2025-09-16 |
PHP 8.4 | ghcr.io/seatonjiang/php |
8.4-fpm-alpine | 2025-09-15 |
OpenResty | ghcr.io/seatonjiang/openresty |
alpine | 2025-09-16 |
Name | Registry | Sync Date |
---|
Adding New Website to OpenResty
To add a new website in OpenResty, follow these steps:
Create a new configuration file in the services/openresty/conf.d/
directory, for example example.com.conf
:
server {
listen 80 reuseport;
listen [::]:80 reuseport;
server_name example.com;
location / {
return 301 https://example.com$request_uri;
}
}
server {
listen 443 ssl reuseport;
listen [::]:443 ssl reuseport;
http2 on;
server_name example.com;
root /var/www/example.com;
index index.html index.php;
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
access_log /var/log/nginx/example.com.access.log combined buffer=1m flush=5m;
error_log /var/log/nginx/example.com.error.log warn;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ [^/]\.php(/|$) {
fastcgi_pass php:9000;
include fastcgi.conf;
include fastcgi-php.conf;
}
include /etc/nginx/rewrite/general.conf;
include /etc/nginx/rewrite/security.conf;
include /etc/nginx/rewrite/wordpress.conf;
}
Create the corresponding website directory in the wwwroot
directory, for example example.com
.
Place the SSL certificate in the services/openresty/ssl/
directory, with the certificate file named example.com.crt
and the private key file named example.com.key
.
docker exec -it openresty nginx -s reload
Tip: You can refer to the
example.com.conf.example
example file in theservices/openresty/conf.d/
directory to create a new website configuration.
Enter https://example.com
in your browser to test if the website is accessible.
Installing PHP Extensions
Enter the PHP container and use the install-php-extensions
command to quickly install extensions:
docker exec -it php /bin/sh
install-php-extensions smbclient
Tip: For a list of supported extensions, please refer to: docker-php-extension-installer
Enabling PHP Slow Script Logging
Modify the services/php/www.conf
file, find the following two lines and uncomment them:
slowlog = /var/log/php/slowlog.log
request_slowlog_timeout = 3
Note: In production environments, it is recommended to disable slow script logging to improve performance.
Enabling MariaDB Slow Query Logging
Modify the services/mariadb/mariadb.cnf
file, set the following parameters to 1:
slow_query_log=1
log_queries_not_using_indexes=1
Note: In production environments, it is recommended to set these parameters to 0 to improve performance.
MariaDB General Query Log Configuration
Modify the services/mariadb/mariadb.cnf
file, set the following parameter to 1:
general_log=1
Note: In production environments, it is recommended to set these parameters to 0 to improve performance.
Setting Redis Password
Modify the services/redis/redis.conf
file, find the requirepass
parameter and set the password:
requirepass your_strong_password
Note: Please use a strong password and avoid using the default password
foobared
.
We welcome all contributions. You can submit any ideas as Pull requests or as Issues, have a good time!
The project is released under the MIT License, see the LICENSE file for details.