[.NET Core] 將 NET Core 部署至 Ubuntu Server 16.04

最近裝了個Linux Server想來玩玩看NET Core並佈署到上面,結果發現好多眉眉角角,網路上的文件似乎有點舊讓我一直撞牆撞不停,經過了幾個小時的煎熬最後終於成功了,這裡筆記一下方便下次使用…

首先按照 官網 提供的步驟進行安裝如下:

  1. 加入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
  2. 開始安裝
    sudo apt-get install dotnet-dev-1.0.4
  3. 建立一個空資料夾
    mkdir HelloWebApp
    cd HelloWebApp
  4. 初始化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
  5. 檢查套件是否遺失並安裝
    dotnet restore
  6. 安裝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
  7. 設定000-default.conf
    <VirtualHost *:80>
    	ProxyPreserveHost On
    	ProxyPass / http://127.0.0.1:5000/
    	ProxyPassReverse / http://127.0.0.1:5000/
    </VirtualHost>
  8. 重新啟動Apach並啟動.NET Core專案
    sudo service apache2 restart
    dotnet run
  9. 完成 !!
    2017-03-14-00_24_31-home-page-hellowebapp

 

 

  • 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

     

來源:

  1. https://www.microsoft.com/net/core#linuxubuntu
  2. How To Use Apache HTTP Server As Reverse-Proxy Using mod_proxy Extension
  3. Asp.Net Core 发布和部署( MacOS + Linux + Nginx )
  4. Publish to a Linux Production Environment
  5. Using Apache web server as a reverse proxy


這裡的資訊對您有用嗎?歡迎斗內給我