Deploy .Net Core Web project on Linux

This article mainly records my operation record of deploying .net core on Linux (Ubuntu), which is also convenient for future deployment.
It's a great honor to be of help to you~


foreword

This article mainly introduces how to deploy the dotnet core web application developed on windows to a Linux server (Ubuntu 22.04).

The software version I am using is as follows:

  • OS version: Ubuntu 22.04
  • web project version: .net 6.0

The website I deployed on the windows side is as follows. Port: 5001. The goal of this article is to deploy it to Ubuntu.
insert image description here


1. Preparation

1. Version information

  • OS version: Ubuntu 22.04
  • web project version: .net 6.0

2. Windows-side web project

2. Operation steps

1. Linux configuration.net operating environment

As we all know, the operation of all .net projects requires a .net runtime environment.
There are two types of .net operating environments: sdk and runtime (runtime).

  • sdk: sdk contains runtime, and can provide .net developers with a running, compiling and packaging environment for .net projects.
  • runtime: runtime is a subset of sdk, which can only make .net programs run on the operating system.

If you don't need to do .net development on Linux, you can only install the runtime. I will mainly demonstrate the installation of the sdk here.

Official Microsoft Reference Documentation:
Installing the .NET SDK or .NET Runtime on Ubuntu Installing .NET
on Linux Using the Install Script or by Extracting the Binaries

1.1 View the download path of the latest .net operating environment

Go to the official download address of .net , and the content of the page will be opened as shown in the figure below:
insert image description here
We select .NET 6.0 in the figure above, enter the page in the figure below, and select the SDK address suitable for our operating system architecture.
insert image description here

The command to view the system architecture in ubuntu is as follows:

dpkg --print-architecture

insert image description here
Mine is amd64, select x64.
insert image description here

Click the [copy] button to copy the download address:
https://download.visualstudio.microsoft.com/download/pr/868b2f38-62ca-4fd8-93ea-e640cf4d2c5b/1e615b6044c0cf99806b8f6e19c97e03/dotnet-sdk-6.0.407-linux- x64. tar.gz

1.2 Install dotnet SDK

1.2.1 Download the installation package

We use the wget command to download the copied runtime installation package

wget https://download.visualstudio.microsoft.com/download/pr/868b2f38-62ca-4fd8-93ea-e640cf4d2c5b/1e615b6044c0cf99806b8f6e19c97e03/dotnet-sdk-6.0.407-linux-x64.tar.gz

insert image description here

1.2.2 Unzip the installation package

Create a .dotnet folder in the current home directory

mkdir -p .dotnet

insert image description here
Created successfully:
folder option, you can see the .dotnet folder after setting the display of hidden files:
insert image description here
unzip the files in the installation package to the .dotnet folder

tar -zxvf dotnet-sdk-6.0.407-linux-x64.tar.gz -C .dotnet

insert image description here
Decompression succeeded.
insert image description here

1.2.3 Setting environment variables

To make environment variables valid for all users, you need to modify the profile file:

sudo vim /etc/profile 

insert image description here
Type [i] to insert the following statement:

export DOTNET_ROOT=$(pwd)/.dotnet
export PATH=$PATH:$DOTNET_ROOT:$DOTNET_ROOT/tools

insert image description here
Type [Esc] again to exit edit mode.
Type [:wq] to save and exit Vim.
Log off or restart to take effect.

1.3 Verification

After restarting, check the dotnet information through the dotnet --info command.
insert image description here
Setup is complete.

SDK installed successfully.

2. Windows-side web project publishing option settings

The publishing option of the web project in Visual Studio needs to change the TargetRuntime to the Linux version, and choose the Linux architecture version that suits you. We have already talked about how to view your own Linux architecture in the previous article. For my amd64, choose x64.
insert image description here
Deployment Mode:

  • Self-contained (independent application) comes with dotnet runtime, which can be deployed to a blank environment server, without the need to install and configure dotnet runtime in advance. The disadvantage is: the deployment package is larger.
  • Framework-dependent (framework-dependent) generates a cross-platform deployment package, and the user needs to install the corresponding version of the runtime on Linux.

