【.NetCore学习】ubuntu16.04 搭建.net core mvc api 运行环境 【netcore基础】ubuntu 16.04 搭建.net core 2.1 linux 运行环境 nginx反向代理 supervisor配置自启动

查看linux内核版本

uname -a

打印结果

python@ubuntu:~$ uname -a
Linux ubuntu 4.4.0-31-generic #50-Ubuntu SMP Wed Jul 13 00:07:12 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

【.NetCore学习】ubuntu16.04 搭建.net core mvc api 运行环境
【netcore基础】ubuntu 16.04 搭建.net core 2.1 linux 运行环境 nginx反向代理 supervisor配置自启动

查看ubuntu系统版本

lsb_release -a

打印结果

python@ubuntu:~$ lsb_release -a
No LSB modules are available.
Distributor ID:    Ubuntu
Description:    Ubuntu 16.04.1 LTS
Release:    16.04
Codename:    xenial
python@ubuntu:~$ 

可以看到版本号是 16.4

查看官方文档执行 dotnet 环境安装过程

https://www.microsoft.com/net/learn/get-started/linuxredhat

 这里我们选择 Ubuntu

【.NetCore学习】ubuntu16.04 搭建.net core mvc api 运行环境
【netcore基础】ubuntu 16.04 搭建.net core 2.1 linux 运行环境 nginx反向代理 supervisor配置自启动

继续执行下面的命令

Register the trusted Microsoft signature key:

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-xenial-prod xenial main" > /etc/apt/sources.list.d/dotnetdev.list'

好了,环境部署完毕,下面我们安装 SDK

分别执行下面三行命令

sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install dotnet-sdk-2.1
 

【.NetCore学习】ubuntu16.04 搭建.net core mvc api 运行环境
【netcore基础】ubuntu 16.04 搭建.net core 2.1 linux 运行环境 nginx反向代理 supervisor配置自启动

至此,SDK安装完毕

 ---------------------------------------------------------------------------------------------------------------------

下面我们新建一个控制台项目测试一下

dotnet new console -o myApp
cd myApp
dotnet run

 遇到了一个目录权限问题

Template "Console Application" could not be created.
Access to the path '/etc/apt/trusted.gpg.d/myApp' is denied.

【.NetCore学习】ubuntu16.04 搭建.net core mvc api 运行环境
【netcore基础】ubuntu 16.04 搭建.net core 2.1 linux 运行环境 nginx反向代理 supervisor配置自启动

使用权限命令,添加目录的权限

然而这里,我们修改一下创建位置,改成当前的 home 目录创建即可

【.NetCore学习】ubuntu16.04 搭建.net core mvc api 运行环境
【netcore基础】ubuntu 16.04 搭建.net core 2.1 linux 运行环境 nginx反向代理 supervisor配置自启动

运行结果如下

python@ubuntu:~/dotnet$ dir
myApp
python@ubuntu:~/dotnet$ cd myApp/
python@ubuntu:~/dotnet/myApp$ dir
myApp.csproj  obj  Program.cs
python@ubuntu:~/dotnet/myApp$ dotnet run
Hello World!
python@ubuntu:~/dotnet/myApp$ 

编译运行成功了。

下面我们用在 windows 下编译好的 mvcapi 项目在 linux 上运行一下试试。

 首先我们把整个项目解决方案目录传到 linux 服务器上

【.NetCore学习】ubuntu16.04 搭建.net core mvc api 运行环境
【netcore基础】ubuntu 16.04 搭建.net core 2.1 linux 运行环境 nginx反向代理 supervisor配置自启动

 这里我用的是 xftp

【.NetCore学习】ubuntu16.04 搭建.net core mvc api 运行环境
【netcore基础】ubuntu 16.04 搭建.net core 2.1 linux 运行环境 nginx反向代理 supervisor配置自启动

然后要运行哪个项目,就直接切换到项目的目录,例如我要运行 mywebapi 这个api接口

python@ubuntu:~/dotnet/myweb$ cd mywebapi
python@ubuntu:~/dotnet/myweb/mywebapi$ ls
appsettings.Development.json  bin          mywebapi.csproj       obj         Properties  wwwroot
appsettings.json              Controllers  mywebapi.csproj.user  Program.cs  Startup.cs
python@ubuntu:~/dotnet/myweb/mywebapi$ dotnet run
Models/GEDU_LSA_V_1_7_1Context.cs(38,10): warning CS1030: #warning: 'To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.' [/home/python/dotnet/myweb/core/mydb.csproj]
Controllers/MyController.cs(24,13): warning CS0162: Unreachable code detected [/home/python/dotnet/myweb/mywebapi/mywebapi.csproj]
Using launch settings from /home/python/dotnet/myweb/mywebapi/Properties/launchSettings.json...
Hosting environment: Development
Content root path: /home/python/dotnet/myweb/mywebapi
Now listening on: http://localhost:61697
Application started. Press Ctrl+C to shut down.
^CApplication is shutting down...

【.NetCore学习】ubuntu16.04 搭建.net core mvc api 运行环境
【netcore基础】ubuntu 16.04 搭建.net core 2.1 linux 运行环境 nginx反向代理 supervisor配置自启动

 这个时候我们访问以下 api 接口地址

【.NetCore学习】ubuntu16.04 搭建.net core mvc api 运行环境
【netcore基础】ubuntu 16.04 搭建.net core 2.1 linux 运行环境 nginx反向代理 supervisor配置自启动

成功看到接口返回的json

mvc项目也是这样操作,即可。

这里只是简单的通过命令行运行,如果要作为服务器,还需要 反向代理 和 自启动 的高级配置

可参考如下博客