課程鏈接:http://video.jessetalk.cn/course/explore
良心課程,大家一起來學習哈!
任務1:課程介紹
- 1、介紹與引入
- 2、配置管理
- 3、依賴注入
- 4、ASP.NET Core HTTP介紹
- 5、認證與授權
- 6、ASP.NET Core MVC
任務2:環境安裝
下載地址:https://dotnet.microsoft.com/download
通過 visualstudio 安裝:菜單欄上點擊工具--獲取工具和功能(T),勾選 .NET CORE
打開 PowerShell(管理員),輸入以下命令驗證是否安裝成功
PS C:\WINDOWS\system32> dotnet
任務3:在控制台創建ASP.NET Core應用程序
查看模板:
PS C:\WINDOWS\system32> dotnet new --help
新建項目 ASP.NET Core Web App (Model-View-Controller)
PS D:\jessetalk> mkdir aspdotnetcore
PS D:\jessetalk> cd .\aspdotnetcore\
PS D:\jessetalk\aspdotnetcore> dotnet new mvc
啟動項目
PS D:\jessetalk\aspdotnetcore> dotnet run
瀏覽器訪問:https://localhost:5001/
任務4:在VS中創建ASP.NET Core應用程序
在 VS 中通過 IIS Express 啟動
官方推薦在開發和測試的時候通過控制台啟動,因為通過控制台啟動之后,日志會自動輸出
任務5:部署到IIS
Windows10如何安裝IIS:https://jingyan.baidu.com/article/eb9f7b6d9e73d1869364e8d8.html
下載 ASP.NET Core Module:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/aspnet-core-module?view=aspnetcore-2.2
安裝完成后在 iis 的模塊下面可以找到以下模塊
在 iis 中添加網站,停掉 Default 網站,新建文件夾sites,在sites里面新建文件夾aspdotnetcoredemo
在應用程序池中修改 .NET CLR 版本,不然無法運行,因為托管代碼指代碼編譯成IL代碼后在dotnet framework下運行,aspdotnetcore需要修改為無托管代碼才可以運行
在控制台發布
PS D:\jessetalk\aspdotnetcore> dotnet publish
發布結果
發布到指定目錄
PS D:\jessetalk\aspdotnetcore> dotnet publish -o D:\jessetalk\sites\aspdotnetcoredemo
瀏覽發布的網站
通過 VS 發布:
清空該目錄下面的內容:D:\jessetalk\sites\aspdotnetcoredemo,刷新瀏覽器
在 VS 中,項目右鍵--發布,通過文件夾發布
刷新瀏覽器
任務6:准備CentOS和Nginx環境
下載 vmware workstation
官網:https://www.vmware.com/products/workstation-pro/workstation-pro-evaluation.html
下載 CentOS,選擇 Minimal ISO
官網:https://www.centos.org/download/
第一次啟動可能出現VMware與 Device/Credential Guard 不兼容 :https://jingyan.baidu.com/article/9f63fb916b50e1c8400f0ebf.html
手動安裝組件:
網卡
修改為 yes
重啟網卡
安裝 ifconfig 命令
得到 ip 地址 192.168.204.128 之后可以通過 putty 連接(SSH方式)
putty下載地址:https://putty.org/
使用 putty 連接 cenos 之后可以方便復制粘貼命令
安裝 Nginx:https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-centos-7
安裝完成后瀏覽器訪問:192.168.204.128
任務7:在CentOS上安裝.NET Core運行時
cenos 安裝 asp .net core 環境:https://www.asp.net/core/overview/aspnet-vnext
安裝后驗證:
[root@localhost ~]# dotnet
[root@localhost ~]# dotnet --version
[root@localhost ~]# dotnet new --help
[root@localhost ~]# cd /
[root@localhost /]# cd home
[root@localhost home]# mkdir netcore
[root@localhost home]# mkdir helloCore
[root@localhost home]# cd helloCore
[root@localhost helloCore]# dotnet new webapi
[root@localhost helloCore]# dotnet run
通過另一個 putty 連接訪問
[root@localhost ~]# curl http://localhost:5000/api/values
任務8:部署到CentOS
下載 FileZilla :https://filezilla-project.org/
通過 ftp 將之前發布在 sites 目錄下的文件上傳到 cenos 上的 netcore 文件夾里
啟動網站
[root@localhost home]# cd netcore
[root@localhost netcore]# ls
appsettings.Development.json aspdotnetcore.pdb web.config
appsettings.json aspdotnetcore.runtimeconfig.json wwwroot
aspdotnetcore.deps.json aspdotnetcore.Views.dll
aspdotnetcore.dll aspdotnetcore.Views.pdb
[root@localhost netcore]# dotnet aspdotnetcore.dll
Hosting environment: Production
Content root path: /home/netcore
Now listening on: http://localhost:5000
Now listening on: https://localhost:5001
Application started. Press Ctrl+C to shut down.
通過另一個 putty 訪問
[root@localhost ~]# curl http://localhost:5000
使用 nginx 將 80 端口 映射到 5000 端口 下
[root@localhost ~]# cd /etc/nginx
[root@localhost nginx]# ls
conf.d koi-utf scgi_params
default.d koi-win scgi_params.default
fastcgi.conf mime.types uwsgi_params
fastcgi.conf.default mime.types.default uwsgi_params.default
fastcgi_params nginx.conf win-utf
fastcgi_params.default nginx.conf.default
[root@localhost nginx]# vi nginx.conf
注釋掉 nginx.conf 文件中默認 80 的 server
# server {
# listen 80 default_server;
# listen [::]:80 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
nginx.conf 文件中將所有 .conf 文件引入進來
# include /etc/nginx/default.d/*.conf;
修改 netcore.conf
[root@localhost nginx]# cd conf.d
[root@localhost nginx]# vi netcore.conf
netcore.conf
server {
listen 80;
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
重啟 nginx
[root@localhost conf.d]# nginx -s reload
[root@localhost conf.d]# systemctl restart nginx
瀏覽器訪問:http://192.168.204.128/
本作品采用知識共享署名-非商業性使用-相同方式共享 4.0 國際許可協議進行許可。
歡迎轉載、使用、重新發布,但務必保留文章署名 鄭子銘 (包含鏈接: http://www.cnblogs.com/MingsonZheng/ ),不得用於商業目的,基於本文修改后的作品務必以相同的許可發布。
如有任何疑問,請與我聯系 (MingsonZheng@outlook.com) 。