最近裝了個Linux Server想來玩玩看NET Core並佈署到上面,結果發現好多眉眉角角,網路上的文件似乎有點舊讓我一直撞牆撞不停,經過了幾個小時的煎熬最後終於成功了,這裡筆記一下方便下次使用…
首先按照 官網 提供的步驟進行安裝如下:
- 加入apt-get來源
sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ xenial main" > /etc/apt/sources.list.d/dotnetdev.list' sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 417A0893 sudo apt-get update
- 開始安裝
sudo apt-get install dotnet-dev-1.0.4
- 建立一個空資料夾
mkdir HelloWebApp cd HelloWebApp
- 初始化MVC專案
dotnet new mvc
Templates清單:
Templates Short Name Language Tags ---------------------------------------------------------------------- Console Application console [C#], F# Common/Console Class library classlib [C#], F# Common/Library Unit Test Project mstest [C#], F# Test/MSTest xUnit Test Project xunit [C#], F# Test/xUnit ASP.NET Core Empty web [C#] Web/Empty ASP.NET Core Web App mvc [C#], F# Web/MVC ASP.NET Core Web API webapi [C#] Web/WebAPI Solution File sln Solution
- 檢查套件是否遺失並安裝
dotnet restore
- 安裝Apache反向代理(Reverse-Proxy)
sudo a2enmod proxy sudo a2enmod proxy_http sudo a2enmod proxy_ajp sudo a2enmod rewrite sudo a2enmod deflate sudo a2enmod headers sudo a2enmod proxy_balancer sudo a2enmod proxy_connect sudo a2enmod proxy_html
- 設定000-default.conf
<VirtualHost *:80> ProxyPreserveHost On ProxyPass / http://127.0.0.1:5000/ ProxyPassReverse / http://127.0.0.1:5000/ </VirtualHost>
- 重新啟動Apach並啟動.NET Core專案
sudo service apache2 restart dotnet run
- 完成 !!
- 2017/05/24 開啟啟動&背景執行
新增:/etc/systemd/system/kestrel-hellomvc.service
[Unit] Description = https://blog.exfast.me/ [Service] WorkingDirectory = /home/user/HelloWebApp/ ExecStart = /usr/bin/dotnet run /home/user/HelloWebApp/HelloWebApp.dll Restart = always # Restart service after 10 seconds if dotnet service crashes RestartSec = 10 SyslogIdentifier = dotnet-example User = user Environment = ASPNETCORE_ENVIRONMENT = Production [Install] WantedBy = multi-user.target
指令:
sudo systemctl stop kestrel-hellomvc sudo systemctl disable kestrel-hellomvc sudo systemctl enable kestrel-hellomvc sudo systemctl start kestrel-hellomvc sudo systemctl status kestrel-hellomvc
來源: