Linux-RedHat7.2 安装.net core2.0

1、添加dotnet产品Feed

sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
sudo sh -c 'echo -e "[packages-microsoft-com-prod]\nname=packages-microsoft-com-prod \nbaseurl=https://packages.microsoft.com/yumrepos/microsoft-rhel7.3-prod\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/dotnetdev.repo'

2、安装.NET Core SDK 2.0

sudo yum update
sudo yum install libunwind libicu
sudo yum install dotnet-sdk-2.0.0

3、查看安装状态

dotnet --info

4、新建一个console项目,运行

dotnet new console -o myApp
cd myApp
dotnet run

 

 

5、新建一个mvc项目、发布 、开机运行

 新建:

cd ..
dotnet new mvc -o mymvc

修改Startup.cs 文件,nginx反向代理使用。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

//添加引用
using Microsoft.AspNetCore.HttpOverrides;

namespace mymvc
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;

        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
            app.UseStaticFiles();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
            //添加下面的代码
            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
            });
            app.UseAuthentication();
        }
    }
}

发布

cd mymvc
dotnet publish -c Release

运行

cd bin/Release/netcoreapp2.0/publish
dotnet mymvc.dll

开机运行(/root 为实际文件目录)

vim /etc/systemd/system/kestrel-mymvc.service 

-- 内容如下:
[Unit]
Description=Example .NET Web MVC Application running on Centos7

[Service]
WorkingDirectory=/root/mymvc
ExecStart=/usr/bin/dotnet /root/mymvc/bin/Release/netcoreapp2.0/publish/mymvc.dll
Restart=always
RestartSec=10  # Restart service after 10 seconds if dotnet service crashes
SyslogIdentifier=dotnet-example
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production 

[Install]
WantedBy=multi-user.target
--启动
systemctl enable kestrel-mymvc.service 
systemctl start kestrel-mymvc.service 
systemctl status kestrel-mymvc.service 

--修改,重启
systemctl daemon-reload
systemctl restart kestrel-mymvc.service 

 

转载于:https://www.cnblogs.com/kuangxiangnice/p/9597445.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值