在Centos7上自启动AspNet Core程序

1. 上传文件

假设AspNet Core项目名为xxx,开发完成后发布到文件夹,然后把整个PublishOutput文件夹的文件复制到Centos7的目录/var/aspnetcore/xxx。修改目录的owner,因为后面使用nginx用户自启动,所以owner修改为nginx:

chown -R nginx:nginx /var/aspnetcore/xxx

2. 创建自启动文件

新建文件

vi /etc/systemd/system/xxx.service

内容如下:

[Unit]
Description=xxx program on centos7

[Service]
WorkingDirectory=/var/aspnetcore/xxx
ExecStart=/usr/bin/dotnet /var/aspnetcore/xxx/xxx.dll
Restart=always
RestartSec=10  # Restart service after 10 seconds if dotnet service crashes
SyslogIdentifier=dotnet-xxx
User=nginx
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

3. 使自启动生效

systemctl enable xxx.service

4. 立即启动服务

systemctl start xxx.service

5. 查看服务状态

systemctl status xxx.service

你可能感兴趣的:(在Centos7上自启动AspNet Core程序)