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

时间:2022-10-22 15:49:27

好记性不如烂笔头!

一、简介

一直以来,微软只对自家平台提供.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

二、.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

三、部署.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-prod\nenabled=1\ngpgcheck=1\ngpgkey=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+Mysql环境

通过IE访问测试页

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

五、安装配置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+Mysql环境

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 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 系统下搭建.NET Core2.0+Nginx+Supervisor+Mysql环境

六、配置守护服务(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+Mysql环境

至此关于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+Mysql环境

Centos7 系统下搭建.NET Core2.0+Nginx+Supervisor+Mysql环境的更多相关文章

  1. centos7系统下搭建docker本地镜像仓库

    ## 准备工作 用到的工具, Xshell5, Xftp5, docker.io/registry:latest镜像 关于docker的安装和设置加速, 请参考这篇博文centos7系统下 docke ...

  2. centos7系统下安装php-fpm并配置nginx支持并开启网站gzip压缩

    注:此处不介绍nginx的安装.以下教程默认已安装nginx. 1. yum install -y php-fpm yum install php-pdo yum install php-mysql ...

  3. CentOS 7_64位系统下搭建Hadoop_2.8.0分布式环境

    准备条件: CentOS 7 64位操作系统 | 选择minimal版本即可(不带可视化桌面环境),也可以选择带完整版Hadoop-2.8.0 | 本文采用的是Hadoop-2.8.0版本.JDK1. ...

  4. CentOS7系统下搭建Jenkins环境

    1. 安装JDK yum -y install java 2.安装Ant 添加JPackage源 yum -y install wget wget http://www.jpackage.org/jp ...

  5. Linux服务器centos7系统下搭建Jenkins

    Jenkins是什么? Jenkins是开源CI&CD软件领导者, 提供超过1000个插件来支持构建.部署.自动化, 满足任何项目的需要. 所以现在是越来越多的公司都在使用Jenkins做持续 ...

  6. windows下搭建hadoop-2.6.0本地idea开发环境

    概述 本文记录windows下hadoop本地开发环境的搭建: OS:windows hadoop执行模式:独立模式 安装包结构: Hadoop-2.6.0-Windows.zip - cygwinI ...

  7. centos7系统下nginx安装并配置开机自启动操作

    准备工作 我的centos7系统是最小化安装的, 缺很多库, 首先安装必须的运行库 ? 1 2 3 4 5 6 7 8 9 10 11 yum install wget gcc gcc-c++ pcr ...

  8. centos7系统下 docker 环境搭建

    运行环境: VMware Workstation Pro 在虚拟机中安装centos7系统, 选择最小安装, 网络连接方式选择的桥接(与宿主机在同一IP段)centos7一定要安装64位, docke ...

  9. CentOS7系统下配置nginx及https安全站点

    本文主要演示在CentOS7系统下OpenSSL的使用方式,以及Nginx在测centOS下的证书配置方式 注意:生成服务器端证书时必须在CN字段中如实输入站点的访问地址 ,如果是域名就写域名,ip访 ...

随机推荐

  1. 内置对象(Session、Application、ViewState)

    内置对象:为了跨页面传值和状态保持.→HTTP的无状态性 [4.]Session:每一台电脑访问服务器,都会是独立的一套session,key值都一样,但是内容都是不一样的 以上所有内容,都跟cook ...

  2. git checkout 命令详解(转)

    在日常的git操作中,git checkout——检出,是我们的常用命令.最为常用的两种情形是创建分支和切换分支. 在下面的命令中,使用了一些简写,在这里说明一下: git st # git stat ...

  3. 线程池Executors探究

    线程池用到的类在java.util.concurrent包下,核心类是Executors,通过其不同的几个方法可产生不同的线程池. 1.生成固定大小的线程池 public static Executo ...

  4. 工具(2): 极简MarkDown排版介绍(How to)

    如何切换编辑器 切换博客园编辑器为MarkDown:MarkDown Editor 选择一个在线编辑和预览站点:StackEdit 如何排版章节 MarkDown: 大标题 ========== 小标 ...

  5. paramiko

    http://www.cnblogs.com/wupeiqi/articles/5095821.html

  6. 6-2 Verbs and Adjectives with that clauses

    1 Many sentences in English contain two clauses: a main clause and a "that" clause. The &q ...

  7. 3分钟带你搞懂ES6 import 和 export

    如下语句是 default import: // B.js import A from './A' 且只在A存在 default export 时生效: // A.js export default ...

  8. collectionView代理方法快速设置cell大小上下左右间隔

    #define JianGe 25 #define GeShu 4 #define ScreenWidth ([UIScreen mainScreen].bounds.size.width) #def ...

  9. Sprint会议2

    昨天:准备查找安卓APP开发的有关资料,安装有关软件 今天:自己制作一个安卓小程序,熟悉一下操作 遇到问题:安装遇到问题,环境配置出现问题

  10. CenOs7安装oracle图文详细过程(01)

    原创作品,转载请在文章头部(显眼位置)注明出处:https://www.cnblogs.com/sunshine5683/p/10011441.html 1.检查必要的安装包是否安装 命令脚本: rp ...