kpirnie/infra-build
GitHub: kpirnie/infra-build
基于 Alpine Linux 从源码构建的、经过安全加固的 Nginx 与 PHP-FPM 生产级容器镜像,通过 GitHub Actions 每日自动构建并发布至 GHCR。
Stars: 0 | Forks: 0
# Infra-Build
[](https://github.com/kpirnie/infra-build/actions/workflows/nginx.yml)
[](https://github.com/kpirnie/infra-build/issues)
[](LICENSE)
[](https://kevinpirnie.com/)
生产级、经过安全加固的 Docker/Podman 镜像,基于 Alpine Linux 从源码构建。通过 GitHub Actions 每晚自动重构并发布至 GHCR。
## 镜像
### nginx
```
ghcr.io/kpirnie/nginx:latest
ghcr.io/kpirnie/nginx:latest-YYYY-MM-DD
```
**构建详情:**
| 特性 | 详情 |
|---|---|
| 基础镜像 | `alpine:latest` |
| nginx | Mainline 版本,从源码编译 |
| TLS | [OpenSSL 4.x](https://github.com/openssl/openssl) — 原生 QUIC 支持,静态链接 |
| HTTP/3 | 通过 `--with-http_v3_module` 实现 QUIC |
| HTTP/2 | `--with-http_v2_module` |
| 压缩 | Brotli (`ngx_brotli`)、Zstd (`zstd-nginx-module`)、gzip (内置) |
| GeoIP | GeoIP2 (`ngx_http_geoip2_module`) — 必须以卷挂载方式挂载 MaxMind 数据库 |
| Headers | `headers-more-nginx-module` |
| 脚本支持 | NJS (`njs`) |
| 图像处理 | `--with-http_image_filter_module` |
| 流代理 | `--with-stream` + SSL + realip + ssl_preread |
| 架构 | `linux/amd64`、`linux/arm64` |
| 运行身份 | root (主进程) → `nginx` UID 101 (工作进程) |
### PHP-FPM
```
ghcr.io/kpirnie/php:8.2-latest
ghcr.io/kpirnie/php:8.2-latest-YYYY-MM-DD
ghcr.io/kpirnie/php:8.3-latest
ghcr.io/kpirnie/php:8.4-latest
ghcr.io/kpirnie/php:8.5-latest
```
**构建详情:**
| 特性 | 详情 |
|---|---|
| 基础镜像 | `php:8.x-fpm-alpine` |
| 版本 | 8.2 · 8.3 · 8.4 · 8.5 |
| 架构 | `linux/amd64`、`linux/arm64` |
| 运行身份 | root (主进程) → `www-data` UID 82 (工作进程) |
**扩展:**
`apcu` `bcmath` `calendar` `exif` `gd` `gettext` `igbinary` `imagick` `intl` `msgpack` `mysqli` `opcache` `pcntl` `pdo_mysql` `pdo_pgsql` `pgsql` `redis` `sodium` `sockets` `tidy` `uuid` `xsl` `yaml` `zip`
外加官方 PHP Alpine 镜像中内置的所有默认扩展:`curl` `dom` `fileinfo` `iconv` `mbstring` `openssl` `pdo` `phar` `simplexml` `tokenizer` `xml` `xmlreader` `xmlwriter` `zlib` 等。
`redis` 编译时包含了 `igbinary` 和 `msgpack` 序列化器支持。
**工具:** WP-CLI · Composer
## 配置
两个镜像均提供了最小化的基础配置。其他所有内容均需通过卷挂载提供。
### nginx
| 挂载点 | 用途 |
|---|---|
| `/etc/nginx/conf.d/` | HTTP 站点配置 (`*.conf`) |
| `/etc/nginx/stream.d/` | 流代理配置 (`*.conf`) |
| `/etc/nginx/sites-enabled/` | 备用站点配置路径 |
| `/var/log/nginx/` | 访问和错误日志 |
| `/path/to/GeoIP2/` | MaxMind `.mmdb` 数据库文件 — 将 `geoip2` 指令指向您的挂载路径 |
### PHP-FPM
| 挂载点 | 用途 |
|---|---|
| `/usr/local/etc/php/conf.d/` | `php.ini` 覆盖片段 (`*.ini`) |
| `/usr/local/etc/php-fpm.d/` | FPM 池覆盖配置 |
| `/var/log/php-fpm/` | FPM 日志 |
## 用法
### Podman
```
# nginx
podman pull ghcr.io/kpirnie/nginx:latest
# PHP 8.4
podman pull ghcr.io/kpirnie/php:8.4-latest
```
### Compose 示例 (Podman / Docker)
```
services:
nginx:
image: ghcr.io/kpirnie/nginx:latest
ports:
- "80:80"
- "443:443"
- "443:443/udp" # QUIC/HTTP3
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d:ro
- ./nginx/ssl:/etc/nginx/ssl:ro
- ./geoip:/etc/nginx/geoip:ro
- nginx_logs:/var/log/nginx
php:
image: ghcr.io/kpirnie/php:8.4-latest
volumes:
- ./app:/var/www/html:ro
- ./php/conf.d:/usr/local/etc/php/conf.d:ro
- php_logs:/var/log/php-fpm
volumes:
nginx_logs:
php_logs:
```
## 本地构建
需要包含 Buildah 的 Podman。仅构建原生平台架构(无模拟)。
```
chmod +x build-local.sh
./build-local.sh
```
该脚本会构建所有镜像,对每个镜像运行冒烟测试,并输出摘要。本地镜像将被打上 `:local` 标签,且不会推送到任何地方。
**测试完成后清理本地镜像:**
```
podman images | grep ':local' | awk '{print $3}' | xargs podman rmi
```
## 每日构建
这两个镜像均会在 **UTC 时间每天 02:00** 通过 GitHub Actions 自动重构。如果推送到 `main` 分支的代码涉及到了相关镜像目录,也会触发构建。
PHP 工作流会并行构建所有四个版本。如果其中一个版本构建失败(例如 PECL 扩展尚未兼容新的 PHP 版本),其他版本仍会正常完成构建。
## GeoIP2 数据库
nginx 镜像包含 GeoIP2 模块,但**不捆绑 MaxMind 数据库** — MaxMind 需要免费的许可证密钥,且数据库必须独立保持更新。
1. 在 [maxmind.com](https://www.maxmind.com) 注册
2. 下载 `GeoLite2-City.mmdb` 和/或 `GeoLite2-Country.mmdb`
3. 将它们挂载到容器中,并在您的 nginx 配置中引用该路径:
```
geoip2 /etc/nginx/geoip/GeoLite2-Country.mmdb {
$geoip2_country_code country iso_code;
}
```
## 安全说明
- 两个镜像均采用多阶段构建;最终镜像中不存在任何构建工具链。
- nginx 基于 OpenSSL 4.x 编译(静态链接,原生支持 QUIC)— 不存在 OpenSSL 运行时依赖。
- 最终镜像文件系统中所有的 setuid/setgid 位均已移除。
- nginx 工作进程和 PHP-FPM 工作进程均以非特权用户身份运行(`nginx` UID 101,`www-data` UID 82)。
- nginx 中全局设置了 `server_tokens off`。
- `php.ini` 中设置了 `expose_php = Off`。
- nginx 中已禁用 SSI 和 empty_gif 模块。
## 许可证
MIT — 详见 [LICENSE](./LICENSE)
标签:安全测试工具, 请求拦截