netcore发布为独立部署在linux上的运行

# Ubuntu 22.04 及更高版本下安装dotnet sdk
sudo apt-get update
sudo apt-get install -y dotnet-sdk-8.0
sudo apt-get install -y dotnet-sdk-6.0

独立部署的方式,发布命令为:

1

dotnet publish -r linux-x64 -o F:\publish

如果是依赖框架的方式,那么命令为:

1

dotnet publish -r linux-x64 --self-contained false -o /my/publish/test-api/

文件拷贝过去后,运行命令为:

1

./GateWayServer test

我们一般可以在Linux服务器上执行 dotnet 命令来运行我们的.net Core WebApi应用。但是这样运行起来的应用很不稳定,关闭终端窗口之后,应用也会停止运行。为了让其可以稳定运行,我们需要让它变成系统的守护进程,成为一种服务一直在系统中运行,出现异常时也能重新启动。Linux系统有自己的守护进程管理工具 Systemd 。systemd 是内核启动后的第一个用户进程,PID 为1,是所有其它用户进程的父进程。它直接与原创内核交互,性能出色,可以提供用于启动、停止和管理进程的许多强大的功能。我们完全可以将程序交给 Systemd ,让系统统一管理,成为真正意义上的系统服务。systemctl 用于管理 systemd 的行为,替换之前的 sysvinit 和 upstart。

我们新建一个文件:/etc/systemd/system/GateWayServer.service,内容如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

[Unit]

Description=api服务

[Service]

WorkingDirectory=/usr/dotnet/GateWayServer/

ExecStart='/usr/dotnet/GateWayServer/GateWayServer' test

Restart=always

# Restart service after 10 seconds if the dotnet service crashes:

RestartSec=10

KillSignal=SIGINT

SyslogIdentifier=api

User=root 

[Install]

WantedBy=multi-user.target

保存该文件并启用该服务,开机启动,如果想停止开机启动那么将enable改成disable就可以了

1

systemctl enable GateWayServer.service

启动该服务

1

systemctl start GateWayServer.service

查看服务状态

1

systemctl status GateWayServer.service

重启服务

1

systemctl restart GateWayServer.service

查看日志,/var/log/message

1

journalctl -fu GateWayServer.service

关闭服务

1

systemctl stop GateWayServer.service

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Centos上部署.netcore项目可以使用nohup和supervisor两种方式: 1. nohup方式: 使用nohup命令可以在后台运行.NET Core应用程序,使其在退出终端或SSH会话时继续运行。以下是使用nohup命令部署.NET Core应用程序的步骤: 1.1.安装.NET Core SDK和运行时环境 1.2.在服务器上创建应用程序目录,并将.NET Core应用程序发布到该目录中。 1.3.使用以下命令启动应用程序: ``` nohup dotnet /path/to/your/application.dll & ``` 以上命令中,/path/to/your/application.dll是应用程序的路径,&符号表示将应用程序放在后台运行。 2. supervisor方式: supervisor是一个进程控制系统,它可以管理和监视系统上运行的进程。以下是使用supervisor部署.NET Core应用程序的步骤: 2.1.安装.NET Core SDK和运行时环境 2.2.安装supervisor: ``` sudo yum install supervisor ``` 2.3.创建一个新的supervisor配置文件: ``` sudo nano /etc/supervisord.d/yourapp.ini ``` 2.4.将以下内容添加到配置文件中: ``` [program:yourapp] command=/usr/bin/dotnet /path/to/your/application.dll directory=/path/to/your/application autostart=true autorestart=true stderr_logfile=/var/log/yourapp.err.log stdout_logfile=/var/log/yourapp.out.log user=<your_username> environment=ASPNETCORE_ENVIRONMENT=Production ``` 以上配置文件中,/path/to/your/application.dll是应用程序的路径,/path/to/your/application是应用程序的目录,<your_username>是你的用户名。 2.5.重新加载supervisor配置文件: ``` sudo supervisorctl reread sudo supervisorctl update ``` 2.6.启动应用程序: ``` sudo supervisorctl start yourapp ``` 以上步骤完成后,你的.NET Core应用程序将启动并在supervisor监视下运行。你可以使用以下命令检查应用程序的状态: ``` sudo supervisorctl status yourapp ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

.net亦洪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值