Centos7系統(tǒng)下如何搭建.NETCore2.0+Nginx+Supervisor環(huán)境

這篇文章主要介紹了Centos7系統(tǒng)下如何搭建.NET Core2.0+Nginx+Supervisor環(huán)境的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Centos7系統(tǒng)下如何搭建.NET Core2.0+Nginx+Supervisor環(huán)境文章都會有所收獲,下面我們一起來看看吧。

創(chuàng)新互聯(lián)專注于靖宇網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供靖宇營銷型網(wǎng)站建設(shè),靖宇網(wǎng)站制作、靖宇網(wǎng)頁設(shè)計、靖宇網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造靖宇網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供靖宇網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

一、linux .net core簡介

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

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

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

二、linux .net core2.0 環(huán)境部署前準備

1.環(huán)境說明:

服務(wù)器系統(tǒng):centos 7.2.1511

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

1)關(guān)閉firewall:

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機啟動
firewall-cmd --state #查看默認防火墻狀態(tài)(關(guān)閉后顯示notrunning,開啟后顯示running)

 2)關(guān)閉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 環(huán)境

1.添加dotnet產(chǎn)品

在安裝.net核心之前,您需要注冊微軟產(chǎn)品提要。這只需要做一次。首先,注冊微軟簽名密鑰,然后添加微軟產(chǎn)品提要。

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

在下一步之前,請從您的系統(tǒng)中刪除.net .net以前的任何預(yù)覽版本。

以下命令更新用于安裝的產(chǎn)品列表,安裝.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 環(huán)境

1.在home目錄下初始化一個測試環(huán)境并輸出”hello world “內(nèi)容 (測試方式一,可忽略)

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

輸出空內(nèi)容如下:

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

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

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

下載地址:

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

執(zhí)行以下命令

cd /home/webapplication1
dotnet restore  //如果使過用測試方式一,就需先執(zhí)行這命令重新加載一下當(dāng)前新的網(wǎng)站文件
dotnet run

運行后如下圖:

 Centos7系統(tǒng)下如何搭建.NET Core2.0+Nginx+Supervisor環(huán)境

 通過ie訪問測試頁

  Centos7系統(tǒng)下如何搭建.NET Core2.0+Nginx+Supervisor環(huán)境

五、安裝配置nginx對asp.net core應(yīng)用的轉(zhuǎn)發(fā)

1.安裝nginx環(huán)境

[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 來設(shè)置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系統(tǒng)下如何搭建.NET Core2.0+Nginx+Supervisor環(huán)境

nginx常用的操作命令

systemctl start nginx.service               #啟動nginx服務(wù)

systemctl enable nginx.service             #設(shè)置開機自啟動

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

systemctl status nginx.service             #查看服務(wù)當(dāng)前狀態(tài)

systemctl restart nginx.service           #重新啟動服務(wù)

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

4.防火墻配置(如果系統(tǒng)有防火墻就需要進行寫入規(guī)則)

命令:firewall-cmd –zone=public –add-port=80/tcp –permanent(開放80端口)

命令:systemctl restart firewalld(重啟防火墻以使配置即時生效)

5.配置nginx對asp.net core應(yīng)用的轉(zhuǎn)發(fā)

修改 /etc/nginx/conf.d/default.conf 文件。

將文件內(nèi)容替換為

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端口訪問

Centos7系統(tǒng)下如何搭建.NET Core2.0+Nginx+Supervisor環(huán)境

六、配置守護服務(wù)(supervisor)

目前存在三個問題

問題1:asp.net core應(yīng)用程序運行在shell之中,如果關(guān)閉shell則會發(fā)現(xiàn)asp.net core應(yīng)用被關(guān)閉,從而導(dǎo)致應(yīng)用無法訪問,這種情況當(dāng)然是我們不想遇到的,而且生產(chǎn)環(huán)境對這種情況是零容忍的。

問題2:如果asp.net core進程意外終止那么需要人為連進shell進行再次啟動,往往這種操作都不夠及時。

問題3:如果服務(wù)器宕機或需要重啟我們則還是需要連入shell進行啟動。

為了解決這個問題,我們需要有一個程序來監(jiān)聽asp.net core 應(yīng)用程序的狀況。在應(yīng)用程序停止運行的時候立即重新啟動。這邊我們用到了supervisor這個工具,supervisor使用python開發(fā)的。

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:如果服務(wù)已啟動,修改配置文件可用“supervisorctl reload”命令來使其生效

3.配置對asp.net core應(yīng)用的守護

創(chuàng)建一個 webapplication1.conf文件,內(nèi)容大致如下

[root@localhost /]# vi webapplication1.conf
[program:webapplication1]
command=dotnet webapplication1.dll ; 運行程序的命令
directory=/home/webapplication1/ ; 命令執(zhí)行的目錄
autorestart=true ; 程序意外退出是否自動重啟
stderr_logfile=/var/log/webapplication1.err.log ; 錯誤日志文件
stdout_logfile=/var/log/webapplication1.out.log ; 輸出日志文件
environment=aspnetcore_environment=production ; 進程環(huán)境變量
user=root ; 進程執(zhí)行的用戶身份
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系統(tǒng)下如何搭建.NET Core2.0+Nginx+Supervisor環(huán)境

至此關(guān)于asp.net core應(yīng)用程序的守護即配置完成。

supervisor守護進程常用操作

【啟動supervisord】
確保配置無誤后可以在每臺主機上使用下面的命令啟動supervisor的服務(wù)器端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/

 執(zhí)行命令: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.

執(zhí)行命令:systemctl is-enabled supervisord #來驗證是否為開機啟動

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

重啟系統(tǒng)看能否能成功訪問

[root@localhost /]# reboot

 Centos7系統(tǒng)下如何搭建.NET Core2.0+Nginx+Supervisor環(huán)境

關(guān)于“Centos7系統(tǒng)下如何搭建.NET Core2.0+Nginx+Supervisor環(huán)境”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“Centos7系統(tǒng)下如何搭建.NET Core2.0+Nginx+Supervisor環(huán)境”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

新聞標題:Centos7系統(tǒng)下如何搭建.NETCore2.0+Nginx+Supervisor環(huán)境
網(wǎng)頁網(wǎng)址:http://muchs.cn/article10/ipjido.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、響應(yīng)式網(wǎng)站、服務(wù)器托管、建站公司、品牌網(wǎng)站設(shè)計移動網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司