国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > Nginx > 正文

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

2024-08-30 12:29:56
字體:
來源:轉載
供稿:網友

一、Linux .NET Core簡介

 一直以來,微軟只對自家平臺提供.NET支持,這樣等于讓這個“理論上”可以跨平臺的框架在Linux和macOS上的支持只能由第三方項目提供(比如Mono .NET)。

直到微軟推出完全開源的.NET Core。這個開源的平臺兼容.NET  Standard,并且能在Windows、Linux和MacOS上提供完全一致的API。雖然這個小巧的.NET框架只是標準.NET的一個子集,但是已經相當強大了。

一方面,這個小巧的框架可以讓某些功能性應用同時運行在三個平臺上(就像某些功能性的Python腳本一樣),另一方面,這也可以讓服務器運維人員將ASP .NET服務程序部署在Linux服務器上(特別是對于運行Windows Server較為吃力的服務器)。

官網參考資料:https://www.microsoft.com/net/core#linuxcentos

二、Linux .NET Core2.0 環境部署前準備

1.環境說明:

服務器系統:CentOS 7.2.1511

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

1)關閉firewall:

systemctl stop firewalld.service #停止firewallsystemctl 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 updateyum install libunwind libicu -yyum install dotnet-sdk-2.0.0 -y

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

dotnet --infodotnet --version

四、測試.NET Core2.0 環境

1.在home目錄下初始化一個測試環境并輸出”Hello World “內容 (測試方式一,可忽略)

cd /homedotnet new console -o hwappcd hwappdotnet run

輸出空內容如下:

[root@localhost hwapp]# dotnet runHello World! 

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

Centos 下.net core 2 環境測試用例 (把它上傳到/home目錄下或自定義的目錄)

下載地址:

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

執行以下命令

cd /home/WebApplication1dotnet 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 nginxCreated 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 nginxroot   14626   1 0 08:47 ?    00:00:00 nginx: master process nginxnginx   14627 14626 0 08:47 ?    00:00:00 nginx: worker processroot   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服務

systemctl enable nginx.service             #設置開機自啟動

systemctl disable nginx.service            #停止開機自啟動

systemctl status nginx.service             #查看服務當前狀態

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

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

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 runUsing launch settings from /home/WebApplication1/Properties/launchSettings.json...Hosting environment: DevelopmentContent root path: /home/WebApplication1Now listening on: http://[::]:88Application started. Press Ctrl+C to shut down.

通過IP 80端口訪問

Centos7,.NET,Core2.0,Nginx,Supervisor

六、配置守護服務(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 WebApplication1root   29878 29685 0 09:57 ?    00:00:00 dotnet WebApplication1.dllroot   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=forkingExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.confExecStop=/usr/bin/supervisorctl shutdownExecReload=/usr/bin/supervisorctl reloadKillMode=processRestart=on-failureRestartSec=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 supervisordCreated 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+Mysql環境的詳細方法,希望對大家有所幫助


注:相關教程知識閱讀請移步到服務器教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 万山特区| 安徽省| 宣城市| 呼玛县| 鄂伦春自治旗| 达州市| 柏乡县| 沿河| 大英县| 隆化县| 土默特右旗| 罗山县| 三河市| 玉龙| 五河县| 东兰县| 镇赉县| 隆子县| 苏尼特左旗| 孟津县| 苏州市| 西安市| 南乐县| 随州市| 西乡县| 临潭县| 江城| 潜山县| 正宁县| 长兴县| 桃园市| 兴安县| 保亭| 沙洋县| 民权县| 鹤壁市| 平湖市| 昌图县| 华安县| 丹寨县| 罗源县|