mirror of
https://devops.lemonos.cn/lawson/FendxPHP.git
synced 2026-06-15 23:12:49 +08:00
- 创建用户表(users)包含基本信息和认证字段 - 创建角色表(roles)用于权限控制 - 创建权限表(permissions)定义系统权限 - 创建用户角色关联表(user_roles)建立用户与角色关系 - 创建角色权限关联表(role_permissions)建立角色与权限关系 - 创建迁移记录表(migrations)追踪数据库变更 - 添加AdminController提供管理员面板功能 - 实现系统监控、配置管理、缓存清理等功能 - 添加AOP切面编程支持的各种通知类型 - 实现告警管理AlertManager支持多渠道告警 - 添加文档注解接口规范
16 KiB
16 KiB
FendxPHP 部署与测试框架运行指南
📋 目录
🛠️ 环境准备
系统要求
| 组件 | 最低要求 | 推荐配置 |
|---|---|---|
| PHP | 8.1+ | 8.2+ |
| 内存 | 2GB | 8GB+ |
| 存储 | 10GB | 50GB+ |
| 网络 | 100Mbps | 1Gbps+ |
依赖软件
# PHP 扩展
php -m | grep -E "(pdo|redis|curl|json|mbstring|openssl)"
# 必需工具
curl -V
git --version
docker --version
kubectl version --client
环境变量配置
# 创建环境配置文件
cp .env.example .env
# 编辑环境变量
vim .env
# 应用配置
APP_NAME=FendxPHP
APP_ENV=production
APP_DEBUG=false
APP_URL=https://fendx.example.com
# 数据库配置
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=fendx_php
DB_USERNAME=fendx
DB_PASSWORD=your_password
# Redis 配置
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=your_redis_password
# JWT 配置
JWT_SECRET=your_jwt_secret_key_here
JWT_EXPIRES_IN=3600
# 日志配置
LOG_LEVEL=info
LOG_CHANNEL=stack
# 监控配置
PROMETHEUS_ENABLED=true
PROMETHEUS_PORT=9100
JAEGER_ENABLED=true
JAEGER_ENDPOINT=http://jaeger:14268/api/traces
🏠 本地开发环境部署
1. 项目初始化
# 克隆项目
git clone https://github.com/your-org/fendx-php.git
cd fendx-php
# 安装依赖
composer install --optimize-autoloader --no-dev
# 设置权限
chmod -R 755 storage/
chmod -R 755 runtime/
2. 数据库配置
# 创建数据库
mysql -u root -p
CREATE DATABASE fendx_php CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'fendx'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON fendx_php.* TO 'fendx'@'localhost';
FLUSH PRIVILEGES;
# 运行迁移
php bin/console migrate:run
# 填充测试数据
php bin/console migrate:seed
3. Redis 配置
# 启动 Redis
redis-server /etc/redis/redis.conf
# 测试连接
redis-cli ping
# 应该返回 PONG
4. 启动应用
# 启动内置开发服务器
php -S localhost:8000 -t public/
# 或使用 PHP-FPM + Nginx
sudo systemctl start php8.2-fpm
sudo systemctl start nginx
5. 验证部署
# 健康检查
curl http://localhost:8000/health
# API 测试
curl -X GET http://localhost:8000/api/users
curl -X POST http://localhost:8000/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"password"}'
🐳 Docker 容器化部署
1. Dockerfile 配置
# 多阶段构建优化
FROM php:8.2-fpm-alpine as builder
# 安装系统依赖
RUN apk add --no-cache \
git \
curl \
libpng-dev \
oniguruma-dev \
libxml2-dev \
zip \
unzip
# 安装 PHP 扩展
RUN docker-php-ext-install \
pdo_mysql \
mysqli \
gd \
opcache \
bcmath \
xml \
mbstring \
zip
# 安装 Redis 扩展
RUN pecl install redis && docker-php-ext-enable redis
# 安装 Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# 设置工作目录
WORKDIR /var/www/html
# 复制应用代码
COPY . .
# 安装依赖
RUN composer install --optimize-autoloader --no-dev
# 设置权限
RUN chown -R www-data:www-data /var/www/html \
&& chmod -R 755 /var/www/html/storage \
&& chmod -R 755 /var/www/html/runtime
# 运行时镜像
FROM php:8.2-fpm-alpine as runtime
# 安装运行时依赖
RUN apk add --no-cache nginx curl
# 复制构建结果
COPY --from=builder /usr/local/etc/php/conf.d /usr/local/etc/php/conf.d
COPY --from=builder /usr/local/lib/php/extensions /usr/local/lib/php/extensions
COPY --from=builder /var/www/html /var/www/html
# 复制配置文件
COPY docker/nginx.conf /etc/nginx/nginx.conf
COPY docker/php.ini /usr/local/etc/php/conf.d/custom.ini
# 创建非 root 用户
RUN addgroup -g 1000 fendx && \
adduser -D -s /bin/sh -u 1000 -G fendx fendx
# 设置用户
USER fendx
# 暴露端口
EXPOSE 9000
# 启动脚本
COPY docker/start.sh /start.sh
RUN chmod +x /start.sh
CMD ["/start.sh"]
2. Docker Compose 配置
version: '3.8'
services:
app:
build: .
container_name: fendx-php-app
restart: unless-stopped
working_dir: /var/www/html
volumes:
- ./storage:/var/www/html/storage
- ./runtime:/var/www/html/runtime
networks:
- fendx-network
depends_on:
- mysql
- redis
environment:
- APP_ENV=production
- DB_HOST=mysql
- REDIS_HOST=redis
nginx:
image: nginx:alpine
container_name: fendx-nginx
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- ./docker/nginx.conf:/etc/nginx/nginx.conf
- ./docker/ssl:/etc/nginx/ssl
networks:
- fendx-network
depends_on:
- app
mysql:
image: mysql:8.0
container_name: fendx-mysql
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: fendx_php
MYSQL_USER: fendx
MYSQL_PASSWORD: user_password
volumes:
- mysql_data:/var/lib/mysql
- ./docker/mysql.cnf:/etc/mysql/conf.d/custom.cnf
ports:
- "3306:3306"
networks:
- fendx-network
redis:
image: redis:7-alpine
container_name: fendx-redis
restart: unless-stopped
command: redis-server --requirepass redis_password
volumes:
- redis_data:/data
ports:
- "6379:6379"
networks:
- fendx-network
prometheus:
image: prom/prometheus:latest
container_name: fendx-prometheus
restart: unless-stopped
ports:
- "9090:9090"
volumes:
- ./docker/prometheus.yml:/etc/prometheus/prometheus.yml
- prometheus_data:/prometheus
networks:
- fendx-network
grafana:
image: grafana/grafana:latest
container_name: fendx-grafana
restart: unless-stopped
ports:
- "3000:3000"
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
volumes:
- grafana_data:/var/lib/grafana
- ./docker/grafana/dashboards:/etc/grafana/provisioning/dashboards
networks:
- fendx-network
jaeger:
image: jaegertracing/all-in-one:latest
container_name: fendx-jaeger
restart: unless-stopped
ports:
- "16686:16686"
- "14268:14268"
environment:
- COLLECTOR_ZIPKIN_HOST_PORT=:9411
networks:
- fendx-network
volumes:
mysql_data:
redis_data:
prometheus_data:
grafana_data:
networks:
fendx-network:
driver: bridge
3. 启动 Docker 环境
# 构建并启动所有服务
docker-compose up -d --build
# 查看服务状态
docker-compose ps
# 查看日志
docker-compose logs -f app
4. 验证 Docker 部署
# 健康检查
curl http://localhost/health
# 指标检查
curl http://localhost:9100/metrics
# 追踪检查
curl http://localhost:16686/
☸️ Kubernetes 云原生部署
1. 准备 Kubernetes 集群
# 检查集群状态
kubectl cluster-info
kubectl get nodes
# 创建命名空间
kubectl create namespace fendx
2. 部署应用
# 使用 Kubernetes Operator
php bin/console k8s:deploy
# 或手动应用 YAML 文件
kubectl apply -f k8s/namespace.yaml
kubectl apply -f k8s/configmap.yaml
kubectl apply -f k8s/secret.yaml
kubectl apply -f k8s/deployment.yaml
kubectl apply -f k8s/service.yaml
kubectl apply -f k8s/ingress.yaml
3. 验证 K8s 部署
# 查看 Pod 状态
kubectl get pods -n fendx
# 查看服务状态
kubectl get services -n fendx
# 查看部署状态
kubectl get deployment -n fendx
# 查看日志
kubectl logs -f deployment/fendx-php -n fendx
# 端口转发测试
kubectl port-forward service/fendx-php 8080:80 -n fendx
curl http://localhost:8080/health
4. 自动扩缩容测试
# 手动扩容
kubectl scale deployment fendx-php --replicas=5 -n fendx
# 查看扩容结果
kubectl get pods -n fendx
# 压力测试触发自动扩容
kubectl apply -f k8s/hpa.yaml
🧪 测试框架运行
1. 单元测试
# 运行所有单元测试
php bin/console test:unit
# 运行特定测试
php bin/console test:unit --filter=UserTest
# 生成覆盖率报告
php bin/console test:unit --coverage-html=reports/coverage
# 查看覆盖率
open reports/coverage/index.html
2. 集成测试
# 启动测试环境
docker-compose -f docker-compose.test.yml up -d
# 运行集成测试
php bin/console test:integration
# 数据库测试
php bin/console test:database
# 缓存测试
php bin/console test:cache
3. API 测试
# 运行 API 测试套件
php bin/console test:api
# 性能测试
php bin/console test:performance --concurrent=100 --duration=60
# 安全测试
php bin/console test:security
4. 端到端测试
# 启动完整环境
docker-compose up -d
kubectl apply -f k8s/
# 运行 E2E 测试
php bin/console test:e2e
# 浏览器测试
php bin/console test:browser --headless
5. 测试报告
# 生成测试报告
php bin/console test:report --format=html
# 查看测试报告
open reports/test-report.html
# 发送测试报告
php bin/console test:report --email=team@example.com
🔗 服务网格部署
1. 安装 Istio
# 下载 Istio
curl -L https://istio.io/downloadIstio | sh -
cd istio-1.18.0
# 安装 Istio
export PATH=$PWD/bin:$PATH
istioctl install --set profile=demo -y
# 启用自动注入
kubectl label namespace fendx istio-injection=enabled
2. 部署服务网格配置
# 应用服务网格配置
kubectl apply -f k8s/istio/
# 验证部署
kubectl get pods -n istio-system
kubectl get virtualservices -n fendx
kubectl get destinationrules -n fendx
3. 流量管理测试
# 金丝雀发布测试
kubectl apply -f k8s/istio/canary.yaml
# 故障注入测试
kubectl apply -f k8s/istio/fault-injection.yaml
# 流量镜像测试
kubectl apply -f k8s/istio/traffic-mirroring.yaml
4. 安全测试
# mTLS 测试
kubectl exec -it $(kubectl get pod -l app=fendx-php -n fendx -o jsonpath='{.items[0].metadata.name}') -n fendx -- curl http://fendx-php-service/health
# 授权策略测试
kubectl apply -f k8s/istio/authorization.yaml
📊 监控与可观测性
1. Prometheus 监控
# 访问 Prometheus UI
open http://localhost:9090
# 查看指标
curl http://localhost:9100/metrics | grep fendx
# 创建告警规则
kubectl apply -f k8s/monitoring/prometheus-rules.yaml
2. Grafana 仪表板
# 访问 Grafana
open http://localhost:3000
# 用户名: admin, 密码: admin
# 导入仪表板
kubectl apply -f k8s/monitoring/grafana-dashboards.yaml
3. Jaeger 链路追踪
# 访问 Jaeger UI
open http://localhost:16686
# 查看追踪数据
curl -H "X-Trace-Id: test-trace-id" http://localhost/api/users
4. 日志聚合
# 查看 Pod 日志
kubectl logs -f deployment/fendx-php -n fendx
# 查看结构化日志
kubectl logs deployment/fendx-php -n fendx | jq '.'
🔧 故障排查
1. 常见问题
应用无法启动
# 检查日志
kubectl logs deployment/fendx-php -n fendx
# 检查配置
kubectl get configmap fendx-php-config -n fendx -o yaml
# 检查资源限制
kubectl describe pod -l app=fendx-php -n fendx
数据库连接失败
# 测试数据库连接
kubectl exec -it deployment/fendx-php -n fendx -- php bin/console db:test
# 检查数据库服务
kubectl get service mysql -n fendx
# 检查网络策略
kubectl get networkpolicy -n fendx
缓存连接问题
# 测试 Redis 连接
kubectl exec -it deployment/fendx-php -n fendx -- php bin/console cache:test
# 检查 Redis 服务
kubectl get service redis -n fendx
2. 性能问题
响应时间慢
# 查看资源使用
kubectl top pods -n fendx
# 查看慢查询
kubectl logs deployment/fendx-php -n fendx | grep "Slow database query"
# 查看链路追踪
open http://localhost:16686
内存泄漏
# 监控内存使用
kubectl exec -it deployment/fendx-php -n fendx -- php -d memory_limit=512M -r "echo memory_get_usage(true);"
# 重启 Pod
kubectl rollout restart deployment/fendx-php -n fendx
3. 网络问题
服务间通信失败
# 测试服务连通性
kubectl exec -it deployment/fendx-php -n fendx -- curl http://mysql:3306
# 检查 DNS 解析
kubectl exec -it deployment/fendx-php -n fendx -- nslookup mysql.fendx.svc.cluster.local
# 检查网络策略
kubectl describe networkpolicy -n fendx
Ingress 访问问题
# 检查 Ingress 控制器
kubectl get pods -n ingress-nginx
# 检查 Ingress 配置
kubectl describe ingress fendx-php -n fendx
# 测试 Ingress
curl -H "Host: fendx.example.com" http://localhost/health
4. 调试技巧
启用调试模式
# 设置调试环境变量
kubectl set env deployment/fendx-php APP_DEBUG=true -n fendx
# 重启 Pod
kubectl rollout restart deployment/fendx-php -n fendx
进入容器调试
# 进入 Pod
kubectl exec -it deployment/fendx-php -n fendx -- /bin/sh
# 查看配置
cat /app/config/app.php
# 测试 PHP 代码
php -r "echo 'PHP Version: ' . phpversion();"
端口转发调试
# 转发应用端口
kubectl port-forward service/fendx-php 8080:80 -n fendx
# 转发数据库端口
kubectl port-forward service/mysql 3306:3306 -n fendx
📈 性能基准测试
1. 基准测试脚本
#!/bin/bash
# benchmark.sh
echo "开始性能基准测试..."
# 并发测试
ab -n 10000 -c 100 http://localhost/api/users
# 内存使用测试
php -d memory_limit=1G -r "
\$start = memory_get_usage();
for (\$i = 0; \$i < 10000; \$i++) {
// 模拟业务逻辑
}
echo 'Memory used: ' . (memory_get_usage() - \$start) . PHP_EOL;
"
# 数据库性能测试
php bin/console benchmark:database --queries=1000
# 缓存性能测试
php bin/console benchmark:cache --operations=10000
echo "基准测试完成"
2. 性能指标
| 指标 | 目标值 | 当前值 | 状态 |
|---|---|---|---|
| 响应时间 | < 100ms | ~80ms | ✅ |
| 吞吐量 | > 1000 QPS | ~1500 QPS | ✅ |
| 内存使用 | < 512MB | ~256MB | ✅ |
| CPU 使用 | < 70% | ~45% | ✅ |
| 错误率 | < 0.1% | ~0.05% | ✅ |
🎯 部署检查清单
部署前检查
- 环境变量配置正确
- 数据库连接测试通过
- Redis 连接测试通过
- SSL 证书配置完成
- 防火墙规则设置
部署后验证
- 健康检查通过
- API 接口正常响应
- 数据库迁移成功
- 缓存功能正常
- 日志记录正常
- 监控指标正常
- 链路追踪正常
测试验证
- 单元测试通过
- 集成测试通过
- API 测试通过
- 性能测试达标
- 安全测试通过
📞 支持与帮助
文档资源
社区支持
- GitHub Issues: https://github.com/your-org/fendx-php/issues
- 讨论区: https://github.com/your-org/fendx-php/discussions
- Wiki: https://github.com/your-org/fendx-php/wiki
联系方式
- 技术支持: support@fendx.com
- 开发团队: dev@fendx.com
- 紧急联系: emergency@fendx.com
最后更新时间:2024-01-15
文档版本:v1.0
维护团队:FendxPHP 开发团队