linux asp.net core 开机自启动

Create the service definition file:

sudo nano /etc/systemd/system/kestrel-hellomvc.service

The following is an example service file for the app:

[Unit]
Description=Example .NET Web API App running on Ubuntu

[Service]
WorkingDirectory=/var/aspnetcore/hellomvc
ExecStart=/usr/bin/dotnet /var/aspnetcore/hellomvc/hellomvc.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Development

[Install]
WantedBy=multi-user.target

Save the file and enable the service.

systemctl enable kestrel-hellomvc.service

Start the service and verify that it's running.

systemctl start kestrel-hellomvc.service
systemctl status kestrel-hellomvc.service

You need to set WorkingDirectory - path to folder with your app and ExecStart - with path to your app dll. By default this is enough. 

发布 ASP.NET Core 应用程序至 Linux 生产环境的步骤如下: 1. 首先,安装 .NET Core 运行时和 SDK。在 Ubuntu 系统中,可以通过以下命令安装: ``` sudo apt-get update sudo apt-get install dotnet-runtime-3.1 sudo apt-get install dotnet-sdk-3.1 ``` 2. 然后,将应用程序打包成发布包。可以使用以下命令: ``` dotnet publish -c Release -r linux-x64 ``` 这将生成一个包含应用程序的可执行文件和所有依赖项的文件夹。 3. 将发布包上传至 Linux 服务器。 4.Linux 服务器上创建一个服务文件,以便在系统启动时自动启动应用程序。可以使用以下命令: ``` sudo nano /etc/systemd/system/myapp.service ``` 在编辑器中添加以下内容: ``` [Unit] Description=My ASP.NET Core Application [Service] WorkingDirectory=/path/to/published/folder ExecStart=/usr/bin/dotnet /path/to/published/folder/myapp.dll Restart=always # Restart service after 10 seconds if the dotnet service crashes: RestartSec=10 SyslogIdentifier=myapp User=<username> Environment=ASPNETCORE_ENVIRONMENT=Production [Install] WantedBy=multi-user.target ``` 其中,`WorkingDirectory` 是发布包的路径,`ExecStart` 是应用程序的启动命令,`User` 是应用程序运行的用户,`Environment` 是 ASP.NET Core 的运行环境。 5. 启动服务并设置开机自启动: ``` sudo systemctl start myapp sudo systemctl enable myapp ``` 现在,ASP.NET Core 应用程序已经成功发布至 Linux 生产环境。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值