Cnetos 7 部署.net core 3.1项目

本文详细介绍了如何在Cnetos7系统中部署.NETCore3.1项目,包括检查版本、安装依赖、使用Xshell上传、启动项目、配置防火墙、解决验证码问题、以及两种方式实现项目开机自启动:使用Supervisor守护进程和Nginx代理。
摘要由CSDN通过智能技术生成

Cnetos 7 部署.net core 3.1项目

1、如果是框架依赖则需检查是否安装.NET Core 3.1

dotnet --info
#查看 dotnet 版本信息
dotnet --version

在这里插入图片描述

3、先安装 lrzsz,然后使用 Xshell 上传发布好的项目到 Linux 服务器,也可以使用Xftp或者其它上传工具上传项目

yum -y install lrzsz

#检查是否安装 lrzsz
rpm -qa lrzsz
```![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/0498a739d24e4bb1ac72842279b1ad68.png#pic_center)


### 4、切换目录到 /var/www/,如果没有则创建

```shell
mkdir /var/www/
cd /var/www/

5、将项目打包上传到/var/www/,运行rz会打开一个弹窗,选择上传项目,点击确定,等待上传完成即可

rz

6、运行上传的项目,如果没有设置端口则使用默认的端口5001、5000直接运行dotnet run即可

#开放端口
firewall-cmd --add-port=9991/tcp --permanent
#重新载入防火墙配置
firewall-cmd --reload

#dotnet VOL.WebApi.dll --urls="http://*:5000;http://*:5001" --environment=Development
dotnet VOL.WebApi.dll

在这里插入图片描述

如果出现验证码不显示的,且后台报错移除为错误则需要安装GDI的库

服务器处理出现异常:The type initializer for 'Gdip' threw an exception.System.DllNotFoundException: Unable to load shared library 'libgdiplus' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibgdiplus: cannot open shared object file: No such file or directory
   at System.Drawing.SafeNativeMethods.Gdip.GdiplusStartup(IntPtr& token, StartupInput& input, StartupOutput& output)
   at System.Drawing.SafeNativeMethods.Gdip..cctor()   at System.Drawing.SafeNativeMethods.Gdip.GdipCreateBitmapFromScan0(Int32 width, Int32 height, Int32 stride, Int32 format, IntPtr scan0, IntPtr& bitmap)

在这里插入图片描述
在这里插入图片描述

7、出现以下界面则正常运行项目,按ctrl+c即可结束项目运行

在这里插入图片描述

8、配置项目开机运行的方式

方式1:使用守护进程

(1)安装守护进程Supervisor
这里直接执行安装 Supervisor,可能会出现没有可用软件包 supervisor,这个时候需要安装一个叫”epel-release”的软件包,这个软件包会自动配置 yum 的软件仓库
#执行 epel-release
yum -y install epel-release

#安装 Supervisor
yum -y install supervisor

#通过配置文件来启动 supervisor
supervisord -c /etc/supervisord.conf

#启动 supervisorctl
supervisorctl -c /etc/supervisord.conf
注意:需要执行 supervisorctl,如果出现 error: <class ‘socket.error’>, [Errno 13] Permission denied: file: /usr/lib64/python2.7/socket.py line: 224 的错误,需要先执行下面语句:
sudo supervisord -c /etc/supervisord.conf
sudo supervisorctl -c /etc/supervisord.conf
然后在去执行需要的操作
#查看 supervisor 版本
version
#配置 supervisor开机启动
#编辑 supervisord.service
vim /usr/lib/systemd/system/supervisord.service
[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service nss-user-lookup.target
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
ExecStop=/usr/bin/supervisorctl shutdown
ExecReload=/usr/bin/supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=42
[Install]
WantedBy=multi-user.target
编辑好后保存退出,然后执行命令,查看是否启动成功,启动服务
systemctl daemon-reload
systemctl enable supervisord

#验证一下是否为开机启动
systemctl is-enabled supervisord

#创建API.ini文件
vim /etc/supervisord.d/API.ini
#查看和编辑 API.ini 配置文件vim API.ini,进入文件后,按“i”或者“a”进入插入模式,插入下面的配置信息,配置程序名称
[program:API] 
#运行程序的命令
#如果使用的是默认端口
#command=dotnet VOL.WebApi.dll --urls="http://*:5000;http://*:5001"
command=dotnet VOL.WebApi.dll
#命令执行的目录
directory=/var/www/API
#进程环境变量
environment=ASPNETCORE_ENVIRONMENT=Production
#进程执行的用户身份
user=root
#进程停止信号,可以为 TERM, HUP, INT, QUIT, KILL, USR1, or USR2 等信号默认为 TERM 。当用
#设定的信号去干掉进程,退出码会被认为是 expected,非必须设置
stopsignal=INT
#如果是 true 的话,子进程将在 supervisord 启动后被自动启动,默认就是 true,非必须设置
autostart=true
#这个是设置子进程挂掉后自动重启的情况,有三个选项,false,unexpected 和 true。如果为 false
#的时候,无论什么情况下,都不会被重新启动,如果为 unexpected,只有当进程的退出码不在下面
#的 exitcodes 里面定义的退出码的时候,才会被自动重启。当为 true 的时候,只要子进程挂掉,将
#会被无条件的重启
autorestart=true
#这个选项是子进程启动多少秒之后,此时状态如果是 running,则我们认为启动成功了,默认值为 1 。
#非必须设置
startsecs=1
#错误日志文件
stderr_logfile=/var/log/API.err.log
#输出日志文件
stdout_logfile=/var/log/APINetCore.out.log

在这里插入图片描述
?origin_url=img%2F10%2F12.png&pos_id=img-Ptx09x4L-1711446620899)

按 ESC,输入命令保存配置文件,:wq (保存编辑操作退出),:wq! (保存编辑强制退出),:w ! sudo tee %,注意:如果之前进入编辑文件没有在 root 或者 sudo 下,使用 wq,将会提示只读,使用强制执行,使用 wq!,将会提示 E212:无法打开或写入文件,这个时候需要用到 w ! sudo tee %这个命令,然后在 q 退出编辑,重新加载 Supervisor 配置,查看 Supervisor 运行的进程

方式2:使用进程服务

#创建服务文件,创建服务定义文件 API.service:
vim /etc/systemd/system/API.service
将以下内容保存至 /etc/systemd/system/API.service:
[Unit]
Description=API

[Service]
WorkingDirectory=/var/www/API
ExecStart=/usr/bin/dotnet /var/www/API/VOL.WebApi.dll
Restart=always
# Restart service after 10 seconds if the sscms service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=sscms
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target
#载入配置
systemctl daemon-reload
#启动项目
systemctl start API
#查看项目运行状态
systemctl status API
#开机启动项目
systemctl enable API
```![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/f6e52619107a423e9ef04afa2ac67772.png#pic_center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/71bca3046f804fd682ab545c512c1497.png#pic_center)


## nginx代理.net core项目

修改/etc/nginx/conf.d/default.cont

```nginx
server {
	listen 80;
	location / {
	proxy_pass http://localhost:9991;
	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;
	}
}
#关闭9991端口
firewall-cmd --remove-port=9991/tcp --permanent
#启用80端口
firewall-cmd --add-port=80/tcp --permanent
#重新载入防火墙配置
firewall-cmd --reload

在这里插入图片描述

  • 12
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值