受欢迎的博客标签

How to Deploying ASP.NET Core Blazor WebAssembly 3.x(Standalone deployment) on Ubuntu 18.04 step by step

Published

Introduction

In this tutorial you’ll set up and deploy a production-ready ASP.NET Core Blazor WebAssembly 3.x (Standalone deployment) Ubuntu 18.04 step by step   using Nginx.

How it works

Remember the relationships.

OS/ ubuntu server 18.04-x64 Solution Folder

OS---------- ubuntu server 18.04-x64 
 ├── .NET SDK & .NET  Runtime
 │   ├── .NET SDK ---------- SDK
 │   ├── ASP.NET Core Runtime    <--- The ASP.NET Core Runtime enables you to run existing web/server applications.
 │   ├── Desktop Runtime            <--- The Desktop Runtime enables you to run existing Windows desktop applications.
 │   └── .NET Runtime                   <---  The .NET Runtime enables you to run existing  console app.
 ├── ASP.NET Core Blazor WebAssembly(Hosted deployment)   <---- Listens on http://locahost:5000
 │   ├── Client/                              <---BlazorWebAssembly.Client.csproj      client for create request web api to BlazorWebAssembly.Server.csproj
 │   ├── Server/                             <---BlazorWebAssembly.Server.csproj      web api server
 │   └── Shared/                            <---BlazorWebAssembly.Shared.csproj
 ├── ASP.NET Core Blazor WebAssembly(Standalone deployment)    <--- BlazorWebAssemblyStandaloneeployment.csproj
 │   ├── Pages/                              <---
 │   ├── Shared/                             <---
 │   ├── wwwroot/                              <---
 │   ├── _Imports.razor                             <---
 │   ├── App.razor                             <---
 │   ├── Program.cs                             <---
 │   └── BlazorWebAssemblyStandaloneeployment.csproj                            <---B

One Ubuntu 18.04 server - Your server

ASP.NET Core Web Application - Runs your app / website on  Ubuntu 18.04 server
nginx - Listens on Port 443 / 80 and forwards HTTP/S calls to your website.

A secured Nginx web server

Table of contents

Requirements

Register Microsoft key and feed
Install the .NET Core  Runtime
Install the .NET SDK

Create a new ASP.NET Core web application
Publish and copy over the app

Install nginx as reverse proxy
Configure SSL
Configure nginx
Monitoring the app
Conclusion

 

Prepare an  Run Evironmentn for ASP.NET Core Application on Ubuntu

Install the .NET Core 3.x Runtime <--an  Run Evironmentn for ASP.NET Core Application on Ubuntu
Install the .NET Core 3.x SDK        <--an  SDK for ASP.NET Core Application on Ubuntu

Prepare an ASP.NET Core Website

insall web <--Running an ASP.NET Core Website on Ubuntu

 

nginx

insall nginx

Configure NGINX<--Configure NGINX as a Reverse Proxy to ASP.NET Core

install https<--Configure the reverse proxy for secure (HTTPS) client connections

domain

buy domain

part one:Prepare an  Run Evironmentn for ASP.NET Core Application on Ubuntu

step 1.Setting up a Linux host

1.Prerequisites

Linux Distribution

   ubuntu 18.04-x64

   root permissions

2.One Ubuntu 18.04 server

There are many service providing offering a cheap Linux based VPS (virtual private server). vultr.com offers  a single core, 1GB RAM instance for  about 5 USD/month. The instance we select is an Ubuntu Server 18.04 *64 LTS (HVM) with 8GB of storage (SSD).

3.NET Core 3.0 - Supported OS versions

.NET Core 3.0 is supported on the following Linux distributions/versions:

OS:Ubuntu
Ubuntu OS Version >=16.04* 64,
Architectures:x64, ARM32, ARM64

For information on  Linux distributions supported by ASP.NET Core, see Prerequisites for .NET Core on Linux.

https://docs.microsoft.com/en-us/dotnet/core/linux-prerequisites?tabs=netcore30

 

step 2. connect to this instance through SSH

1.On a Windows machine, you can use mRemoteNG to connect to a Linux machine. 

2.PuTTY: a free SSH and Telnet client

https://www.chiark.greenend.org.uk/~sgtatham/putty/?spm=5176.2020520101.0.0.20c74df5gR1HOz

3.Xshell6 https://www.netsarang.com/en/all-downloads/

4.Termius SSH:https://www.termius.com/windows

 

step 3.Prepare an  Run Evironmentn for ASP.NET Core Application on Ubuntu

Prepare ASP.NET Projects

Initial Server Setup with Ubuntu 18.04 

How To Install Nginx on Ubuntu 18.04

Install winscp on windows

other tools:

SecureCRT+SecureFX  https://www.portablesoft.org/securecrt-securefx-legacy-versions/

 

# lsb_release -a

LSB Version:	core-9.20160110ubuntu0.2-amd64:core-9.20160110ubuntu0.2-noarch:security-9.20160110ubuntu0.2-amd64:security-9.20160110ubuntu0.2-noarch
Distributor ID:	Ubuntu
Description:	Ubuntu 16.04.4 LTS
Release:	16.04
Codename:	xenial

 

 

Install .NET Core SDK on Linux Ubuntu 18.04 - x64

you can read the doc https://dotnet.microsoft.com/download/linux-package-manager/ubuntu18-04/sdk-current and  run the  commands.

This tutorial installs .Net core 3.x on  ubuntu 18.04-x64 .

step 1:Register Microsoft key and feed,This only needs to be done once per machine.

Before installing .NET, you'll need to register the Microsoft key, register the product repository, and install required dependencies. 

