這一篇實體記錄一次用Centos7創建并部署.net core專案的程序,希望能幫到用到的小伙伴,
Kestrel 是 ASP.NET Core 專案模板中包括的默認 Web 服務器,Kestrel可以用作邊緣服務器,同時Kestrel也可以做反向代理配置

一.創建并運行.net core MVC專案
1.用命令創建一個.net core MVC專案(前提是安裝了.net core SDK)
dotnet new mvc -n "Test" (-n引數是指定專案的名稱)

2.進入專案并運行(必須要先進入創建的專案再執行命令)
dotnet run

3.在瀏覽器中查看效果
這個錯誤主要是https的安全證書問題,再這里我們先簡單的修改專案的配置,使其正常的跑起來,

4.修改專案中Properties下面的launchSettings.json檔案

5.重新運行專案就可以正常打開了

6.發布專案
dotnet publish --configuration Release
二.使用 Apache 在 Linux 上托管 ASP.NET Core
1.安裝Apache
yum install httpd

2.配置 Apache
Apache 的組態檔位于 /etc/httpd/conf.d/ 目錄內, 除了 /etc/httpd/conf.modules.d/ 中的模塊組態檔外(其中包含加載模塊所需的任何組態檔),將對任何帶 .conf 擴展名的檔案按字母順序進行處理,
在/etc/httpd/conf.d/目錄下為應用創建名為 Test.conf 的組態檔,配置如下:
<VirtualHost *:*> RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME} </VirtualHost> <VirtualHost *:80> ProxyPreserveHost On ProxyPass / http://127.0.0.1:5000/ ProxyPassReverse / http://127.0.0.1:5000/ ServerName www.example.com ServerAlias *.example.com ErrorLog ${APACHE_LOG_DIR}helloapp-error.log CustomLog ${APACHE_LOG_DIR}helloapp-access.log common </VirtualHost>
3.保存檔案并測驗配置
sudo service httpd configtest

4.為專案創建一個服務檔案
vim /etc/systemd/system/kestrel-Test.service
配置內容如下:
[Unit] Description=Example .NET Web API App running on CentOS 7
[Service] WorkingDirectory=/var/dotnet/Test ExecStart=/usr/bin/dotnet /var/dotnet/Test/bin/Release/netcoreapp2.2/Test.dll Restart=always RestartSec=10 KillSignal=SIGINT SyslogIdentifier=dotnet-example User=apache Environment=ASPNETCORE_ENVIRONMENT=Production [Install] WantedBy=multi-user.target

5.啟動服務和apache服務
systemctl start kestrel-Test.service
systemctl start httpd

此時訪問本地的80埠就會發現已經轉發到.net core專案上了
脫坑指南
當配置完/etc/conf.d/Test.conf時,發現apache起不來了,這時請檢查你的SELinux是否開著,開著的話關閉,重啟服務就好了
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/163246.html
標籤:Linux
上一篇:Ubuntu18.04安裝配置
