Skip to content

部署 NuGet 私有服务器

NuGet 是.NET 生态系统的包管理器,部署私有 NuGet 服务器可以用于企业内部组件共享和管理。以下是几种在 Windows 上部署 NuGet 私有服务器的方法:

方法一:使用 BaGet(轻量级,推荐)

BaGet 是一个开源的、跨平台的 NuGet 服务器实现,易于部署。

安装步骤:

  1. 下载 BaGet
cmd
dotnet tool install --global BaGet
  1. 配置 BaGet
  • 创建配置文件 baget.appsettings.json
json
{
  "Database": {
    "Type": "Sqlite",
    "ConnectionString": "Data Source=baget.db"
  },
  "Storage": {
    "Type": "FileSystem",
    "Path": "C:\\NuGetPackages"
  },
  "Search": {
    "Type": "Database"
  },
  "Mirror": {
    "Enabled": false
  },
  "PackageDeletion": {
    "Enabled": true
  }
}
  1. 运行 BaGet
cmd
baget --config baget.appsettings.json

默认运行在 http://localhost:5000

  1. 配置为 Windows 服务(可选)
  • 使用 NSSM(Non-Sucking Service Manager):
cmd
nssm install BaGet "C:\path\to\baget.exe" --config "C:\path\to\baget.appsettings.json"
nssm start BaGet

方法二:使用 NuGet.Server(官方简单方案)

NuGet.Server 是微软提供的简单 NuGet 服务器实现。

安装步骤:

  1. 创建新项目
cmd
dotnet new web -n MyNuGetServer
cd MyNuGetServer
  1. 添加 NuGet.Server 包
cmd
dotnet add package NuGet.Server
  1. 修改 Program.cs
csharp
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddNuGetServer();
var app = builder.Build();
app.UseNuGetServer();
app.Run();
  1. 配置 appsettings.json
json
{
  "NuGetServer": {
    "PackagePath": "C:\\NuGetPackages",
    "ApiKey": "MySecretKey"
  }
}
  1. 发布并运行
cmd
dotnet publish -c Release -o C:\NuGetServer
cd C:\NuGetServer
dotnet MyNuGetServer.dll

方法三:使用 ProGet(企业级)

ProGet 是一个功能丰富的商业 NuGet 服务器,提供免费版。

安装步骤:

  1. 下载 ProGet
  1. 运行安装向导
  • 按向导步骤完成安装
  • 配置 SQL Server 数据库(支持 SQLite、SQL Server)
  1. 访问管理界面
  • 默认地址 http://localhost:8624
  • 初始管理员账号:Admin/Admin

方法四:使用 Nexus Repository OSS

Sonatype Nexus 也支持 NuGet 仓库管理。

安装步骤:

  1. 下载 Nexus
  1. 安装并运行
  • 解压后运行nexus.exe /run
  • 默认访问地址 http://localhost:8081
  1. 配置 NuGet 仓库
  • 创建 NuGet 代理仓库(连接 nuget.org)
  • 创建 NuGet 宿主仓库(存储私有包)
  • 创建 NuGet 组仓库(合并多个仓库)

配置客户端使用私有服务器

  1. 添加私有源
cmd
nuget sources Add -Name "MyPrivateServer" -Source "http://localhost:5000/v3/index.json"
  1. 推送包到服务器
cmd
nuget push MyPackage.1.0.0.nupkg -Source "MyPrivateServer" -ApiKey MySecretKey
  1. 从私有源安装包
cmd
dotnet add package MyPackage --source "http://localhost:5000/v3/index.json"

安全配置建议

  1. 启用 HTTPS
  • 使用 IIS 或反向代理(如 Nginx)配置 HTTPS
  • 或使用 Kestrel 配置 HTTPS 证书
  1. 访问控制
  • BaGet:通过 API 密钥控制上传
  • NuGet.Server:配置 IIS 身份验证
  • ProGet/Nexus:内置完善的用户权限系统
  1. 备份策略
  • 定期备份包存储目录和数据库

性能优化

  1. 存储位置
  • 将包存储放在快速磁盘上(如 SSD)
  1. 数据库选择
  • 小规模使用 SQLite
  • 大规模使用 SQL Server 或 PostgreSQL
  1. 缓存配置
  • 配置反向代理缓存(如 Nginx)

对于小型团队或个人使用,BaGet 是最简单轻量的选择;对于企业级需求,ProGet 或 Nexus 提供更全面的功能和管理界面。