feruz/yii2-starter-v1

Yii 2 Advanced Project Starter kids

Installs: 1

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Type:project

pkg:composer/feruz/yii2-starter-v1

v0.0.1 2025-10-03 17:58 UTC

This package is auto-updated.

Last update: 2025-12-03 18:30:16 UTC


README

Complete Yii2 Advanced Template starter kit with Docker support, including API protection, monitoring, and microservices architecture.

๐Ÿš€ Features

  • Yii2 Advanced Template with API application
  • Docker-based development and production environment
  • PHP 8.3 with PHP-FPM
  • Nginx web server with optimized configuration
  • PostgreSQL 15 database
  • Redis 7 for caching and sessions
  • RabbitMQ 3.12 message broker
  • Supervisor for background workers
  • API Protection:
    • Priority-based rate limiting (Token Bucket)
    • Adaptive throttling (CPU/Memory based)
    • Circuit breaker pattern
    • Load shedding
  • Monitoring:
    • PHP-FPM metrics
    • Application metrics
    • Alert system (Telegram, Email, Slack)
    • Health checks
  • Outbox Pattern for reliable event publishing
  • Prometheus & Grafana support (optional)

๐Ÿ“‹ Prerequisites

  • Docker 20.10+
  • Docker Compose 2.0+
  • Make (optional, for convenience commands)

๐Ÿ› ๏ธ Quick Start

1. Clone and Initialize

# Clone the repository
git clone <repository-url>
cd yii2-advanced-docker

# Copy environment file
cp .env.example .env

# Edit .env file with your configuration
nano .env

# Initialize and start services
make init
make up

Or without Make:

cp .env.example .env
docker-compose build
docker-compose up -d
docker-compose exec php-fpm php yii migrate --interactive=0

2. Access Services

3. Health Check

curl http://localhost:8082/health

๐Ÿ“ Project Structure

yii2-advanced-docker/
โ”œโ”€โ”€ api/                    # API application
โ”‚   โ”œโ”€โ”€ config/            # API configuration
โ”‚   โ”œโ”€โ”€ controllers/       # API controllers
โ”‚   โ””โ”€โ”€ web/               # Web root
โ”œโ”€โ”€ common/                # Shared components
โ”‚   โ”œโ”€โ”€ components/        # Protection components
โ”‚   โ”œโ”€โ”€ behaviors/         # Behaviors
โ”‚   โ”œโ”€โ”€ models/           # Models
โ”‚   โ””โ”€โ”€ config/           # Common config
โ”œโ”€โ”€ console/              # Console commands
โ”‚   โ””โ”€โ”€ controllers/      # Workers
โ”œโ”€โ”€ docker/               # Docker configuration
โ”‚   โ”œโ”€โ”€ php-fpm/         # PHP-FPM config
โ”‚   โ”œโ”€โ”€ nginx/           # Nginx config
โ”‚   โ”œโ”€โ”€ postgresql/      # PostgreSQL config
โ”‚   โ”œโ”€โ”€ redis/           # Redis config
โ”‚   โ””โ”€โ”€ rabbitmq/        # RabbitMQ config
โ”œโ”€โ”€ migrations/           # Database migrations
โ”œโ”€โ”€ scripts/             # Utility scripts
โ”œโ”€โ”€ docker-compose.yml   # Main compose file
โ”œโ”€โ”€ Makefile            # Convenience commands
โ””โ”€โ”€ README.md

๐Ÿ”ง Configuration

Environment Variables

Key environment variables (see .env.example for full list):

# Database
DB_DSN=pgsql:host=pgsql;dbname=yii2advanced
DB_USERNAME=yii2
DB_PASSWORD=secret

# Redis
REDIS_HOST=redis
REDIS_PORT=6379

# RabbitMQ
RABBITMQ_HOST=rabbitmq
RABBITMQ_USER=guest
RABBITMQ_PASSWORD=guest

# Rate Limiting
RATE_LIMIT_USER_CAPACITY=100
RATE_LIMIT_USER_REFILL_RATE=10

