涉及: ASP.NET Core webapi Ubuntu 服务器 Nginx 代理
ASP.NET Core WebApi 开发
创建 ASP.NET Core WebApi 程序
创建一个简单的测试案例就可以了,
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- namespace Hubert.API.Demo.Controllers
- {
- public class DemoController : BaseController
- {
- // GET: API/Demo
- [HttpGet]
- public IEnumerable<string> Get()
- {
- return new string[] { "value1", "value2" };
- }
- // GET: API/Demo/5
- [HttpGet("{id}", Name = "Get")]
- public string Get(int id)
- {
- return "value";
- }
- // POST: API/Demo
- [HttpPost]
- public void Post([FromBody] string value)
- {
- }
- // PUT: API/Demo/5
- [HttpPut("{id}")]
- public void Put(int id, [FromBody] string value)
- {
- }
- // DELETE: API/ApiWithActions/5
- [HttpDelete("{id}")]
- public void Delete(int id)
- {
- }
- }
- }
BaseController 代码 这里可以写一些公共的方法参数等
- using Microsoft.AspNetCore.Mvc;
- namespace Hubert.API.Demo.Controllers
- {
- [Route("api/[controller]/[action]")]
- [ApiController]
- public class BaseController : ControllerBase
- {
- }
- }
发布 ASP.NET Core WebApi 程序
可以先测试一下 部署到 IIS 需要安装 AspNetCoreModule 并且应用池设置无托管即可;
部署 ASP.NET Core WebApi 到 Ubuntu 服务器
首先安装 ASP.NET Core SDK
- curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor> microsoft.gpg
- sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
- sudo sh -c 'echo"deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-bionic-prod bionic main"> /etc/apt/sources.list.d/dotnetdev.list'
- sudo apt-get install apt-transport-https
- sudo apt-get update
- sudo apt-get install dotnet-sdk-2.1.1
查看 DotNet 版本
dotnet --version
上传发布文件到 Ubuntu
创建文件夹命令 mkdir 上传文件 rz
上传成功之后 运行该程序
通过 Http://Ip 地址: 5005 测试访问
安装 Nginx 并对其做相应配置
安装 Nginx
sudo apt-get install nginx
查看 Nginx 版本
nginx -v
启动 nginx 或者重启 nginx
- service nginx start
- service nginx restart
如果端口号被占用则会提示
- nginx: [emerg] "server" directive is not allowed here in /etc/nginx/sites-enabled/default:21
- nginx: configuration file /etc/nginx/nginx.conf test failed
这个时候你需要 查看端口是否被占用
netstat -ntpl
杀死这个进程
kill 关于 80 端口的进程
然后在启动 nginx 就可以啦
创建一个关于 dotnetcore webapi 的配置文件; 我这里已经创建好了
输入命令 vi 编辑这个配置文件
vi hubert
还需要在 nginx 配置文件中引入 Hosts 文件夹的配置
在 http 里配置
include /etc/nginx/hosts/*;
然后重启 nginx
service nginx restart
通过域名访问
来源: https://www.cnblogs.com/HubertBiyo/p/9863506.html