zoukankan      html  css  js  c++  java
  • centos 生产环境部署 asp.net core

    参考:
    https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx
    https://docs.microsoft.com/en-us/dotnet/core/linux-prerequisites
    https://docs.microsoft.com/zh-cn/dotnet/core/install/linux-package-manager-centos7
    https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer

    发布程序

    dotnet publish --configuration Release
    

    服务器安装 net core runtime

    sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
    sudo yum update -y
    
    #使用2.1
    sudo yum install aspnetcore-runtime-2.1
    #使用3.1
    sudo yum install aspnetcore-runtime-3.1
    #查看版本
    dotnet --info
    

    服务守护

    创建文件/etc/systemd/system/kestrel-helloapp.service

    [Unit]
    Description=Example .NET Web API App running on Ubuntu
    
    [Service]
    WorkingDirectory=/var/www/helloapp
    ExecStart=/usr/bin/dotnet /var/www/helloapp/helloapp.dll #服务运行的命令行
    Restart=always
    RestartSec=10 # 崩溃后10秒重启
    KillSignal=SIGINT
    SyslogIdentifier=dotnet-example
    User=root # 以root身份运行,建议改为其他普通用户
    Environment=ASPNETCORE_ENVIRONMENT=Production
    Environment=ASPNETCORE_URLS=https://+:443;http://+:80;http://+:5000 #通过环境变量设置端口,+也可改为 * 或 0.0.0.0
    Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
    
    [Install]
    WantedBy=multi-user.target
    

    可选:上面kestrel-helloapp.service通过ASPNETCORE_URLS环境变量设置了开放的端口,也可通过UseUrls代码强行指定

    2.x:
    public static IWebHost BuildWebHost(string[] args)
    {
        return WebHost.CreateDefaultBuilder(args)
                .UseUrls("http://+:5000;") //通过UseUrls使用特定端口
                .UseStartup<Startup>()
                .Build();
    }
    
    3.x:
    public static IHostBuilder CreateHostBuilder(string[] args) =>
    	Host.CreateDefaultBuilder(args)
    		.ConfigureWebHostDefaults(webBuilder =>
    		{
    			webBuilder.UseStartup<Startup>()
    			.UseUrls("http://+:5000;");
    		});
    

    服务启动

    systemctl enable kestrel-helloapp.service
    systemctl start kestrel-helloapp.service
    systemctl status kestrel-helloapp.service
    

    配置防火墙端口

    firewall-cmd --permanent --zone=public --add-port=5000/tcp
    firewall-cmd --reload
    

    nginx转发

    修改程序,支持代理转发

    using Microsoft.AspNetCore.HttpOverrides;
    
    services.Configure<ForwardedHeadersOptions>(options =>
    {
        //options.KnownProxies.Add(IPAddress.Parse("192.168.56.10"));
        options.ForwardedHeaders =
                ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
    });
    
    app.UseForwardedHeaders();
    

    添加nginx配置

    /etc/nginx/conf.d/xx.conf

    server {
        listen        80;
        server_name   example.com *.example.com;
        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 $http_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;
        }
    }
    
  • 相关阅读:
    【贪心】POJ1017:Packets
    【贪心】POJ2393:Yogurt factory
    【贪心】POJ3190:Stall Reservations
    【递归】地盘划分
    【递归与递推】青蛙过河
    【搜索】POJ1242:Rescue
    单词方阵(dfs)
    反向因子Inverse Factorial
    P1604 B进制星球
    抵制克苏恩(记忆化搜索)
  • 原文地址:https://www.cnblogs.com/wswind/p/centos-aspnetcore-publish.html
Copyright © 2011-2022 走看看