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