Hostinger ASP.NET 应用程序部署教程
ASP.NET 是由 Microsoft 创建的流行 Web 开发框架。它允许开发者使用 C# 和 Visual Basic .NET 等多种编程语言构建动态且功能强大的 Web 应用程序、网站和服务。
凭借其强大的功能以及与其他 Microsoft 技术的无缝集成,ASP.NET 简化了 Web 应用程序的创建和部署过程。
Ubuntu 上支持的 ASP.NET 是 ASP.NET Core。如果你使用的是基于 .NET Framework 或 Web API 的 ASP.NET MVC,可能需要先**将项目转换为 ASP.NET Core MVC**,才能在我们的 Linux VPS 上部署。
要在 Hostinger 上启动 ASP.NET 应用程序,你需要一台安装了 Ubuntu 24.04 64 位 ASP.NET 模板 的 VPS。
访问我们的 ASP.NET 托管 页面,了解更多可用的 VPS 方案信息。
配置 ASP.NET Core 应用程序
通过 SSH 登录你的 VPS 后,你可以运行以下命令来查看已安装的 .NET 版本:
dotnet --version首先,我们使用预装模板创建一个简单的 Web 应用程序,并将其命名为 NewApp:
dotnet new webapp -o NewApp这会为你的应用程序创建一个新文件夹:/root/NewApp/。接下来,我们在 launchSettings.json 中将应用程序的默认端口更改为 5000:
nano /root/NewApp/Properties/launchSettings.json修改后的内容应如下所示:
"profiles": { "NewApp": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "applicationUrl": "http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }此外,我们还需要移除 UseHttpsRedirection() 中间件,因为我们将使用 NGINX 来处理 HTTPS 重定向。打开 Program.cs 文件,确保其中没有 app.UseHttpsRedirection():
nano /root/NewApp/Program.cs由于我们使用 NGINX 作为反向代理,还可以添加 app.UseForwardedHeaders() 中间件。修改后的内容应如下所示:
var builder = WebApplication.CreateBuilder(args);// Add services to the container.builder.Services.AddRazorPages();var app = builder.Build();// Configure the HTTP request pipeline.if (!app.Environment.IsDevelopment()){ app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts();}app.UseStaticFiles();app.UseForwardedHeaders();app.UseRouting();app.UseAuthorization();app.MapRazorPages();app.Run();发布并运行应用程序
要使用 Release 配置发布应用程序,请确保你位于项目目录中,然后运行以下命令:
cd /root/NewApp/dotnet publish --configuration Release项目已发布。现在,我们需要创建一个服务,以确保应用程序能够启动并持续运行。首先,在 /etc/systemd/system 目录中为我们的项目创建一个服务文件:
cd /etc/systemd/systemsudo nano NewApp.service在新文件中添加以下配置:
[Unit]Description=NewApp
[Service]WorkingDirectory=/root/NewApp/bin/Release/net6.0/publish/ExecStart=/usr/bin/dotnet /root/NewApp/bin/Release/net6.0/publish/NewApp.dllRestart=always# Restart service after 10 seconds if the dotnet service crashes:RestartSec=10KillSignal=SIGINTSyslogIdentifier=dotnet-NewAppUser=rootEnvironment=ASPNETCORE_ENVIRONMENT=ProductionEnvironment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]WantedBy=multi-user.target现在,使用以下命令启用并启动该服务:
sudo systemctl enable NewApp.servicesudo systemctl start NewApp.service安装并配置 NGINX
首先安装 NGINX 服务器。它是一款流行的开源 Web 服务器软件,以高性能、可扩展性和可靠性著称,被广泛用作反向代理服务器、负载均衡器和 HTTP 缓存。
sudo apt-get -y install nginx certbot python3-certbot-nginx安装完成后,我们来配置 NGINX。你可以使用以下命令编辑配置文件:
sudo nano /etc/nginx/sites-available/default我们需要对该文件进行两处修改。首先,在 server_name 中设置你的域名:
server_name domain.tld;然后,按如下所示设置 location 配置:
location / { proxy_pass http://localhost:5000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme;}安装 SSL 证书并启动 NGINX 代理
域名 DNS 解析传播完成后,就可以为你的 ASP.NET 应用程序生成 SSL 证书了。运行以下命令自动生成 SSL 证书:
sudo certbot --nginx -d domain.tld之后,重启 NGINX 以使配置生效:
nginx -t && nginx -s reload然后,运行 crontab 命令来自动续期 SSL 证书:
crontab -e最后,添加以下命令,在每天中午 12:00 自动续期 SSL 证书:
0 12 * * * /usr/bin/certbot renew --quiet大功告成!现在,你可以通过本教程中配置的域名访问你的应用程序了。