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.
Table of Contents
How it works
Remember the relationships.
OS/ ubuntu server 18.04-x64 Solution Folder
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 SDK
Install the .NET Core Runtime
Install ASP.NET Core Runtime
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 3.x Runtime <--an Run Evironmentn for ASP.NET Core Application on Ubuntu
Install the .NET Core 3.x SDK <--an Run Evironmentn for ASP.NET Core Application on Ubuntu
Prepare an ASP.NET Core Website
insall 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 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
https://winscp.net/download/WinSCP-5.15.3-Setup.exe
other tools:
SecureCRT+SecureFX https://www.portablesoft.org/securecrt-securefx-legacy-versions/
3 Ways to Use SSH on Windows to Log Into Linux Server
Method 1: Windows 10’s Built-in SSH Client
Use the OpenSSH client on Windows 10,open a PowerShell window or a command prompt window and run the ssh command:
Method 2: Use SSH in Windows Subsystem for Linux
Open the Microsoft Store and enter WSL in the search box. Select Run Linux on Windows and install a Linux distro of your choice.
Once your Linux distro is installed, open the Control Panel and select Programs -> Turn Windows features on or off. Tick on the checkbox of Windows Subsystem for Linux to enable this feature. (You may need to reboot your Windows PC for this change to take effect.)
Next, you can launch the Linux distro from the start menu by search the distro’s name. The first time you launch it, you need to create a user and set a password.
Method 3: Use SSH client on Windows
# lsb_release -a
Install .NET Core SDK on Linux Ubuntu 22.04 - x64
you can read the doc https://learn.microsoft.com/en-us/dotnet/core/install/linux-ubuntu-install?tabs=dotnet9&pivots=os-linux-ubuntu-2204 and run the commands.
This tutorial installs .Net 9.x on ubuntu 22.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:
step 2:Install the .NET SDK
step 3.Check everything installed correctly
To make sure it works after you’ve set it up, just run:
#dotnet --info
If the command runs, printing out information about dotnet, you're good to go.
step 4:Website Creation
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:
correct:
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
or
output
We use the following command to list the available releases of .NET Core on Linux host.
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
The content of the .sevice file is as follows.
If you completed the configuration and save the .service file, you need to enable it.
then start it.
在你的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 5.x on Linux with Nginx
https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-5.0
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/
3 Ways to Use SSH on Windows to Log Into Linux Server