expressappframework-404717-deployment-asp-net-core-blazor-deploy-net-core-application-to-linux-with-nginx.md
This topic describes how to set up and publish an XAF Blazor Server UI application to Ubuntu, Red Hat Enterprise (RHEL), and SUSE Linux Enterprise Server machines with Nginx reverse proxy.
The Template Kit uses Microsoft SQL Server as the default database provider option. This examples changes that default option to PostgreSQL (simply as a customization example). For more information on how to switch database providers, refer to the following topic: Specify EF Core Database Provider in XAF Application.
Access to Ubuntu. Use its standard user account with sudo privileges.
The latest stable .NET runtime installed on the server.
PostgreSQL database engine installed.
An XAF ASP.NET Core application. This topic is based on the MainDemo Blazor Server demo application that ships with XAF. You can find this demo in the following folder: %PUBLIC%\Documents\DevExpress Demos 25.2\Components\XAF\MainDemo.NET.EFCore\CS\MainDemo.Blazor.Server.
Access to Red Hat Enterprise (RHEL) v8.0 or later. Use its standard user account with sudo privileges.
The latest stable .NET runtime installed on the server.
PostgreSQL database engine installed.
An XAF ASP.NET Core application. This topic is based on the MainDemo Blazor Server demo application that ships with XAF. You can find this demo in the following folder: %PUBLIC%\Documents\DevExpress Demos 25.2\Components\XAF\MainDemo.NET.EFCore\CS\MainDemo.Blazor.Server.
Access to SUSE Linux Enterprise Server (SLES) v12 or v15. Use its standard user account with sudo privileges.
The latest stable .NET runtime installed on the server.
PostgreSQL database engine installed.
An XAF ASP.NET Core application. This topic is based on the MainDemo Blazor Server demo application that ships with XAF. You can find this demo in the following folder: %PUBLIC%\Documents\DevExpress Demos 25.2\Components\XAF\MainDemo.NET.EFCore\CS\MainDemo.Blazor.Server.
Install the following font libraries. These libraries allow the application to measure text to correctly render documents and export them to PDF.
Install the ttf-mscorefonts-installer package that adds Microsoft TrueType core fonts to your system. (Installed fonts include Arial, Times New Roman, Courier New, and others.)
Install the package below to render JPEG images in PDF files (you can install any other package that implements libjpeg API v6.2 or v8.0.).
Make sure an appropriate drawing engine is available. Add the following packages to your MainDemo.Blazor.Server project:
Since requests are forwarded through a reverse proxy, use Forwarded Headers Middleware. It updates Request.Scheme with the X-Forwarded-Proto header so that redirect URIs and other security policies work correctly.
Forwarded Headers Middleware should run before any other middleware. This order ensures that other middleware can consume header values for processing. To run Forwarded Headers Middleware after diagnostic and error handling middleware, see Forwarded Headers Middleware order.
To set up this middleware, call the UseForwardedHeaders method from your project’s Startup.cs file before calling any other middleware. Within this call, configure the middleware to forward X-Forwarded-For and X-Forwarded-Proto headers.
//...
using Microsoft.AspNetCore.HttpOverrides;
namespace MainDemo.Blazor.Server;
public class Startup {
//...
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
//...
app.UseForwardedHeaders(new ForwardedHeadersOptions {
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
//...
}
}
To use PostgreSQL database provider, follow the steps below:
Install the Npgsql.EntityFrameworkCore.PostgreSQL package to both MainDemo.Module and MainDemo.Blazor.Server projects. Make sure that this package’s version matches the version of the EntityFrameworkCore package you have installed.
Change the application connection string.
If you use migrations, update the MainDemoDesignTimeDbContextFactory.ConnectionString property as follows:
You may see an error message that says “Cannot write DateTime with Kind=Unspecified to PostgreSQL type ‘timestamp with time zone’“. If this happens, set DateTimeKind to UTC for each DateTime property or add the following line to the Program.Main() method.
namespace MainDemo.Blazor.Server;
public class Program : IDesignTimeApplicationFactory {
public static int Main(string[] args) {
//...
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
//...
}
}
Refer the following topic for details: Date and Time Handling.
Make sure that the DevExpress NuGet source URL is registered in your source list. Use the following link to locate this URL: Your DevExpress NuGet Feed URL. We also recommend that you use the NuGet v3 source URL.
Execute the dotnet publish command from the development environment to package an application into a directory that can be run on the server.
Copy your application to the server using a tool that is integrated into the organization’s workflow (for example, SCP, SFTP). It is common to store web applications in the var directory (for example, var/www/MainDemo.Blazor.Server).
Check that the application works correctly. To do this, run the application from the command line.
If the application database exists and does not require any updates, your XAF Blazor application is ready to use.
Install Nginx. To do this, you can use the following instructions:
As Nginx has just been installed, explicitly start it.
Configure Nginx as a reverse proxy to forward HTTP requests to your ASP.NET Core application.
In the configuration snippet above, Nginx accepts public HTTPS traffic on ports 80 and 443 and redirects unsecured HTTP traffic to an encrypted HTTPS connection. Nginx forwards matching requests to Kestrel at http://127.0.0.1.
Specify the path to the certificate file and associated key. For example, you can generate a self-signed certificate file for testing purposes. For more information, refer to the following tutorial: How To Create a Self-Signed SSL Certificate for Nginx in Ubuntu 16.04.
Run the following command to check that configuration file syntax is correct.
Run the command below to apply the new configuration to Nginx.
Navigate to the application directory and run the application.
Navigate to https://<your_host_name> in a browser to check that Nginx is working correctly with the application.
Nginx cannot manage Kestrel application processes. You can use systemd to create a service file that starts and monitors the underlying web application.
Create a service definition file.
The example above uses the /usr/bin/dotnet command to start the application. You can change this option if the dotnet runtime location is different, or if you have published your application with the --self-contained argument. For example:
Save the file and enable the service.
Start the service.
Verify that the service is running.
These deployment recommendations do not apply to all possible configurations and should not be considered comprehensive. We offer these instructions as a getting-started reference. Steps may vary depending on your operating system, installed software, and DevExpress versions. You, the developer, are responsible for the application, database, network, and other configurations based on your client, security, environment, and other requirements. We recommend that you review these settings with your database, network, and IT infrastructure administrators and consider their recommendations tailored to your case. We also recommend that you review Performance Optimization tips for deployed applications.
If you encounter problems, refer to the following topic: Deployment Troubleshooting Guide.
Refer to the Microsoft documentation for more information about deploying ASP.NET Core and Blazor applications: