Linux .Net Core publish the project and build

.Net Core deployment environment

Introduction: According to the official requirements before installing DotNetCore environment requires registration Microsoft Key and Product Repository, and also need to install dependencies, this step only needs to run once per machine, using the following command:

Sudo rpm Uvh https: // packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm

1. First update your system with the following command

sudo yum update

2. Install .Net Core SDK 3.1 (here I chose 3.1, you may need to install the SDK version of the project according to the environment)

sudo yum install dotnet-sdk-3.1

 

3. Check whether the installation is successful, you can also know .Net Core version with the following command

dotnet --version

So far .Net Core environment to complete the installation!

.Net Core Linux project released to

Preface: .Net Core project build process, where no explanation, everyone can be their own Baidu, here directly from the publishing project

1. After the file is released locally, we need to put the files into a local project in Centos, here you need the help of a software FileZilla (Download: https://filezilla-project.org/download.php?platform=win64 )

This software can upload files to windows in Centos (detailed usage of this software at your own Baidu)

 

 2.项目上传到Centos中后,在Centos发布项目的目录中执行 注意: 我的项目名是WebApplication1,所以是WebApplication1.dll,根据你自己取的名称来启动项目文件

dotnet    WebApplication1.dll  

 

 

 

 可以看到我们已经启动了项目,但是现在还不急,我们要用Nginx反向代理来处理现有的端口

 

.Net Core使用Nginx反向代理

1.先安装下载工具wget

 

yum install wget

 

2.下载Nginx压缩包

wget -c https://nginx.org/download/nginx-1.11.6.tar.gz

3.解压压缩包

 

 

tar -zxvf nginx-1.11.6.tar.gz

 

4. 到此,我们先把准备工作做好,先下载必要的依赖库

 安装gcc环境

yum install gcc-c++

安装PCRE依赖库

yum install -y pcre pcre-devel

安装zlib 依赖库

yum install -y zlib zlib-devel

安装OpenSSL安全套接字层密码库

yum install -y openssl openssl-deve

5.我们开始操作Nginx,先进入Nginx文件夹中

cd nginx-1.11.6

 执行配置命令

./configure

执行安装命令

make install

进入配置文件文件夹

cd /usr/local/nginx/conf

编辑nginx.conf配置文件

这里建议使用 vim 编辑文件,颜色分明,不易将内容改错

yum -y install vim*  
编辑nginx.conf配置文件
vim nginx.conf

 

 

 

 

 

 

 6.再次进入Nginx目录

cd /usr/local/nginx/sbin/

7.修改配置文件后重新运行Nginx

./nginx -s reload

如果重启出现:nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory),可以通过执行下面的命令解决

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

然后在执行Nginx

./nginx -s reload

8.最后启动项目,到你的Centos发布项目的路径下,启动项目

dotnet WebApplication1.dll

9.使用你的Centos Ip+反向代理的端口进行访问(我刚设置的是80),这个可以根据你自己需要而更改,不固定!

Centos .Net Core 后台守护进程(Supervisor)部署

前言:如果大家前面的配置都成功了,那么恭喜你,到了这里你应该会有一个疑问,现在项目是跑起来了但是如果我要一直运行项目,发现不能对Linux进行操作,而且终端关闭,项目也会立马停止,能不能有个东西,不用我们去管,项目一直在跑,又不会影响我们操作,更不会因为终端关闭而程序崩溃这个时候

 

Supervisor就出现了!它能很好的解决我们刚才说的问题

1.安装Supervisor

sudo su - #切换为root用户 
yum install epel-release
yum install -y supervisor

 

 

2.安装完成之后我们开始进行Supervisor配置

在etc下创建目录,并赋权限(没有etc目录则会自动创建一个)

mkdir -m 700 -p /etc/supervisor

在目录“ /etc/supervisor”下创建配置文件

 echo_supervisord_conf > /etc/supervisor/supervisord.conf

修改配置文件(在配置文件末添上下面一段命令)  

files=/etc/supervisor/conf.d/*.conf   表示会执行/etc/supervisor/conf.d 目录下的所有conf文件,也就是我们配置的项目进程文件位置,如果是多个,则添加多个conf文件就行,注意:一定配置文件的内容不能错,否则不会运行
[include]
files=/etc/supervisor/conf.d/*.conf  

在目录“/etc/supervisor”下创建dotnet core 进程配置文件存放目录“conf.d”

mkdir -m 700 /etc/supervisor/conf.d

以下这个就是 dotnet core 进程文件存放的位置,多个.Net Core项目,可以创建多个,MyDotNetName命名这个最好是项目名称以.conf后缀结尾,以便于多个项目而作区分

 vim /etc/supervisor/conf.d/MyDotNetName.conf

"MyDotNetName"可以为dotnet core 入口dll文件名字 ,插入内容,注意首尾需无空格,需顶格:

[program:MyDotNetName]
command=/bin/bash -c "dotnet MyDotNetName.dll"
directory=/root/publish
stderr_logfile=/var/log/MyDotNetName.error.log
stdout_logfile=/var/log/MyDotNetName.stdout.log
environment=ASPNETCORE_ENVIRONMENT=Production
user=root
stopsignal=INT
autostart=true
autorestart=true
startsecs=3
[program:MyDotNetName]  ;启动守护进程会显示的名称,这个命名规范不强制,但是为了便于好记,最好使用自己的项目名称

command=/bin/bash -c "dotnet MyDotNetName.dll" ;/bin/bash -c 这个前缀是固定的不能省 "dotnet MyDotNetName.dll" .Net Core 启动程序的入口
directory=/root/publish ;/root/publish 你的启动项目的文件路径

stderr_logfile=/var/log/MyDotNetName.error.log ;错误日志(默认即可)
stdout_logfile=/var/log/MyDotNetName.stdout.log  ;项目日志(默认即可)
environment=ASPNETCORE_ENVIRONMENT=Production    ; 环境变量,这里是.net Core 环境变量(以下配置信息默认即可)
user=root  ; 操作的用户
stopsignal=INT autostart=true 自动启动
autorestart=true 自动重启 startsecs=3 重启时间间隔(秒)
 

3.以上的配置完成后我们就应该开始启动配置

systemctl daemon-reload #使配置生效
systemctl enable supervisor.service #设置服务开机启动,即设置enable
systemctl start supervisor.service  #启动服务

4.验证dotnet 进程是否启动成功

ps -ef | grep dotnet

 

 

 

5.(拓展知识)supervisor远程管理

进入supervisor配置文件

vim /etc/supervisor/supervisord.conf
;[inet_http_server]         ; inet (TCP) server disabled by default
;port=127.0.0.1:9001        ; (ip_address:port specifier, *:port for all iface)
;username=user              ; (default is no username (open server))
;password=123               ; (default is no password (open server))

;以下内容开启http服务
[inet_http_server]
port=192.168.1.71:9001      ;ip 加端口,改为自己Centos的IP
username=admin               ;登陆账号,可以不设
password=123456              ;登陆账户,可以不设

 重启服务,就好了!

6.关闭防火墙

CentOS 7.0默认使用的是firewall作为防火墙。

查看防火墙状态命令:firewall-cmd --state

停止firewall命令:systemctl stop firewalld.service

禁止firewall开机启动命令:systemctl disable firewalld.service

 -------------------------------------------------------------------------------------------------------------------------------------------------

Guess you like

Origin www.cnblogs.com/XiangZiPeng/p/12367784.html