首先有自己的項目倉庫,項目的composr.json配置如下
{ "name": "test/packag1", "description": "私有倉庫創建測試", "type": "library", "license": "proprietary", "minimum-stability": "dev", "require": { "ext-curl": "*", "ext-redis": "*", "ext-fileinfo": "*", "ext-libxml": "*", "ext-simpleXml": "*", "ext-json": "*", "monolog/monolog": "^1.25", }, "keywords": [ "test,library" ], "authors": [ { "name": "joshua317", "email": "joshua317@joshua317.com", "role": "Master" } ], "repositories": { "composer.joshua317": { "type": "composer", "url": "http://composer.joshua317.com" } }, "prefer-stable": true, "config": { "optimize-autoloader": true, "preferred-install": "dist", "sort-packages": true, "secure-http": false, "process-timeout": 120 }, "require-dev": { "phpunit/phpunit": "^9.0" } }
一、使用 Satis 搭建私有倉庫
使用 Composer 自帶的建項目功能,這個相當於git clone+composer install+ 運行 post-install 腳本。
私有倉庫的創建很簡單,直接通過使用下面命令創建就可以
cd /data/wwwroot/
composer create-project composer/satis --stability=dev --keep-vcs --no-secure-http
二、建立satis.json配置文件
上面命令完成后,進入到目錄,進行satis.json文件的創建
{ "name": "My Repository", "homepage": "http://composer.joshua317.com", "repositories": [ { "type": "vcs", "url": "http://gitlab.joshua317.com/test/package1.git" }], "require": { "test/package1": "dev-master" }, "require-all": false, "archive": { "directory": "dist", "format": "tar", "skip-dev": true } }
配置文件詳解
name:倉庫名字
homepage:主頁地址
repositories:包所在地址
require:指定獲取哪些包及對應的版本,獲取所有包使用”require-all”: true,與包中composer.json中的名稱相同,不同會出現問題
archive的配置項:
directory: 必需要的,表示生成的壓縮包存放的目錄,會在build時的目錄中
format: 壓縮包格式, zip(默認)和tar
prefix-url: 下載鏈接的前綴的Url,默認會從homepage中取
skip-dev: 默認為假,是否跳過開發分支
absolute-directory: 絕對目錄
whitelist: 白名單,只下載哪些
blacklist: 黑名單,不下載哪些
checksum: 可選,是否驗證sha1
二、生成倉庫及更新倉庫列表
使用下面命令會生成一個public目錄,里面有生成的所有包的信息,然后把這個目錄設置到可以用域名訪問的地方,比如 composer.joshua317.com
cd /data/wwwwoot/
php bin/satis build satis.json public/
也可以使用下面命令行生成指定包的信息
php bin/satis build satis.json public/ test/test1 test/test2
三、NGINX配置
server { listen 80; server_name composer.joshua317.com; index index.html index.php; root /home/wwwroot/satis/public; access_log /data/log/composer-access.log default_access; error_log /data/log/composer-error.log error; location ~ \.php$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
服務起來后,瀏覽器輸入:http://composer.joshua317.com 進行訪問
四、使用私有倉庫包
在自己項目中的composer.json中添加類似如下內容
{ "require": { "test/package1": "*" } "config": { "secure-http": false } "repositories": { "packagist": { "type": "composer", "url": "http://composer.joshua317.com" } } }