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

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

May 12, 2023 pm 08:13 PM
nginx centos7 supervisor

一、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

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

docker容器名稱怎麼查 docker容器名稱怎麼查 Apr 15, 2025 pm 12:21 PM

可以通過以下步驟查詢 Docker 容器名稱:列出所有容器(docker ps)。篩選容器列表(使用 grep 命令)。獲取容器名稱(位於 "NAMES" 列中)。

nginx在windows中怎麼配置 nginx在windows中怎麼配置 Apr 14, 2025 pm 12:57 PM

如何在 Windows 中配置 Nginx?安裝 Nginx 並創建虛擬主機配置。修改主配置文件並包含虛擬主機配置。啟動或重新加載 Nginx。測試配置並查看網站。選擇性啟用 SSL 並配置 SSL 證書。選擇性設置防火牆允許 80 和 443 端口流量。

怎麼查看nginx是否啟動 怎麼查看nginx是否啟動 Apr 14, 2025 pm 01:03 PM

確認 Nginx 是否啟動的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 檢查端口 80 是否開放;3. 查看系統日誌中 Nginx 啟動消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

docker怎麼啟動容器 docker怎麼啟動容器 Apr 15, 2025 pm 12:27 PM

Docker 容器啟動步驟:拉取容器鏡像:運行 "docker pull [鏡像名稱]"。創建容器:使用 "docker create [選項] [鏡像名稱] [命令和參數]"。啟動容器:執行 "docker start [容器名稱或 ID]"。檢查容器狀態:通過 "docker ps" 驗證容器是否正在運行。

nginx怎麼配置雲服務器域名 nginx怎麼配置雲服務器域名 Apr 14, 2025 pm 12:18 PM

在雲服務器上配置 Nginx 域名的方法:創建 A 記錄,指向雲服務器的公共 IP 地址。在 Nginx 配置文件中添加虛擬主機塊,指定偵聽端口、域名和網站根目錄。重啟 Nginx 以應用更改。訪問域名測試配置。其他注意事項:安裝 SSL 證書啟用 HTTPS、確保防火牆允許 80 端口流量、等待 DNS 解析生效。

docker怎麼創建容器 docker怎麼創建容器 Apr 15, 2025 pm 12:18 PM

在 Docker 中創建容器: 1. 拉取鏡像: docker pull [鏡像名] 2. 創建容器: docker run [選項] [鏡像名] [命令] 3. 啟動容器: docker start [容器名]

nginx怎麼查版本 nginx怎麼查版本 Apr 14, 2025 am 11:57 AM

可以查詢 Nginx 版本的方法有:使用 nginx -v 命令;查看 nginx.conf 文件中的 version 指令;打開 Nginx 錯誤頁,查看頁面的標題。

怎麼啟動nginx服務器 怎麼啟動nginx服務器 Apr 14, 2025 pm 12:27 PM

啟動 Nginx 服務器需要按照不同操作系統採取不同的步驟:Linux/Unix 系統:安裝 Nginx 軟件包(例如使用 apt-get 或 yum)。使用 systemctl 啟動 Nginx 服務(例如 sudo systemctl start nginx)。 Windows 系統:下載並安裝 Windows 二進製文件。使用 nginx.exe 可執行文件啟動 Nginx(例如 nginx.exe -c conf\nginx.conf)。無論使用哪種操作系統,您都可以通過訪問服務器 IP

See all articles