1.创建默认的asp.netcore webapi的项目
2.在appsetting.josn文件中添加节点更改默认端口
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://192.168.188.128:6666"
}
}
}
}
3.生成项目并发布到文件夹
ps:选择独立框架,linux-64
发布后的文件结构如下,这里linux-x64和publish文件夹下的文件内容是一样的,把linux-64文件夹内容复制到linux服务其上
4.部署
我这的复制到:/Home/code/11 目录下(路径随便选择)
5.使用ssh终端连接linux服务器切换到项目的根文件目录
输入执行项目名称
./linuxpublish
这里会提示没有访问执行权限,我们需要把执行权限加上
6.在postman上访问
7.添加守护进程
Linux的Daemon管理路径:
cd /etc/systemd/system
新建一个service配置文件:
vi my-auth.service
vi编辑器中编写的配置信息:
[Unit]
Description=Auth .NET Web API App running on Ubuntu
[Service]
#发布代码文件路径
WorkingDirectory=/home/code
#启动项目命令
ExecStart=dotnet /home/code/linuxpublish.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
#服务标识
SyslogIdentifier=my-auth
User=root
#生产环境变量
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target
退出并保存vi
:wq
刷新daemon让配置生效
systemctl daemon-reload
查看所有.net core进程
ps -ef|grep dotnet
查看服务状态
systemctl status my-auth
服务正在运行,但未启用。 “Enabled”表示在服务器启动后自动启动。 这是所需的最终配置
sudo systemctl enable my-auth.service
停止或重启服务
systemctl stop my-auth
systemctl restart my-auth