搜尋
首頁運維NginxCentos7系統下如何搭建.NET Core2.0+Nginx+Supervisor環境

Centos7系統下如何搭建.NET Core2.0+Nginx+Supervisor環境

May 12, 2023 pm 08:13 PM
nginxcentos7supervisor

一、linux .net core簡介

 一直以來,微軟只對自家平台提供.net支持,這樣等於讓這個「理論上」可以跨平台的框架在linux和macos上的支援只能由第三方專案提供(如mono .net)。

直到微軟推出完全開源的.net core。這個開源的平台相容.net  standard,並且能在windows、linux和macos上提供完全一致的api。雖然這個小巧的.net框架只是標準.net的一個子集,但已經相當強大了。

一方面,這個小巧的框架可以讓某些功能性應用同時運行在三個平台上(就像某些功能性的python腳本一樣),另一方面,這也可以讓伺服器運維人員將asp .net服務程式部署在linux伺服器上(特別是對於執行windows server較為吃力的伺服器)。

二、linux .net core2.0 環境部署前準備

1.環境說明:

伺服器系統:centos 7.2. 1511

 2.安裝前準備(關閉防火牆、關閉selinux)

1)關閉firewall:

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)

 2)關閉selinux

sed -i "s/selinux=enforcing/selinux=disabled/g" /etc/selinux/config

查看改後檔案如下:

[root@localhost ~]# cat /etc/selinux/config 
 
# this file controls the state of selinux on the system.
# selinux= can take one of these three values:
#   enforcing - selinux security policy is enforced.
#   permissive - selinux prints warnings instead of enforcing.
#   disabled - no selinux policy is loaded.
selinux=disabled
# selinuxtype= can take one of three two values:
#   targeted - targeted processes are protected,
#   minimum - modification of targeted policy. only selected processes are protected. 
#   mls - multi level security protection.
selinuxtype=targeted

3)重啟centos

reboot

#三、centos 部署.net core2.0 環境

1.添加dotnet產品

在安裝.net核心之前,您需要註冊微軟產品提要。這只需要做一次。首先,註冊微軟簽署金鑰,然後新增微軟產品提要。

rpm --import https://packages.microsoft.com/keys/microsoft.asc                   
sh -c 'echo -e "[packages-microsoft-com-prod]nname=packages-microsoft-com-prod nbaseurl=https://packages.microsoft.com/yumrepos/microsoft-rhel7.3-prodnenabled=1ngpgcheck=1ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/dotnetdev.repo'

2.安裝.net核心sdk

在下一步之前,請從您的系統中刪除.net .net先前的任何預覽版本。

以下指令更新用於安裝的產品列表,安裝.net核心所需的元件,然後安裝.net核心sdk。

yum update
yum install libunwind libicu -y
yum install dotnet-sdk-2.0.0 -y

3.檢查dotnet是否安裝成功與版本查看

dotnet --info
dotnet --version

四、測試.net core2.0 環境

1.在home目錄下初始化一個測試環境並輸出」hello world 「內容(測試方式一,可忽略)

cd /home
dotnet new console -o hwapp
cd hwapp
dotnet run

輸出空內容如下:

[root@localhost hwapp]# dotnet run
hello world!

2.上傳.net core的實例頁面進行測試(測試方式二、推薦)

centos 下.net core 2 環境測試案例(把它上傳到/home目錄下或自訂的目錄)

下載位址:

http://down.51cto.com/data/2334968

執行下列指令

cd /home/webapplication1
dotnet restore  //如果使过用测试方式一,就需先执行这命令重新加载一下当前新的网站文件
dotnet run

執行後如下圖:

## 

Centos7系统下如何搭建.NET Core2.0+Nginx+Supervisor环境

 透過ie存取測試頁

  

Centos7系统下如何搭建.NET Core2.0+Nginx+Supervisor环境

#五、安裝設定nginx對asp.net core應用的轉送

#1.安裝nginx環境

[root@localhost ~]#curl -o nginx.rpm http://nginx.org/packages/centos/7/noarch/rpms/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@localhost ~]#rpm -ivh nginx.rpm
[root@localhost ~]#yum install nginx -y

輸入:systemctl start nginx 來啟動nginx。

[root@localhost ~]# systemctl start nginx

輸入:systemctl enable nginx 來設定nginx的開機啟動(linux宕機、重啟會自動運行nginx不需要連上去輸入命令)

[root@localhost ~]#systemctl enable nginx
created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

2.透過ie檢查能否訪問

[root@localhost nginx-1.8.1]# ps -ef|grep nginx
root   14626   1 0 08:47 ?    00:00:00 nginx: master process nginx
nginx   14627 14626 0 08:47 ?    00:00:00 nginx: worker process
root   14636  3269 0 08:49 pts/1  00:00:00 grep --color=auto nginx

Centos7系统下如何搭建.NET Core2.0+Nginx+Supervisor环境

nginx常用的操作指令

systemctl start nginx.service               #開始#nginx服務

##ctln nginx.service             #設定啟動自啟動

systemctl disable nginx.service           狀態

systemctl restart nginx. service           #重新啟動服務

systemctl list-units –type=service        #檢視所有已啟動的服務

4.防火牆設定(如果系統有使用就需要進行寫入規則)##4.防火牆配置就需要進行寫入規則。

#指令:firewall-cmd –zone=public –add-port=80/tcp –permanent(開放80埠)

指令:systemctl restart firewalld(重啟防火牆以使設定即時生效)

5.設定nginx對asp.net core應用的轉送

修改/etc/nginx/conf.d/default.conf 檔案。

將檔案內容替換為

server {
  listen 80;
  location / {
    proxy_pass http://localhost:88;
    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;
  }
}

 重新載入nignx

[root@localhost nginx]# nginx -s reload

nginx的設定己完成

6.開啟dotnet run進行測試

[root@localhost ~]# cd /home/webapplication1/
[root@localhost webapplication1]# dotnet run
using launch settings from /home/webapplication1/properties/launchsettings.json...
hosting environment: development
content root path: /home/webapplication1
now listening on: http://[::]:88
application started. press ctrl+c to shut down.

透過ip 80連接埠存取

六、設定守護服務(supervisor)

Centos7系统下如何搭建.NET Core2.0+Nginx+Supervisor环境

目前有三個問題

问题1:asp.net core应用程序运行在shell之中,如果关闭shell则会发现asp.net core应用被关闭,从而导致应用无法访问,这种情况当然是我们不想遇到的,而且生产环境对这种情况是零容忍的。

问题2:如果asp.net core进程意外终止那么需要人为连进shell进行再次启动,往往这种操作都不够及时。

问题3:如果服务器宕机或需要重启我们则还是需要连入shell进行启动。

为了解决这个问题,我们需要有一个程序来监听asp.net core 应用程序的状况。在应用程序停止运行的时候立即重新启动。这边我们用到了supervisor这个工具,supervisor使用python开发的。

1.安装supervisor

[root@localhost /]# yum install python-setuptools -y
[root@localhost /]#easy_install supervisor

2.配置supervisor

[root@localhost /]#mkdir /etc/supervisor
[root@localhost /]#echo_supervisord_conf > /etc/supervisor/supervisord.conf

修改supervisord.conf文件,将文件尾部的配置

[root@localhost /]# vi /etc/supervisor/supervisord.conf

将里面的最后两行:

