Linux上部署NetCore服务

2020-12-17  乐帮网

service centos

本例以CentOS为例,详细记录一下怎么在Linux里部署netcore开发的服务程序。在这之前有三个基础知识先了解下,

(1) netcore中window 下可执行文件即入口程序是exe结尾的程序,在linux下入口刚在dll文件中,请找到服务程序的入口,本例为:vkt.demo.workerservice.dll
(2) netcore需要提前配置环境变量,当没有配置时刚默认为生产环境即Production。相关知识可以参考我之前写的:https://lebang2020.cn/details/201019oxa52ktu.html
(3)CentOS服务器需要提前安装netcore运行环境。(可参考官网)

以下为详细部署过程

1、把我们编译好的程序复制到CentOS服务器上,我复制到:/home/vktservice/demo。如果你上传文件的账号不是root,那就需要你对root添加一下执行权限,后面命令行都是在root下执行。

[root@test demo]# chmod -R 775 /home/vktservice/demo/

2、新建服务配置文件

在CentOS服务器的命令行中执行如下命令创建并编辑文件vktdemo.service:

[root@test]# cd /usr/lib/systemd/system
[root@test]# sudo touch vktdemo.service
[root@test]# vi vktdemo.service

然后粘贴内容如下:

[Unit]
Description=vkt demo service
After=network.target

[Service]
Type=notify
User=root
WorkingDirectory=/home/vktservice/demo
ExecStart=/usr/bin/dotnet /home/vktservice/demo/vkt.demo.workerservice.dll
# ensure the service restarts after crashing
Restart=always
# amount of time to wait before restarting the service              
RestartSec=5
# copied from dotnet documentation at
# https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-3.1#code-try-7
KillSignal=SIGINT
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

然后在当前目录执行命令添加到服务并且启动服务

[root@test system]# systemctl daemon-reload
[root@test system]# systemctl daemon-reload
[root@test system]# systemctl enable vktdemo
[root@test system]# systemctl start  vktdemo
[root@test system]# systemctl status vktdemo
● vktdemo.service - vkt demo service
   Loaded: loaded (/usr/lib/systemd/system/vktdemo.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-12-17 09:08:39 UTC; 2min 47s ago
 Main PID: 3858232 (dotnet)
    Tasks: 14 (limit: 50809)
   Memory: 24.2M
   CGroup: /system.slice/vktdemo.service
           └─3858232 /usr/bin/dotnet /home/vktservice/demo/vkt.demo.workerservice.dll

Dec 17 09:08:39 vkt01 systemd[1]: Starting vkt demo service...
Dec 17 09:08:39 vkt01 systemd[1]: Started vkt demo service.

再查看我们的log目录下的日志。

[root@test system]# cd /home/vktservice/demo/Logs
[root@test Logs]# ls
20201217_Trace.log
[root@test Logs]# cat 20201217_Trace.log 
2020-12-17 09:08:39 INFO: Worker started at: 12/17/2020 9:08:39 AM
2020-12-17 09:08:39 INFO: BeanProvider Running...
2020-12-17 09:08:39 INFO: Application started. Hosting environment: Production; Content root path: /home/vktservice/demo
2020-12-17 09:08:40 INFO: BeanProvider Running...
2020-12-17 09:08:41 INFO: BeanProvider Running...
2020-12-17 09:08:42 INFO: BeanProvider Running...

卸载服务

[root@test Logs]# systemctl stop vktdemo
[root@test Logs]# systemctl disable vktdemo
Removed /etc/systemd/system/multi-user.target.wants/vktdemo.service.
[root@test Logs]# rm -rf /etc/systemd/system/$vktdemo
[root@test Logs]# rm -rf /etc/systemd/system/vktdemo.service
[root@test Logs]# systemctl daemon-reload

至此我们的CentOS下的服务测试完成!

开发教程在这里:NetCore 开发服务 (Windows Linux) 

相关链接:

Failed to execute command: Permission denied

Process could not be executed Exec format error

原创作品,欢迎指正,在微信公众号可直接留言,禁止转载!

公众号二维码

关注我的微信公众号
在公众号里留言交流
投稿邮箱:1052839972@qq.com

庭院深深深几许?杨柳堆烟,帘幕无重数。
玉勒雕鞍游冶处,楼高不见章台路。
雨横风狂三月暮。门掩黄昏,无计留春住。
泪眼问花花不语,乱红飞过秋千去。

欧阳修

付款二维码

如果感觉对您有帮助
欢迎向作者提供捐赠
这将是创作的最大动力