I have installed the .net 6.0 SDK before, including the runtime, so here I choose Framework-dependent.

3. Upload web publishing files from windows to LInux

After the setup is successful, select Publish to publish the web application to a folder.
After the release is successful, use ftp to upload the web release file from windows to Linux.
PS: My web publishing project on Linux is: /home/qyh/workspace/WebPublishFolder/WowCrtAdvrts
insert image description here

The upload is successful:
insert image description here
the program entry in the above picture is: Jcjy.Web (your web project name)

4. Linux runs web projects

The console enters the .net web project path:

cd ~/workspace/WebPublishFolder/WowCrtAdvrts

insert image description here
Run the web project:

dotnet Jcjy.Web.dll

Enter the ip+port number in the browser, as shown in the figure below. The deployment was successful.
insert image description here
So far, the deployment of .Net Core Web on Linux has been finished. Next, I will mainly talk about how to use Nginx as a reverse proxy.

5. Set the web project to start automatically at boot

Supervisor is a general-purpose process management program developed by python. It can turn ordinary command-line processes into background daemon processes, monitor the process status, and automatically restart when the process exits abnormally.

5.1 install supervisor

sudo apt-get install supervisor

insert image description here
As shown in the figure, the installation is complete.

5.2 Add configuration information for .net web project in supervisor

5.2.1 Create the log output folder of the .net web project

Let's first create the log output folder of the web project. Here I am going to put the log in this directory: /var/log/web/dotnet/

sudo mkdir -p /var/log/web/dotnet

5.2.2 Enter /etc/supervisor/conf.d

After the supervisor is installed, there will be a supervisor folder under /etc/, and conf.d is the folder where the supervisor configuration file is stored.

cd /etc/supervisor/conf.d

insert image description here

5.2.3 Create a new config file

We create configuration files for .net web projects:

sudo touch WowCrtAdvrts.conf

Here [WowCrtAdvrts] is your web project name,

insert image description here

5.2.4 Add configuration items such as booting automatically

Edit the WowCrtAdvrts.conf file

sudo vi WowCrtAdvrts.conf
i

insert image description here
Enter the configuration items:

[program:WowCrtAdvrts]
command=dotnet Jcjy.Web.dll
directory=/home/qyh/workspace/WebPublishFolder/WowCrtAdvrts/
environment=ASPNETCORE__ENVIRONMENT=Production 
user=root  
stopsignal=INT
autostart=true 
autorestart=true 
startsecs=1 
stderr_logfile=/var/log/web/dotnet/WowCrtAdvrts.err.log 
stdout_logfile=/var/log/web/dotnet/WowCrtAdvrts.out.log 

directory is the directory where our .net core web project is located;
stderr_logfile is the path where the error log is located, and stdout_logfile is the path where the output log is located, which I placed under /var/log/web/dotnet/; then enter ESC->:wq to
insert image description here
save Just exit.
insert image description here
The modification is complete.

5.2.5 restart supervisor

sudo service supervisor stop 
sudo service supervisor start

insert image description here

5.2.6 Check whether the supervisor service is running normally

sudo supervisorctl

5.3.5 Open the visual web site management interface for supervisor (optional operation)

The supervisor comes with a visual management interface, which can be opened after we do some configuration.
In the /etc/supervisor directory, open the supervisor.conf file

cd /etc/supervisor
sudo vi supervisor.conf
i

insert image description here

Add the following configuration items

[inet_http_server]    
port=127.0.0.1:8001     
username=admin
password=123456

insert image description here

save changes and exit

ESC
:wq

restart supervisor

sudo service supervisor stop 
sudo service supervisor start

Summarize

The above is what I will talk about today. Generally speaking, it is still very simple, but I still stepped on some pitfalls when referring to online tutorials during specific operations. It is a great honor if this article is helpful to you~

Guess you like

Origin blog.csdn.net/guigenyi/article/details/129869230