Ubuntu 22.04 .NET8 程序 环境安装和运行

目录

一、安装.NET运行时

二、运行程序

三、使用systemctl管理


前言

我们需要将.NET8编写的console控制台程序,部署在Ubuntu服务器上运行。

一、安装.NET运行时

1.增加微软包安装源

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

2.安装.NET8运行时

sudo apt-get update &&  sudo apt-get install -y aspnetcore-runtime-8.0

 安装SDK(如果需要编译)

sudo apt-get update && sudo apt-get install -y dotnet-sdk-8.0

3.验证是否安装成功

dotnet --info
root@ubuntu01:/# dotnet --info

Host:
  Version:      8.0.5
  Architecture: x64
  Commit:       087e15321b
  RID:          linux-x64

.NET SDKs installed:
  No SDKs were found.

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

Other architectures found:
  None

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download
root@ubuntu01:/# 
二、运行程序

1.将发布后文件夹上传到linux服务器


2.添加执行权限

chmod +x -R  /file/helloworld/

3.运行DotNet程序

dotnet helloworld.dll

# 增加微软包安装源

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


# 安装SDK(如果需要编译)

sudo apt-get update && sudo apt-get install -y dotnet-sdk-8.0


# 安装运行时

sudo apt-get update &&  sudo apt-get install -y aspnetcore-runtime-8.0

# 验证是否安装成功

dotnet --info

# 上传发布文件到linux服务器


# 添加执行权限

chmod +x -R  /file/helloworld/

# 运行DotNet程序

dotnet helloworld.dll
三、使用systemctl管理

在Linux系统中,使用systemd管理.NET程序,实现程序自动开机启动,查看状态等操作。

1.创建service文件

vim /usr/lib/systemd/system/helloword.service

# 添加下面的内容

[Unit]
Description=helloworld .NET Web Application
 
[Service]
WorkingDirectory=/file/helloworld/
ExecStart=/usr/bin/dotnet /file/helloword/helloworld.dll
Restart=always
# Restart service after 2 seconds if the dotnet service crashes:
RestartSec=2
KillSignal=SIGINT
SyslogIdentifier=helloworld
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
 
[Install]
WantedBy=multi-user.target

2.添加执行权限

chmod +x /usr/lib/systemd/system/helloworld.service

3.重新加载和启用开机自启

systemctl daemon-reload && systemctl enable helloworld.service

4.查看状态

systemctl status helloworld.service
root@ubuntu01:/# systemctl status helloworld.service 
● helloworld.service - helloworld .NET Web Application
     Loaded: loaded (/lib/systemd/system/helloworld.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2024-05-29 17:39:48 CST; 1min 54s ago
   Main PID: 27708 (dotnet)
      Tasks: 15 (limit: 4558)
     Memory: 47.4M
        CPU: 1.921s
     CGroup: /system.slice/helloworld.service
             └─27708 /usr/bin/dotnet /file/helloworld/helloworld.dll

May 29 17:39:48 ubuntu01 systemd[1]: Started helloworld.NET Web Application.
May 29 17:39:48 ubuntu01 helloworld[27708]: * Start load configure...... Configure Load Sucessfull
May 29 17:39:48 ubuntu01 helloworld[27708]: * Mysql connection Openning....
May 29 17:39:48 ubuntu01 helloworld[27708]: Process with an Id of 26029 is not running.

5.查看进程

ps -ef | grep helloworld

猜你喜欢

转载自blog.csdn.net/gmaaa123/article/details/139267572