CentOS部署ASP.NetCore5.0

目录

一、安装数据库MySql8

二、发布Asp.Net Core服务

三、配置Nginx


一、安装数据库MySql8

1、查看是否已安装mysql

yum repolist enabled | grep "mysql"

2、在MySql官网中找到yum源rpm安装包(MySQL :: Download MySQL Yum Repository),复制下载链接:https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

3、进入CentOS,输入以下命令

wget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

yum localinstall mysql80-community-release-el7-1.noarch.rpm

yum install mysql-community-server

//配置mysql

systemctl start mysqld       //启动mysql服务

//获取初始密码(建议使用5.8以上的版本,5.7以下版本不生成初始密码)

grep 'temporary password' /var/log/mysqld.log     

mysql -uroot -p        //本地MySQL客户端登录,输入密码(不显示)后回车

//修改密码,默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。

ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';        

//配置远程登陆

update user set host='%' where user ='root';

flush privileges;

二、发布Asp.Net Core服务

1、安装donet运行环境

官方教程:在 CentOS Linux 上安装 .NET - .NET | Microsoft Learn,在终端输入:

sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm

sudo dnf install dotnet-sdk-5.0

sudo dnf install aspnetcore-runtime-5.0

输入dotnet --info查看是否安装成功

image

2、启动Asp.Net Core程序

将Asp.Net Core 发布后的文件上传到服务器上(推荐使用MobaXterm),上传目录推荐 /var/www下(root目录下会出现访问权限问题)。

//启动程序

cd /var/www/donet

dotnet xxxx.dll    //区分大小写

image

出现以上画面,你就成功了,如果有误,你先在你本地window下dotnet  xxx.dll看看有没有错误,一般在你本地上没问题,在服务器也是没问题的。

现在外网还不能访问你的5000端口的,这个dotnet默认只监听本地的5000端口,所以外网无法访问,

解决方法:

a、就是在命令后面加上参数urls:

dotnet xxxx.dll --urls http://*:5000 

image

b、使用nginx代理后面会讲到。

3、将Asp.Net Core程序发布为服务

官方说明使用 Nginx 在 Linux 上托管 ASP.NET Core | Microsoft Learn

cd /etc/systemd/system

vi kestrel-helloapp.service    //创建服务文件,文件名中不要有下划线,否则报错

//编辑文件

[Unit]
Description=helloapp_service

[Service]
WorkingDirectory=/var/www/helloapp    #服务目录修改为自己的目录
ExecStart=/usr/bin/dotnet /var/www/helloapp/helloapp.dll    #这里也要修改为对应的程序
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

//服务设置

systemctl start kestrel-helloapp.service     //开启服务

systemctl status kestrel-helloapp.service    //查看服务状态

systemctl enable kestrel-helloapp.service    //设置开机启动

systemctl restart kestrel-helloapp.service    //重启服务

三、配置Nginx

1、安装nginx

//给centos添加一个yum源,yum的应用商城上没有nginx

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-70.el7.ngx.noarch.rpm 

yum install -y nginx       //安装nginx

systemctl start nginx      //启动nginx

systemctl enable nginx   //将nginx设为开机启动

2、配置nginx

cd /etc/nginx

image

如上图所示,nginx.conf是主配置文件,打开这个文件,下面有一句include /etc/nginx/conf.d*.conf,说明还有一些配置在conf.d目录里面,而我们主要要设置的都在conf.d里面


image

cd ./conf.d   // 进到conf.d目录里,看到里面就一个default.conf文件

vi test.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;
    }
}

保存退出

nginx -t        //运行nginx检查

nginx -s reload  //重启nginx

参考文章:

CentOS7安装mysql8.0.12_mysql 8.0 community server 3.4 kb/s | 2.6 kb 00:00-CSDN博客

https://www.cnblogs.com/caijt/p/10978324.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Centos7发布说明 环境说明: 服务器系统:CentOS 7.2.1511 相关工具:Xshel、Xftp 服务器软件软件:.netcore、nginx、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是否可以访问。 配置nginx对ASP.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、付费专栏及课程。

余额充值