IIS 部署
本文档假设您更喜欢使用 Blazor WebAssembly 作为 UI 框架,并使用 MongoDB 作为数据库提供程序。如需其他选项,请更改本文档顶部的偏好设置。
先决条件
- 一台已准备好用于部署的 IIS 服务器。
- 安装 hosting bundle。
- MongoDB 数据库必须已准备就绪,可供您的项目使用。
- 如果您想在本地环境中发布,本指南将使用 mkcert 创建自签名证书。请按照 安装指南 安装 mkcert。
生成身份验证证书
如果您正在使用 OpenIddict,则需要生成一个身份验证证书。您可以在 HttpApi.Host 文件夹中执行此命令。
dotnet dev-certs https -v -ep authserver.pfx -p 00000000-0000-0000-0000-000000000000
00000000-0000-0000-0000-000000000000是证书的密码,您可以将其更改为您想要的任何密码。
创建发布文件
您可以在项目根文件夹中执行此命令。
dotnet publish ./src/Volo.Sample.DbMigrator/Volo.Sample.DbMigrator.csproj -c Release -o ./publish/dbmigrator # 替换为您的项目名称
dotnet publish ./src/Volo.Sample.Blazor/Volo.Sample.Blazor.csproj -c Release -o ./publish/blazor # 替换为您的项目名称
dotnet publish ./src/Volo.Sample.HttpApi.Host/Volo.Sample.HttpApi.Host.csproj -c Release -o ./publish/apihost # 替换为您的项目名称
使用您的自定义设置运行 DbMigrator
使用您的域名更新连接字符串和 OpenIddict 部分。运行 DbMigrator 应用程序。
例如,在一个分层式 MVC 项目中。
{
"ConnectionStrings": {
"Default": "Server=volo.sample;Database=Sample;User Id=sa;Password=1q2w3E**;TrustServerCertificate=true"
},
"Redis": {
"Configuration": "volo.sample"
},
"OpenIddict": {
"Applications": {
"Sample_Web": {
"ClientId": "Sample_Web",
"ClientSecret": "1q2w3e*",
"RootUrl": "https://web.sample"
},
"Sample_Swagger": {
"ClientId": "Sample_Swagger",
"RootUrl": "https://api.sample"
}
}
}
}
准备本地部署
如果您打算在具有真实域名的服务器上部署,可以跳过此部分。
使用 mkcert 创建自签名证书
您可以在命令提示符中执行此命令。
cd Desktop # 或其他路径
mkcert -pkcs12 auth.sample api.sample web.sample # 替换为您的域名
将创建的文件扩展名重命名为 ".pfx"
将证书导入 IIS
将域名添加到 hosts 文件
将域名添加到 hosts 文件(在 Windows 中:C:\Windows\System32\drivers\etc\hosts,在 Linux 和 macOS 中:/etc/hosts)。
例如,在一个分层式 MVC 项目中。
127.0.0.1 auth.sample
127.0.0.1 api.sample
127.0.0.1 web.sample
在 IIS 上发布应用程序
更新 appsettings
根据您的项目类型和域名更新 appsettings。
例如,在一个分层式 MVC 项目中。
//AuthServer
{
"App": {
"SelfUrl": "https://auth.sample",
"CorsOrigins": "https://api.sample,https://web.sample",
"RedirectAllowedUrls": "https://api.sample,https://web.sample",
"DisablePII": "false"
},
"ConnectionStrings": {
"Default": "Server=volo.sample;Database=Sample;User Id=sa;Password=1q2w3E**;TrustServerCertificate=true"
},
"AuthServer": {
"Authority": "https://auth.sample",
"RequireHttpsMetadata": "true"
},
"StringEncryption": {
"DefaultPassPhrase": "f9uRkTLdtAZLmlh3"
},
"Redis": {
"Configuration": "volo.sample"
}
}
//HttpApi.Host
{
"App": {
"SelfUrl": "https://api.sample",
"CorsOrigins": "https://web.sample",
"DisablePII": "false",
"HealthCheckUrl": "/health-status"
},
"ConnectionStrings": {
"Default": "Server=volo.sample;Database=Sample;User Id=sa;Password=1q2w3E**;TrustServerCertificate=true"
},
"Redis": {
"Configuration": "volo.sample"
},
"AuthServer": {
"Authority": "https://auth.sample",
"RequireHttpsMetadata": "true",
"SwaggerClientId": "Sample_Swagger"
},
"StringEncryption": {
"DefaultPassPhrase": "f9uRkTLdtAZLmlh3"
}
}
//Web
{
"App": {
"SelfUrl": "https://web.sample",
"DisablePII": "false"
},
"RemoteServices": {
"Default": {
"BaseUrl": "https://api.sample/"
},
"AbpAccountPublic": {
"BaseUrl": "https://auth.sample/"
}
},
"Redis": {
"Configuration": "volo.sample"
},
"AuthServer": {
"Authority": "https://auth.sample",
"RequireHttpsMetadata": "true",
"ClientId": "Sample_Web",
"ClientSecret": "1q2w3e*"
},
"StringEncryption": {
"DefaultPassPhrase": "f9uRkTLdtAZLmlh3"
}
}
复制 .pfx 文件
您需要将 pfx 文件从 ./src/HttpApi.Host 复制到 ./publish/apihost 文件夹。
发布应用程序
您可以从 IIS 添加为网站。
对于 apihost,我们需要将创建网站的应用程序池中的加载用户配置文件设置为 true。
对于本地部署,添加网站时选择 SSL 证书。
最终结果应如下所示(取决于您的项目类型)。
我们可以从浏览器访问网站。
修复 405 Method Not Allowed 错误
从 Web.config 文件中移除 WebDAV 模块和处理程序。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<modules>
<remove name="WebDAVModule" />
</modules>
<handlers>
<remove name="WebDAV" />
</handlers>
</system.webServer>
</configuration>
如果未使用,请同时从计算机中移除 WebDAV Publishing 功能。为此,请按照以下步骤操作:
- 选择“开始”,在“开始搜索”框中键入“打开或关闭 Windows 功能”,然后选择“打开或关闭 Windows 功能”。
- 在“Windows 功能”窗口中,展开“Internet Information Services” -> “World Wide Web Services” -> “Common HTTP Features”。
- 取消选中“WebDAV Publishing”功能。
参阅:
- https://learn.microsoft.com/zh-cn/aspnet/web-api/overview/testing-and-debugging/troubleshooting-http-405-errors-after-publishing-web-api-applications#resolve-http-405-errors
- https://learn.microsoft.com/zh-cn/troubleshoot/developer/webapps/iis/site-behavior-performance/http-error-405-website#resolution-for-cause-3
将应用程序作为 IIS 子应用程序发布
如果您的 MVC 应用程序是一个子应用程序,则需要将 AbpThemingOptions 的 BaseUrl 属性设置为子应用程序的路径。BaseUrl 用于配置布局页面 head 部分中的 base 元素。
public void ConfigureServices(IServiceCollection services)
{
Configure<AbpThemingOptions>(options =>
{
options.BaseUrl = "/myapp/";
});
}
<html>
<head>
<base href="/myapp/" />
...
</head>
<body>
...
</body>
</html>
对于 Blazor 应用程序,您可以在 App.razor 文件中设置 base 标签,而不是配置 AbpThemingOptions。
如何获取 stdout 日志
如果您的应用程序在 IIS 上运行并遇到 502.5、500.3x 等错误,可以启用 stdout 日志来查看错误详情。
要启用并查看 stdout 日志:
- 导航到宿主系统上的站点部署文件夹。
- 如果 logs 文件夹不存在,请创建该文件夹。有关如何使 MSBuild 在部署中自动创建 logs 文件夹的说明,请参阅 Directory structure 主题。
- 编辑
web.config文件。将stdoutLogEnabled设置为true,并将stdoutLogFile路径更改为指向 logs 文件夹(例如,.\logs\stdout)。路径中的 stdout 是日志文件名前缀。创建日志时,会自动添加时间戳、进程 ID 和文件扩展名。使用 stdout 作为文件名前缀,典型的日志文件被命名为stdout_20180205184032_5412.log。 - 确保您的应用程序池身份对 logs 文件夹具有写入权限。
- 保存更新后的
web.config文件。 - 向应用程序发出请求。
- 导航到 logs 文件夹。找到并打开最新的 stdout 日志。
以下示例 aspNetCore 元素在相对路径
.\log\.处配置 stdout 日志记录。请确认 AppPool 用户身份对提供的路径具有写入权限。
<aspNetCore processPath="dotnet"
arguments=".\MyAbpApp.dll"
stdoutLogEnabled="true"
stdoutLogFile=".\logs\stdout"
hostingModel="inprocess">
</aspNetCore>
参考:
在 Azure App Service 和 IIS 上对 ASP.NET Core 进行故障排除
抠丁客