# Throttling
THROTTLE_CPU_THRESHOLD=70
THROTTLE_MEMORY_THRESHOLD=80

# Alerts
ALERT_TELEGRAM_TOKEN=your_token
ALERT_TELEGRAM_CHAT_ID=your_chat_id
ALERT_EMAILS=admin@example.com

Alert Configuration

Edit common/config/alerts.yml to configure alert rules:

alerts:
  fpm_warning:
    severity: warning
    channels:
      - log
      - telegram
    throttle: 300  # seconds

๐Ÿƒ Running Workers

Workers are automatically started by Supervisor in the PHP-FPM container:

  • Outbox Processor (2 instances) - Publishes events to RabbitMQ
  • FPM Monitor - Monitors PHP-FPM health
  • Metrics Collector - Collects application metrics
  • Alert Checker - Checks alert conditions

View worker logs:

make logs-workers
# or
docker-compose exec php-fpm tail -f /var/log/supervisor/*.log

๐Ÿ“Š Monitoring

View Metrics

curl http://localhost/api/monitoring/metrics

View FPM Status

curl http://localhost/api/monitoring/fpm-status

Alert History

docker-compose exec php-fpm php yii alert/history

๐Ÿงช Testing

Create Test Order

curl -X POST http://localhost:8082/order/create \
  -H "Content-Type: application/json" \
  -d '{
    "user_id": 1,
    "total_amount": 99.99,
    "items": [
      {"id": 1, "name": "Product 1", "price": 49.99, "quantity": 1},
      {"id": 2, "name": "Product 2", "price": 50.00, "quantity": 1}
    ]
  }'

List Orders

curl http://localhost:8082/order/index

Test Rate Limiting

for i in {1..100}; do curl http://localhost:8082/order/index; done

๐Ÿณ Docker Commands

Using Makefile

make help              # Show available commands
make up                # Start all services
make down              # Stop all services
make restart           # Restart all services
make logs              # Tail all logs
make shell             # Shell into PHP container
make migrate           # Run migrations
make composer-install  # Install dependencies
make monitoring        # Start with monitoring stack

Using Docker Compose Directly

docker-compose up -d              # Start services
docker-compose down               # Stop services
docker-compose ps                 # List services
docker-compose logs -f            # Tail logs
docker-compose exec php-fpm sh    # Shell into PHP

๐Ÿ”จ Development

Run Migrations

make migrate
# or
docker-compose exec php-fpm php yii migrate

Create Migration

make migrate-create name=create_users_table

Install Dependencies

make composer-install
# or
docker-compose exec php-fpm composer install

Access Database

make shell-db
# or
docker-compose exec pgsql psql -U yii2 -d yii2advanced

๐Ÿ“ˆ Production Deployment

1. Update Environment

cp .env.example .env.production
# Edit .env.production with production settings

2. Build Production Images

docker-compose -f docker-compose.yml -f docker-compose.prod.yml build

3. Deploy

docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d

4. Security Checklist

  • Change all default passwords
  • Configure HTTPS/SSL certificates
  • Set proper file permissions
  • Configure firewall rules
  • Enable security headers in Nginx
  • Set up log rotation
  • Configure backup strategy
  • Set up monitoring alerts

๐Ÿ” Troubleshooting

Services Won't Start

# Check logs
docker-compose logs

# Rebuild containers
docker-compose down
docker-compose build --no-cache
docker-compose up -d

Database Connection Issues

# Check database is running
docker-compose ps pgsql

# Test connection
docker-compose exec php-fpm php yii migrate --interactive=0

Permission Issues

# Fix permissions
docker-compose exec php-fpm chown -R www:www /var/www

Worker Not Running

# Check supervisor status
docker-compose exec php-fpm supervisorctl status

# Restart workers
docker-compose exec php-fpm supervisorctl restart all

๐Ÿ“š Documentation

๐Ÿค Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“ License

This project is licensed under the BSD-3-Clause License.

๐Ÿ™ Acknowledgments

  • Yii2 Framework Team
  • Docker Community
  • All contributors

Built with โค๏ธ using Yii2 and Docker