当前位置:   article > 正文

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

ubuntu systemctl dotnet

目录

一、安装.NET运行时

二、运行程序

三、使用systemctl管理


前言

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

一、安装.NET运行时

1.增加微软包安装源

  1. wget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
  2. sudo dpkg -i packages-microsoft-prod.deb
  3. 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
  1. root@ubuntu01:/# dotnet --info
  2. Host:
  3. Version: 8.0.5
  4. Architecture: x64
  5. Commit: 087e15321b
  6. RID: linux-x64
  7. .NET SDKs installed:
  8. No SDKs were found.
  9. .NET runtimes installed:
  10. Microsoft.AspNetCore.App 8.0.5 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  11. Microsoft.NETCore.App 8.0.5 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  12. Other architectures found:
  13. None
  14. Environment variables:
  15. Not set
  16. global.json file:
  17. Not found
  18. Learn more:
  19. https://aka.ms/dotnet/info
  20. Download .NET:
  21. https://aka.ms/dotnet/download
  22. root@ubuntu01:/#
二、运行程序

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


2.添加执行权限

chmod +x -R  /file/helloworld/

3.运行DotNet程序

dotnet helloworld.dll

  1. # 增加微软包安装源
  2. wget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
  3. sudo dpkg -i packages-microsoft-prod.deb
  4. rm packages-microsoft-prod.deb
  5. # 安装SDK(如果需要编译)
  6. sudo apt-get update && sudo apt-get install -y dotnet-sdk-8.0
  7. # 安装运行时
  8. sudo apt-get update && sudo apt-get install -y aspnetcore-runtime-8.0
  9. # 验证是否安装成功
  10. dotnet --info
  11. # 上传发布文件到linux服务器
  12. # 添加执行权限
  13. chmod +x -R /file/helloworld/
  14. # 运行DotNet程序
  15. dotnet helloworld.dll
三、使用systemctl管理

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

1.创建service文件

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

# 添加下面的内容

  1. [Unit]
  2. Description=helloworld .NET Web Application
  3. [Service]
  4. WorkingDirectory=/file/helloworld/
  5. ExecStart=/usr/bin/dotnet /file/helloword/helloworld.dll
  6. Restart=always
  7. # Restart service after 2 seconds if the dotnet service crashes:
  8. RestartSec=2
  9. KillSignal=SIGINT
  10. SyslogIdentifier=helloworld
  11. User=root
  12. Environment=ASPNETCORE_ENVIRONMENT=Production
  13. [Install]
  14. 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
  1. root@ubuntu01:/# systemctl status helloworld.service
  2. ● helloworld.service - helloworld .NET Web Application
  3. Loaded: loaded (/lib/systemd/system/helloworld.service; enabled; vendor preset: enabled)
  4. Active: active (running) since Wed 2024-05-29 17:39:48 CST; 1min 54s ago
  5. Main PID: 27708 (dotnet)
  6. Tasks: 15 (limit: 4558)
  7. Memory: 47.4M
  8. CPU: 1.921s
  9. CGroup: /system.slice/helloworld.service
  10. └─27708 /usr/bin/dotnet /file/helloworld/helloworld.dll
  11. May 29 17:39:48 ubuntu01 systemd[1]: Started helloworld.NET Web Application.
  12. May 29 17:39:48 ubuntu01 helloworld[27708]: * Start load configure...... Configure Load Sucessfull
  13. May 29 17:39:48 ubuntu01 helloworld[27708]: * Mysql connection Openning....
  14. May 29 17:39:48 ubuntu01 helloworld[27708]: Process with an Id of 26029 is not running.

5.查看进程

ps -ef | grep helloworld

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/897187
推荐阅读
相关标签
  

闽ICP备14008679号