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 

 

posted @ 2018-09-06 11:40  狂想NICE  阅读(1138)  评论(0编辑  收藏  举报