1. 首页>
  2. 技术文章>
  3. netcore发布为独立部署在linux上的运行

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

8/19/22 2:21:31 PM 浏览 2143 评论 0

netcore

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

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

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

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

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

./MyDotNetCore.Project.WebApi --urls http://*:5001

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

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

[Unit]
Description=api服务
[Service]
WorkingDirectory=/mnt/hgfs/vmware
ExecStart='/mnt/hgfs/vmware/MyDotNetCore.Project.WebApi' --urls http://*:5001
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就可以了

systemctl enable api.service

启动该服务

systemctl start api.service

查看服务状态

systemctl status api.service

重启服务

systemctl restart api.service

查看日志,/var/log/message

journalctl -fu api.service

关闭服务

systemctl stop api.service


网友讨论