在CentOS 8 上 部署 .Net Core 应用程序

1、更新dnf 源
dnf update
2、安装 Asp.Net Core 运行时
dnf install aspnetcore-runtime-3.1
2.1、验证是否安装成功
dotnet --info

在这里插入图片描述

出现如上图所示就说明安装成功

3、安装Nginx
dnf -y install nginx
3.1、查看nginx版本
nginx -v
在这里插入图片描述

3.2、设置开机自启动
systemctl enable nginx
3.3、启动 nginx 服务
service nginx start
3.4、其他 相关 指令

卸载

dnf remove nginx

停止 服务

service nginx stop

重启

service nginx restart

加载配置文件

service nginx reload
4、MySql 安装
4.1、下载
wget https://repo.mysql.com//mysql80-community-release-el8-1.noarch.rpm
4.2、使用rpm 安装 mysql
rpm -ivh mysql80-community-release-el8-1.noarch.rpm
4.3、dnf 安装 mysql 服务
dnf -y install mysql-server
4.4、设置开机自启动
systemctl enable mysqld.service
4.5、启动mysql
systemctl start mysqld.service
4.6、设置远程连接(可选)
因我时在某云上,所以需要设置我本地连接,如果是在自己虚拟器可跳过此步骤

4.6.1、进入 mysql 命令行
在这里插入图片描述

4.6.2、更新 系统表(user)
update mysql.user set host="%" where user=“root”;
在这里插入图片描述

4.6.3、设置 root 密码
– 切换数据库
use mysql;
– 执行语句
ALTER USER ‘root’@’%’ IDENTIFIED WITH mysql_native_password BY ‘123’;
– 刷新修改后的权限
flush privileges;
– 退出
exit;
4.6.4、测试

在这里插入图片描述

5、将应用程序发布后的包上传
5.1、创建文件夹(用于存放应用程序包)
mkdir -p /var/www/web
5.2、ftp 上传应用程序文件到 上一步创建的文件夹(/var/www/web/)中
6、Redis 安装(可选)
如果项目中没有用到 Redis 可以跳过此步骤

6.1、下载、解压、编译
wget http://download.redis.io/releases/redis-6.0.6.tar.gz
tar xzf redis-6.0.6.tar.gz
cd redis-6.0.6
dnf install tcl
make
6.2、编译测试
make test
6.3 迁移到指定的目录(可选)
mkdir -p /usr/local/soft/redis
cd /usr/local/soft/redis/
mkdir bin
mkdir conf
cd bin/
cp /redis-6.0.6/src/redis-cli ./
cp /redis-6.0.6/src/redis-server ./
cd …/conf/
cp /redis-6.0.6/redis.conf ./

配置 redis-server 的 配置文件为 /usr/local/soft/conf/redis.conf

/usr/local/soft/redis/bin/redis-server /usr/local/soft/redis/conf/redis.conf

检查端口是否在使用

netstat -anp | grep 6379
6.4、使用 systemd 方式守护 redis 进程

6.4.1、编辑 redis.service 文件
vim /lib/systemd/system/redis.service
 6.4.2、设置redis.service 内容
[Unit]
Description=RedisAfter=network.target

[Service]
Type=forkingPIDFile=/var/run/redis_6379.pid
ExecStart=/usr/local/soft/redis/bin/redis-server /usr/local/soft/redis/conf/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPIDPrivateTmp=true

[Install]
WantedBy=multi-user.target
6.4.3、重载系统
systemctl daemon-reload
6.4.4、设置开机启动及其他指令

开机自启

systemctl enable redis

启动

systemctl start redis

查看状态

systemctl status redis

停止

systemctl stop redis
7、配置.Net Core 应用程序的守护进程
7.1、编辑 aspnetCore.service 文件
文件名 自定义,这里我起名为 aspnetCore.service

vim /lib/systemd/system/aspnetCore.service
7.2、编辑内容
[Unit]
Description=AspnetCoreDemo running on Centos8

[Service]# 应用程序所在的文件目录
WorkingDirectory=/var/www/web/
ExecStart=/usr/bin/dotnet /var/www/web/Carefree.AspNetCoreDemo.dll
Restart=always

如果dotnet服务崩溃,10秒后重新启动服务

RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=AspNetCoreDemo
User=root
#Production:生产环境 Development:开发环境
Environment=ASPNETCORE_ENVIRONMENT=Development
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target
7.3、重载系统及设置开机启动

重载系统

systemctl daemon-reload

开机自启动

systemctl enable aspnetCore.service
8、Nginx 代理
8.1、编辑配置文件
vim /etc/nginx/conf.d/web.conf
8.2、编辑内容
server
{
listen 80;
location /
{
proxy_pass http://localhost:5000;
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;
proxy_set_header X-Forwarded-For
proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
8.3、编辑 nginx.conf 文件

在这里插入图片描述

8.4、验证配置文件是否正确及加载配置文件

验证配置文件

nginx -t

加载配置文件

nginx -s reload
至此我们的应用程序可正常访问了。如有何问题可与我联系,共同学习。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Centos7发布说明 环境说明: 服务器系统:CentOS 7.2.1511 相关工具:Xshel、Xftp 服务器软件软件:.netcorenginx、supervisor 准备好发布的程序 安装.NET Core SDK for CentOS7 打开网址:https://www.microsoft.com/net/core#linuxcentos 复制如下命令,单步执行: sudo yum install libunwind libicu curl -sSL -o dotnet.tar.gz https://go.microsoft.com/fwlink/?LinkID=835019 sudo mkdir -p /opt/dotnet && sudo tar zxf dotnet.tar.gz -C /opt/dotnet sudo ln -s /opt/dotnet/dotnet /usr/local/bin 输入 dotnet –info 来查看是否安装成功 配置Nginx 下载安装Nginx,单步执行如下命令: curl -o nginx.rpm http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm rpm -ivh nginx.rpm yum install nginx systemctl start nginx 来启动nginx systemctl enable nginx 来设置nginx的开机启动(linux宕机、重启会自动运行nginx不需要连上去输入命令)。 配置防火墙 命令:firewall-cmd --zone=public --add-port=80/tcp --permanent(开放80端口) 命令:systemctl restart firewalld(重启防火墙以使配置即时生效) 测试nginx是否可以访问。 配置nginxASP.NET Core应用的转发 修改 /etc/nginx/conf.d/default.conf 文件,将文件内容替换为: server { listen 80; location / { proxy_pass http://localhost:5000; 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; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值