;[include]                          
;files = relative/directory/*.ini

 改为

[include]
files = conf.d/*.conf

ps:如果服务已启动,修改配置文件可用“supervisorctl reload”命令来使其生效

3.配置对asp.net core应用的守护

创建一个 webapplication1.conf文件,内容大致如下

[root@localhost /]# vi webapplication1.conf
[program:webapplication1]
command=dotnet webapplication1.dll ; 运行程序的命令
directory=/home/webapplication1/ ; 命令执行的目录
autorestart=true ; 程序意外退出是否自动重启
stderr_logfile=/var/log/webapplication1.err.log ; 错误日志文件
stdout_logfile=/var/log/webapplication1.out.log ; 输出日志文件
environment=aspnetcore_environment=production ; 进程环境变量
user=root ; 进程执行的用户身份
stopsignal=int

将文件拷贝至:“/etc/supervisor/conf.d/webapplication1.conf”下

[root@localhost /]#mkdir /etc/supervisor/conf.d
[root@localhost /]#cp webapplication1.conf /etc/supervisor/conf.d/

运行supervisord,查看是否生效

[root@localhost /]#supervisord -c /etc/supervisor/supervisord.confsupervisord -c /etc/supervisor/supervisord.conf
[root@localhost /]# ps -ef | grep webapplication1
root   29878 29685 0 09:57 ?    00:00:00 dotnet webapplication1.dll
root   29892 29363 0 09:57 pts/3  00:00:00 grep --color=auto webapplication1

如果存在dotnet webapplication1.dll 进程则代表运行成功,这时候在使用浏览器进行访问。

Centos7系统下如何搭建.NET Core2.0+Nginx+Supervisor环境

至此关于asp.net core应用程序的守护即配置完成。

supervisor守护进程常用操作

【启动supervisord】
确保配置无误后可以在每台主机上使用下面的命令启动supervisor的服务器端supervisord
supervisord

【停止supervisord】    
supervisorctl shutdown

【重新加载配置文件】
supervisorctl reload

七 、配置supervisor开机启动

新建一个“supervisord.service”文件

[root@localhost /]# vi supervisord.service
# dservice for systemd (centos 7.0+)
# by et-cs (https://github.com/et-cs)
[unit]
description=supervisor daemon
[service]
type=forking
execstart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
execstop=/usr/bin/supervisorctl shutdown
execreload=/usr/bin/supervisorctl reload
killmode=process
restart=on-failure
restartsec=42s
[install]
wantedby=multi-user.target

 将文件拷贝至:“/usr/lib/systemd/system/supervisord.service”

[root@localhost /]# cp supervisord.service /usr/lib/systemd/system/

 执行命令:systemctl enable supervisord

[root@localhost /]# systemctl enable supervisord
created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.

执行命令:systemctl is-enabled supervisord #来验证是否为开机启动

[root@localhost /]# systemctl is-enabled supervisord

重启系统看能否能成功访问

[root@localhost /]# reboot

 Centos7系统下如何搭建.NET Core2.0+Nginx+Supervisor环境

以上是Centos7系統下如何搭建.NET Core2.0+Nginx+Supervisor環境的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:亿速云。如有侵權,請聯絡admin@php.cn刪除
NGINX:現代Web應用程序的多功能工具NGINX:現代Web應用程序的多功能工具Apr 11, 2025 am 12:03 AM

NGINXisessentialformodernwebapplicationsduetoitsrolesasareverseproxy,loadbalancer,andwebserver,offeringhighperformanceandscalability.1)Itactsasareverseproxy,enhancingsecurityandperformancebycachingandloadbalancing.2)NGINXsupportsvariousloadbalancingm

NGINX SSL/TLS配置:使用HTTPS確保您的網站NGINX SSL/TLS配置:使用HTTPS確保您的網站Apr 10, 2025 am 09:38 AM

通過Nginx配置SSL/TLS來確保網站安全,需要以下步驟:1.創建基本配置,指定SSL證書和私鑰;2.優化配置,啟用HTTP/2和OCSPStapling;3.調試常見錯誤,如證書路徑和加密套件問題;4.應用性能優化建議,如使用Let'sEncrypt和會話復用。

NGINX面試問題:ACE您的DevOps/System Admin面試NGINX面試問題:ACE您的DevOps/System Admin面試Apr 09, 2025 am 12:14 AM

Nginx是高性能的HTTP和反向代理服務器,擅長處理高並發連接。 1)基本配置:監聽端口並提供靜態文件服務。 2)高級配置:實現反向代理和負載均衡。 3)調試技巧:檢查錯誤日誌和測試配置文件。 4)性能優化:啟用Gzip壓縮和調整緩存策略。

NGINX緩存技術:改善網站性能NGINX緩存技術:改善網站性能Apr 08, 2025 am 12:18 AM

Nginx缓存可以通过以下步骤显著提升网站性能:1)定义缓存区和设置缓存路径;2)配置缓存有效期;3)根据不同内容设置不同的缓存策略;4)优化缓存存储和负载均衡;5)监控和调试缓存效果。通过这些方法,Nginx缓存能减少后端服务器压力,提升响应速度和用户体验。

帶Docker的NGINX:部署和縮放容器化應用程序帶Docker的NGINX:部署和縮放容器化應用程序Apr 07, 2025 am 12:08 AM

使用DockerCompose可以簡化Nginx的部署和管理,通過DockerSwarm或Kubernetes進行擴展是常見的做法。 1)使用DockerCompose定義和運行Nginx容器,2)通過DockerSwarm或Kubernetes實現集群管理和自動擴展。

高級NGINX配置:掌握服務器塊和反向代理高級NGINX配置:掌握服務器塊和反向代理Apr 06, 2025 am 12:05 AM

Nginx的高級配置可以通過服務器塊和反向代理實現:1.服務器塊允許在一個實例中運行多個網站,每個塊獨立配置。 2.反向代理將請求轉發到後端服務器,實現負載均衡和緩存加速。

NGINX性能調整:針對速度和低潛伏期進行優化NGINX性能調整:針對速度和低潛伏期進行優化Apr 05, 2025 am 12:08 AM

Nginx性能調優可以通過調整worker進程數、連接池大小、啟用Gzip壓縮和HTTP/2協議、使用緩存和負載均衡來實現。 1.調整worker進程數和連接池大小:worker_processesauto;events{worker_connections1024;}。 2.啟用Gzip壓縮和HTTP/2協議:http{gzipon;server{listen443sslhttp2;}}。 3.使用緩存優化:http{proxy_cache_path/path/to/cachelevels=1:2k

NGINX安全性硬化:保護您的Web服務器免受攻擊NGINX安全性硬化:保護您的Web服務器免受攻擊Apr 04, 2025 am 12:06 AM

Nginx安全強化可以通過以下步驟實現:1)確保所有流量通過HTTPS傳輸,2)配置HTTP頭增強通信安全性,3)設置SSL/TLS加密數據傳輸,4)實施訪問控制和速率限制防範惡意流量,5)使用ngx_http_secure_link_module模塊防範SQL注入攻擊,這些措施能有效提升Nginx服務器的安全性。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),