受欢迎的博客标签

How to Deploying Real World ASP.NET Core 6.x on Ubuntu 18.04 step by step(vultr)

Published

Introduction

In this tutorial you’ll set up and deploy a production-ready ASP.NET Core application with a MongoDb Server on Ubuntu 18.04 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
 │   ├── .NET Runtime               <- ---------The .NET Runtime enables you to run existing  console app.
 │   ├── ASP.NET Core Runtime    <--- The ASP.NET Core Runtime enables you to run existing web/server applications.
 │   ├── .NET Desktop Runtime           <---(net6.0-windows) ,The .NET Desktop Runtime enables you to run existing Windows desktop applications. This release includes the .NET Runtime; you don't need to install it separately.
 ├── .NET MAUI Workload & 
 │   ├── net6.0-ios            <---ios  for .NET, These TFMs represent OS-specific flavors of .NET 5 that include net5.0 plus OS-specific functionality.
 │   └──net6.0-android                   <---Android for .NET, These TFMs represent OS-specific flavors of .NET 5 that include net5.0 plus OS-specific functionality.
 ├── ASP.NET Core  web applications   <---- Listens on http://locahost:5000
 │   ├── nongodb server
 │   └── redis server
 └── Nginx web server   <--- Listens on Port 443 / 80 and forwards HTTP/S calls to http://locahost:5000.

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
Mongodb Redis - Keeps your app data
Supervisor - Keeps your app running

 

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 Monodb
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 5.x Runtime <--an  Run Evironmentn for ASP.NET Core Application on Ubuntu
Install the .NET Core 5.x SDK        <--an  Run Evironmentn for ASP.NET Core Application on Ubuntu

Prepare an ASP.NET Core Website

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

install mongodb

install redis

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  6.0 - Supported OS versions

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

OS:Ubuntu
Ubuntu OS Version:  20.10, 20.04, 18.04, 16.04

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/install/linux-ubuntu#supported-distributions

 

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

https://winscp.net/download/WinSCP-5.15.3-Setup.exe

文件传输协议 = SFTP-3
加密协议 = SSH-2
SSH实现 = OpenSSH_7.2p2 Ubuntu-4ubuntu2.4
加密算法 = aes
压缩 = 否

 

other tools:

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

 

# lsb_release -a

No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.6 LTS
Release:	18.04
Codename:	bionic

https://github.com/dotnet/core/blob/master/release-notes/5.0/5.0-supported-os.md

Install using deb/rpm packages

step 1:Try purging the package list:

 

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

output:

Saving to: ‘packages-microsoft-prod.deb’ ...
Preparing to unpack packages-microsoft-prod.deb ...
Unpacking packages-microsoft-prod (1.0-ubuntu18.04.2) ...
Setting up packages-microsoft-prod (1.0-ubuntu18.04.2) ...

step 2:Run a manual install with the following commands:

First, download a binary release for either the SDK or the runtime from one of the following sites:

Open a terminal and run the following commands:

sudo apt-get update; \
  sudo apt-get install -y apt-transport-https && \
  sudo apt-get update && \
  sudo apt-get install -y dotnet-sdk-6.0

output:

 Setting up dotnet-hostfxr-6.0 (6.0.0-1) ...
Setting up dotnet-runtime-6.0 (6.0.0-1) ...
Setting up aspnetcore-runtime-6.0 (6.0.0-1) ...
Setting up dotnet-sdk-6.0 (6.0.100-1) ...

https://docs.microsoft.com/en-us/dotnet/core/install/linux-ubuntu#1804-

 

step 3.Check everything installed correctly

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

#dotnet --info

.NET SDK (reflecting any global.json):
 Version:   6.0.100
 Commit:    9e8b04bbff

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

Host (useful for support):
  Version: 6.0.0
  Commit:  4822e3c3aa

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

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

To install additional .NET 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 mvc -o test

Output:

