Composer Satis 私有包仓库搭建与使用指南
什么是 Composer Satis
Composer Satis 是一个轻量级的静态 Composer 仓库生成器,它允许开发者搭建自己的私有 PHP 包仓库。与完整的 Packagist 不同,Satis 不需要数据库,只需简单的 JSON 配置即可生成静态的包索引文件,非常适合企业内部或项目组共享私有 PHP 组件。
基础配置
创建配置文件
首先需要创建一个 JSON 格式的配置文件(通常命名为 satis.json
),其中包含以下基本元素:
{
"name": "企业私有仓库",
"homepage": "http://packages.example.org",
"repositories": [
{ "type": "vcs", "url": "https://git.example.org/mycompany/privaterepo" },
{ "type": "vcs", "url": "http://svn.example.org/private/repo" }
],
"require-all": true
}
name
: 仓库名称homepage
: 仓库最终部署的 URL 地址repositories
: 包含所有 VCS 仓库地址require-all
: 自动包含所有版本
选择性包含包
如果不需要包含所有包,可以指定具体包名和版本约束:
{
"repositories": [
{ "type": "vcs", "url": "https://git.example.org/mycompany/privaterepo" }
],
"require": {
"company/package": "*",
"company/package2": "2.0.*"
}
}
构建仓库
配置完成后,运行以下命令构建仓库:
php bin/satis build satis.json web/
这个命令会:
- 扫描配置中的所有仓库
- 收集包信息
- 生成静态的
packages.json
文件 - 创建 web 可访问的目录结构
建议将此命令设置为定时任务(如 cron job),以保持仓库信息最新。
高级配置选项
部分更新
当仓库包含大量包时,可以只更新特定包:
php bin/satis build satis.json web/ package1 package2
或者只更新特定仓库:
php bin/satis build --repository-url https://git.example.org/repo.git satis.json web/
下载优化
为加速安装,可以配置本地存档:
{
"archive": {
"directory": "dist",
"format": "tar",
"prefix-url": "https://cdn.example.org",
"skip-dev": true
}
}
directory
: 存档文件存放目录format
: 存档格式(zip 或 tar)prefix-url
: CDN 地址skip-dev
: 是否跳过开发分支
依赖解析
自动解析并包含所有依赖:
{
"require-dependencies": true,
"require-dev-dependencies": false
}
废弃包标记
标记不再维护的包:
{
"abandoned": {
"company/old-package": true,
"company/legacy-package": "company/new-package"
}
}
客户端使用配置
在项目中使用私有仓库:
{
"repositories": [
{ "type": "composer", "url": "http://packages.example.org" }
],
"require": {
"company/package": "1.0.0"
}
}
安全配置
SSH 认证
{
"repositories": [
{
"type": "composer",
"url": "ssh2.sftp://example.org",
"options": {
"ssh2": {
"username": "composer",
"pubkey_file": "/path/to/id_rsa.pub",
"privkey_file": "/path/to/id_rsa"
}
}
}
]
}
HTTPS 客户端证书
{
"repositories": [
{
"type": "composer",
"url": "https://example.org",
"options": {
"ssl": {
"local_cert": "/path/to/cert.pem"
}
}
}
]
}
HTTP 头认证
{
"repositories": [
{
"type": "composer",
"url": "https://example.org",
"options": {
"http": {
"header": ["API-TOKEN: YOUR-TOKEN"]
}
}
}
]
}
最佳实践
- 自动化构建:设置定时任务自动更新仓库
- CDN 加速:使用
prefix-url
指向 CDN 提升下载速度 - 最小化权限:为仓库访问配置最小必要权限
- 版本控制:将
satis.json
纳入版本控制 - 监控:监控构建过程确保仓库更新正常
通过合理配置 Composer Satis,企业可以高效管理私有 PHP 组件,同时享受 Composer 生态系统的便利性。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考