centos7下.netcore环境搭建、创建并.netcore程序、设置开机自动启动

介绍centos7下.netcore环境搭建、创建并.netcore程序、设置开机自动启动.netcore程序(注册为服务运行)

一:.netcore环境搭建

1、执行命令

1

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

2、更新yum

1

sudo yum update

3、安装.netcore 

1

sudo yum install dotnet-sdk-2.2 -y

4、检测是否安装成功:

二:创建.netcore web程序并运行

1、定位到你想放置项目的位置,创建项目

1

cd /home<br>dotnet new webApp -o testApp --no-https

2、直接发布项目到项目文件夹

1

2

cd testApp

dotnet publish -o /home/testApp/Release

3、测试运行.netcore程序

1

2

cd release

dotnet testApp.dll

4、如需通过宿主机访问,需修改Program.cs代码,方式多种,这里采用添加host.json方式

首先更改CreateWebHostBuilder方法代码如下

        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            var configuration = new ConfigurationBuilder().SetBasePath(Environment.CurrentDirectory)
                              .AddJsonFile("host.json")
                              .Build();
            return WebHost.CreateDefaultBuilder(args).UseConfiguration(configuration)
                                                     .UseStartup<Startup>();
        }

在项目根目录添加host.json文件 

{
  "urls": "http://*:5000"
}

对于.net core3.0以下的版本,可采用以下命令:

  webBuilder.UseStartup<Startup>().UseUrls("http://*:3011");

 

5、通过浏览器访问 http://虚拟机ip:5000,效果如下:

三:设置.netcore程序开机启动

1、创建服务文件

vim /etc/systemd/system/testApp.service

  写入如下内容:

[Unit]
Description=testApp for centos7

[Service]
WorkingDirectory=/home/testApp/Release
ExecStart=/usr/bin/dotnet /home/testApp/Release/testApp.dll
Restart=always
RestartSec=10  # Restart service after 10 seconds if dotnet service crashes
SyslogIdentifier=dotnet-testApp
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

2、设置开机自动启动testApp.service服务

systemctl enable testApp.service

3、开启服务,并查询状态

systemctl start testApp.service
systemctl status testApp.service

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值