Welcome to .NET 6.0!
---------------------
SDK Version: 6.0.100

Telemetry
---------
The .NET tools collect usage data in order to help us improve your experience. 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 CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry

----------------
Installed an ASP.NET Core HTTPS development certificate.
To trust the certificate run 'dotnet dev-certs https --trust' (Windows and macOS only).
Learn about HTTPS: https://aka.ms/dotnet-https
----------------
Write your first app: https://aka.ms/dotnet-hello-world
Find out what's new: https://aka.ms/dotnet-whats-new
Explore documentation: https://aka.ms/dotnet-docs
Report issues and find source on GitHub: https://github.com/dotnet/core
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli
--------------------------------------------------------------------------------------
The template "ASP.NET Core Web App (Model-View-Controller)" was created successfully.
This template contains technologies from parties other than Microsoft, see https://aka.ms/aspnetcore/6.0-third-party-notices for details.

Processing post-creation actions...
Running 'dotnet restore' on /root/dotnet_install/myapp/test/test.csproj...
  Determining projects to restore...
  Restored /root/dotnet_install/myapp/test/test.csproj (in 120 ms).
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.

#cd test

error:

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

 

correct:

dotnet run --urls http://0.0.0.0:5005
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
      No XML encryptor configured. Key {b3ba5299-a2aa-40a7-a90d-a39a11b909f8} may be persisted to storage in unencrypted form.
info: Microsoft.Hosting.Lifetime[14]
      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: /root/dotnet_install/myapp/test/

 

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/

 

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

 We will use systemd and create a service file to start and monitor the underlying web app. systemd is an init system that provides many powerful features for starting, stopping and managing processes.

We can create a configuration file using the following command

sudo vi /etc/systemd/system/kestrel-wwwstocksocom.service

The content of the .sevice file is as follows.

[Unit]
Description=www.stockso.com App running on Ubuntu

[Service]
WorkingDirectory=/var/www/nopcommerce/wwwstocksocom/src/Presentation/Nop.Web/bin/release/netcoreapp3.0/publish
ExecStart=/usr/bin/dotnet /var/www/nopcommerce/wwwstocksocom/src/Presentation/Nop.Web/bin/release/netcoreapp3.0/publish/Nop.Web.dll --urls http://0.0.0.0:16999
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data  #以www-datar的身份启动
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

If you completed the configuration and save the .service file, you need to reload and enable it.

first,reload kestrel-wwwstocksocom.service

sudo systemctl daemon-reload 

Reload systemd manager configuration. This will rerun all generators (see systemd.generator(7)), reload
       all unit files, and recreate the entire dependency tree. While the daemon is being reloaded, all sockets
       systemd listens on behalf of user configuration will stay accessible.

second enable it.

sudo systemctl enable kestrel-wwwstocksocom.service

then start it.

sudo systemctl start kestrel-wwwstocksocom.service
sudo systemctl restart kestrel-wwwstocksocom.service

 

sudo systemctl status kestrel-wwwstocksocom.service

 

sudo journalctl -fu kestrel-wwwstocksocom.service

 

在你的Linux 里确认 8045 处于 Listen 状态用命令: netstat -tlanp | grep 8045

首先登录到云管理控制台,然后“实例”->“更多”->“网络安全和安全组”->“安全组配置”->“配置规则”->“入方向”->“添加安全组规则”->
端口范围  80/80
授权对象  0.0.0.0/0

 

加规则到安全测量组

将实例加入到安全策略组

 

windows

1.dotnet publish

2.copy in\Debug\netcoreapp2.1\publish\*.* to linux  var/www/

3.cd var/www/publish

4.dotnet project.dll

 

Resources

Prerequisites for .NET Core on Linux

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

Microsoft Host ASP.NET Core 3.x on Linux with Nginx

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-3.0

Debug Linux core dumps in Visual Studio

https://devblogs.microsoft.com/cppblog/debug-linux-core-dumps-in-visual-studio/