Open a terminal and run the following commands:

wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb

step 2:Install the .NET SDK

sudo add-apt-repository universe
sudo apt-get update
sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install dotnet-sdk-3.1

 

step 3.Check everything installed correctly

To make sure it works after you’ve set it up, just run:

 

#dotnet --info

.NET Core SDK (reflecting any global.json):
 Version:   3.0.100
 Commit:    04339c3a26

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  18.04
 OS Platform: Linux
 RID:         ubuntu.18.04-x64
 Base Path:   /usr/share/dotnet/sdk/3.0.100/

Host (useful for support):
  Version: 3.0.0
  Commit:  95a0a61858

.NET Core SDKs installed:
  3.0.100 [/usr/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.App 3.0.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.0.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

If the command runs, printing out information about dotnet, you're good to go.

step 4:Website Creation

#mkdir myapp
#cd myapp
#dotnet new -i Microsoft.AspNetCore.Components.WebAssembly.Templates
#cd test
The template "ASP.NET Core Empty" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on test1/test1.csproj...
  Restore completed in 1.03 sec for /root/myapp/test1/test1.csproj.

Restore succeeded.

The dotnet command creates a new website server application of type website for you. The -o parameter creates a directory named test where your app is stored, and populates it with the required files. The cd myApp command puts you into the newly created app directory.

step5:Run the app

Kestrel is the web server that is included by default in ASP.NET Core new project templates. It is a cross-platform web server for ASP.NET Core based on libuv, a cross-platform asynchronous I/O library. If your application accepts requests only from an internal network, you can use Kestrel by itself.

 

error:

dotnet run --urls http://localhost:5005

 

correct:

dotnet run --urls http://0.0.0.0:5005

output:

warn: Microsoft.AspNetCore.Server.Kestrel[0]
      Unable to bind to http://localhost:5005 on the IPv6 loopback interface: 'Cannot assign requested address'.
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://localhost:5005
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /var/www/BlazorWebAssembly/BlazorWebAssemblyStandaloneeployment

 

step 5:Test the Application Directly on Kestrel Server

navigate to http://<serveraddress>:<port> to verify the app works on Linux locally.

visit http://localhost:5000/。,open another terminal

wget http://localhost:5000/

output:

--2020-06-26 20:02:18--  http://localhost:5000/
Resolving localhost (localhost)... 127.0.0.1, ::1
Connecting to localhost (localhost)|127.0.0.1|:5000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 679 [text/html]
Saving to: ‘index.html’

index.html                 100%[=======================================>]     679  --.-KB/s    in 0s      

2020-06-26 20:02:18 (91.4 MB/s) - ‘index.html’ saved [679/679]

We use the following command to list the available releases of .NET Core on Linux host.

apt-cache search dotnet

 

Configure ASP.NET Core as a Background Service

A standalone deployment serves the Blazor WebAssembly app as a set of static files that are requested directly by clients. Any static file server is able to serve the Blazor app.

Standalone deployment assets are published into the /bin/Release/{TARGET FRAMEWORK}/publish/wwwroot folder.

dotnet publish -c release

output:

Standalone deployment assets are published into the /bin/Release/netstandard2.1/publish/wwwroot folder.

Welcome to .NET Core 3.1!
---------------------
SDK Version: 3.1.301

Telemetry
---------
The .NET Core tools collect usage data in order to help us improve your experience. The data is anonymous. It is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.

Read more about .NET Core CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry

----------------
Explore documentation: https://aka.ms/dotnet-docs
Report issues and find source on GitHub: https://github.com/dotnet/core
Find out what's new: https://aka.ms/dotnet-whats-new
Learn about the installed HTTPS developer cert: https://aka.ms/aspnet-core-https
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli-docs
Write your first app: https://aka.ms/first-net-core-app
--------------------------------------------------------------------------------------
Microsoft (R) Build Engine version 16.6.0+5ff7b0c9e for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  Restored /var/www/stock/ReadDZHRealTime/src/BlazorWebAssembly/BlazorWebAssemblyStandaloneeployment/BlazorWebAssemblyStandaloneeployment.csproj (in 995 ms).
  BlazorWebAssemblyStandaloneeployment -> /var/www/stock/ReadDZHRealTime/src/BlazorWebAssembly/BlazorWebAssemblyStandaloneeployment/bin/release/netstandard2.1/BlazorWebAssemblyStandaloneeployment.dll
  BlazorWebAssemblyStandaloneeployment (Blazor output) -> /var/www/stock/ReadDZHRealTime/src/BlazorWebAssembly/BlazorWebAssemblyStandaloneeployment/bin/release/netstandard2.1/wwwroot
  BlazorWebAssemblyStandaloneeployment -> /var/www/stock/ReadDZHRealTime/src/BlazorWebAssembly/BlazorWebAssemblyStandaloneeployment/bin/release/netstandard2.1/publish/

 

Nginx

 

server {
	listen 80;
	listen [::]:80;

	server_name  s.stockso.com ;

	root /var/share/BlazorWebAssembly/BlazorWebAssemblyStandaloneeployment/bin/release/netstandard2.1/publish/wwwroot;
	index index.html;

	location / {
		try_files $uri $uri/ =404;
	}
}

 

 

 

 

 

 

Resources

Prerequisites for .NET Core on Linux

https://docs.microsoft.com/en-us/dotnet/core/linux-prerequisites?tabs=netcore2x

Standalone deployment ASP.NET Core 3.x on Linux with Nginx

https://docs.microsoft.com/en-us/aspnet/core/blazor/host-and-deploy/webassembly?view=aspnetcore-3.1#standalone-deployment