佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > Centos7系統(tǒng)下搭建.NET Core2.0+Nginx+Supervisor環(huán)境

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

熱門標(biāo)簽:使用U盤裝系統(tǒng) 百度競(jìng)價(jià)點(diǎn)擊價(jià)格的計(jì)算公式 阿里云 美圖手機(jī) 硅谷的囚徒呼叫中心 網(wǎng)站建設(shè) 智能手機(jī) 檢查注冊(cè)表項(xiàng)

一、Linux .NET Core簡(jiǎn)介

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

直到微軟推出完全開源的.NET Core。這個(gè)開源的平臺(tái)兼容.NET  Standard,并且能在Windows、Linux和MacOS上提供完全一致的API。雖然這個(gè)小巧的.NET框架只是標(biāo)準(zhǔn).NET的一個(gè)子集,但是已經(jīng)相當(dāng)強(qiáng)大了。

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

官網(wǎng)參考資料:https://www.microsoft.com/net/core#linuxcentos

二、Linux .NET Core2.0 環(huán)境部署前準(zhǔn)備

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

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

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

1)關(guān)閉firewall:

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機(jī)啟動(dòng)
firewall-cmd --state #查看默認(rèn)防火墻狀態(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核心之前,您需要注冊(cè)微軟產(chǎn)品提要。這只需要做一次。首先,注冊(cè)微軟簽名密鑰,然后添加微軟產(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

在下一步之前,請(qǐng)從您的系統(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

四、測(cè)試.NET Core2.0 環(huán)境

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

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

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

[root@localhost hwapp]# dotnet run
Hello World! 

2.上傳.net core的實(shí)例頁(yè)面進(jìn)行測(cè)試 (測(cè)試方式二、推薦)

Centos 下.net core 2 環(huán)境測(cè)試用例?。ò阉蟼鞯?home目錄下或自定義的目錄)

下載地址:

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

執(zhí)行以下命令

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

運(yùn)行后如下圖:

 

 通過(guò)IE訪問(wèn)測(cè)試頁(yè)

  

五、安裝配置nginx對(duì)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 來(lái)啟動(dòng)nginx。

[root@localhost ~]# systemctl start nginx

輸入:systemctl enable nginx 來(lái)設(shè)置nginx的開機(jī)啟動(dòng)(linux宕機(jī)、重啟會(huì)自動(dòng)運(yùn)行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.通過(guò)iE檢查能否訪問(wèn)

[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

nginx常用的操作命令

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

systemctl enable nginx.service             #設(shè)置開機(jī)自啟動(dòng)

systemctl disable nginx.service            #停止開機(jī)自啟動(dòng)

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

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

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

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

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

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

5.配置nginx對(duì)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進(jìn)行測(cè)試

[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.

通過(guò)IP 80端口訪問(wèn)

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

目前存在三個(gè)問(wèn)題

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

問(wèn)題2:如果ASP.NET Core進(jìn)程意外終止那么需要人為連進(jìn)shell進(jìn)行再次啟動(dòng),往往這種操作都不夠及時(shí)。

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

為了解決這個(gè)問(wèn)題,我們需要有一個(gè)程序來(lái)監(jiān)聽(tīng)ASP.NET Core 應(yīng)用程序的狀況。在應(yīng)用程序停止運(yùn)行的時(shí)候立即重新啟動(dòng)。這邊我們用到了Supervisor這個(gè)工具,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ù)已啟動(dòng),修改配置文件可用“supervisorctl reload”命令來(lái)使其生效

3.配置對(duì)ASP.NET Core應(yīng)用的守護(hù)

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

[root@localhost /]# vi WebApplication1.conf
[program:WebApplication1]
command=dotnet WebApplication1.dll ; 運(yùn)行程序的命令
directory=/home/WebApplication1/ ; 命令執(zhí)行的目錄
autorestart=true ; 程序意外退出是否自動(dòng)重啟
stderr_logfile=/var/log/WebApplication1.err.log ; 錯(cuò)誤日志文件
stdout_logfile=/var/log/WebApplication1.out.log ; 輸出日志文件
environment=ASPNETCORE_ENVIRONMENT=Production ; 進(jìn)程環(huán)境變量
user=root ; 進(jìn)程執(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/

運(yùn)行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 進(jìn)程則代表運(yùn)行成功,這時(shí)候在使用瀏覽器進(jìn)行訪問(wèn)。

至此關(guān)于ASP.NET Core應(yīng)用程序的守護(hù)即配置完成。

Supervisor守護(hù)進(jìn)程常用操作

【啟動(dòng)supervisord】
確保配置無(wú)誤后可以在每臺(tái)主機(jī)上使用下面的命令啟動(dòng)supervisor的服務(wù)器端supervisord
supervisord

【停止supervisord】    
supervisorctl shutdown

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

七 、配置Supervisor開機(jī)啟動(dòng)

新建一個(gè)“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 #來(lái)驗(yàn)證是否為開機(jī)啟動(dòng)

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

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

[root@localhost /]# reboot

 

以上既是 Centos7系統(tǒng)下搭建.NET Core2.0+Nginx+Supervisor+Mysql環(huán)境的詳細(xì)方法,希望對(duì)大家有所幫助

標(biāo)簽:山南 湖北 賀州 煙臺(tái) 黃山 懷化 通遼 湘潭

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Centos7系統(tǒng)下搭建.NET Core2.0+Nginx+Supervisor環(huán)境》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    恭城| 揭东县| 灯塔市| 海林市| 静安区| 冀州市| 达日县| 德格县| 宝清县| 罗城| 浦城县| 潼关县| 明光市| 白朗县| 哈尔滨市| 黑水县| 防城港市| 石棉县| 湖南省| 洛阳市| 新蔡县| 济源市| 江陵县| 金乡县| 苏尼特左旗| 甘泉县| 长海县| 安吉县| 芜湖县| 开阳县| 花垣县| 阿尔山市| 新源县| 蕉岭县| 邵阳县| 木兰县| 青冈县| 茶陵县| 康马县| 舒城县| 都兰县|