【.NetCore學習】ubuntu16.04 搭建.net core mvc api 運行環境

查看linux內核版本html

uname -a

打印結果python

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

查看ubuntu系統版本linux

lsb_release -a

打印結果nginx

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.4web

查看官方文檔執行 dotnet 環境安裝過程json

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

 這裏咱們選擇 Ubuntuwindows

 

繼續執行下面的命令api

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
 

至此,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.

使用權限命令,添加目錄的權限

然而這裏,咱們修改一下建立位置,改爲當前的 home 目錄建立便可

運行結果以下

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 服務器上

 這裏我用的是 xftp

而後要運行哪一個項目,就直接切換到項目的目錄,例如我要運行 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...

 

 這個時候咱們訪問如下 api 接口地址

成功看到接口返回的json

mvc項目也是這樣操做,便可。

 

這裏只是簡單的經過命令行運行,若是要做爲服務器,還須要 反向代理 和 自啓動 的高級配置

可參考以下博客

 

【netcore基礎】ubuntu 16.04 搭建.net core 2.1 linux 運行環境 nginx反向代理 supervisor配置自啓動

相關文章
相關標籤/搜索