1、安裝.NET Core Runtime
2、上傳.net core文件
3、守護進程
4、安裝Nginx
5、配置Nginx
1、安裝.NET Core Runtime
首先需要安裝.NET Core Runtime: https://dotnet.microsoft.com/download/linux-package-manager/centos/runtime-current
進入上面網站,然后選擇CentOS,之后再右面可以看到CentOS 8 和CentOS 7的安裝命令,根據自己的CentOS版本選擇,如下圖:
2、上傳.net core文件
把編譯好的.net core 文件拷貝到CentOS服務器中(上傳工具很多,比如WinSCP,MobaXterm,文件路徑和上傳工具根據自己喜好選擇創建)
上傳成功后,進入對應目錄,可以使用 dotnet DockerCoreDemo.dll,運行程序,成功界面如下:
也可以指定端口號啟動,比如執行命令:dotnet DockerCoreDemo.dll --urls http://*:2677 運行程序,成功界面如下:
到這里我們的程序已經可以運行,並且同個網段的電腦也是可以訪問的,如下圖:(頁面比較簡單,大家知道怎么用就行了)
我們已經學會在Linux下部署asp .net core 程序了是不是很有自豪感;有同學會說了,這個啟動沒用的,遠程斷了,起的服務也就斷了,所以我們要做守護進程
3、守護進程
下面我們看下如何做守護進程:
首先在/etc/systemd/system 下新建文件(可以使用WinSCP,MobaXterm),文件名以.service結尾,配置內容為(注意:中文注釋需要刪掉,否則部分 Linux 服務器會報錯):
[Unit] Description=dockercoredemo #服務描述,隨便填就好 [Service] WorkingDirectory=/root/coredemo/DockerCoreDemo/bin/Debug/netcoreapp3.1 #工作目錄,填你應用的絕對路徑 ExecStart=/usr/bin/dotnet /root/coredemo/DockerCoreDemo/bin/Debug/netcoreapp3.1/DockerCoreDemo.dll --urls http://*:2677 #啟動:前半截是你dotnet的位置(一般都在這個位置),后半部分是你程序入口的dll,中間用空格隔開 注意:--urls http://*:2677是配置端口號 Restart=always RestartSec=25 #如果服務出現問題會在25秒后重啟,數值可自己設置 SyslogIdentifier=dockercoredemo #設置日志標識,此行可以沒有 User=root #配置服務用戶,越高越好 Environment=ASPNETCORE_ENVIRONMENT=Production [Install] WantedBy=multi-user.target
寫完配置文件后保存,輸入指令確認服務:
systemctl enable (你的服務名).service
例如:systemctl enable dockercoredemo.service;
然后啟動服務 systemctl start dockercoredemo.service
然后查看一下服務狀態:systemctl status dockercoredemo 出現狀態說明服務運行正常。
當然這只是基礎,服務我們肯定是要做負載均衡,才能保證程序的健壯性,下面我們看怎么做負載均衡:
4、安裝Nginx
安裝Nginx:
sudo yum install nginx
安裝完成后,啟用並啟動Nginx服務:
sudo systemctl enable nginx sudo systemctl start nginx
啟動成功后,直接瀏覽器訪問你的 IP 地址,如果看到如下效果說明安裝成功:
Centos 7下安裝nginx,使用yum install nginx,提示沒有可用的軟件包,可以看這里:https://www.cnblogs.com/sxw117886/p/13625897.html
5、配置Nginx
修改 nginx.conf 配置代理:
然后我們再回來配置Nginx,進入 /etc/nginx,然后 vi 修改nginx.conf ,保存
upstream dotnetblogserver1 { server 127.0.0.1:2677; server 127.0.0.1:2678; } server { listen 8881; server_name core.idotnet5.com; location /{ proxy_pass http://dotnetblogserver1; index index.html index.htm; } location = /50x.html { root html; } }
完整nginx.conf為

# For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; upstream dotnetblogserver1 { server 127.0.0.1:2677; server 127.0.0.1:2678; } server { listen 8881; server_name core.idotnet5.com; location /{ proxy_pass http://dotnetblogserver1; index index.html index.htm; } location = /50x.html { root html; } } # Settings for a TLS enabled server. # # server { # listen 443 ssl http2 default_server; # listen [::]:443 ssl http2 default_server; # server_name _; # root /usr/share/nginx/html; # # ssl_certificate "/etc/pki/nginx/server.crt"; # ssl_certificate_key "/etc/pki/nginx/private/server.key"; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 10m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # # # 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:
然后執行sudo nginx -s reload 來重啟nginx。
從配置文件中可以看出,我們配置了兩個服務地址,分別是
server 127.0.0.1:2677; server 127.0.0.1:2678;
這就是說明Nginx監聽這本服務2677和2678這兩個端口號,而nginx自身的端口號是8881;
然后我們按照步驟3的守護進程,起兩個對應端口的服務;
然后在其他機器上訪問你的服務地址:8881,如192.168.101.129:8881 查看效果,如下圖:
至此,我們已經學會在linux CentOS下部署Asp .Net Core並且也可以用Nginx做負載均衡了。
Ubutu下安裝時一樣的道理,大家可以模仿操作。