Introduce centos7 .netcore environment to build, create and .netcore program, set the boot .netcore program (registered as a service to run) Auto
A: .netcore environment to build
1, execute the command
Sudo rpm Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
2, yum update
sudo yum update
3, installation .netcore
sudo yum install dotnet-sdk-2.2 -y
4, detects whether the installation was successful:
Two: Creating .netcore web and run
1, navigate to where you want to place the project, create project
cd /home
dotnet new webApp -o testApp --no-https
2, directly publish the project to the project folder
CD TestApp dotnet publish -o / home / TestApp / Release
3, the test program run .netcore
CD release dotnet testApp.dll
4. To access by the host, Program.cs need to modify the code, a variety of ways, this embodiment adopts added host.json
First change the code as follows CreateWebHostBuilder method
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>(); }
Add host.json file in the root directory of the project
{
"urls": "http://*:5000"
}
5, accessed through a browser http: // virtual machine ip: 5000, the effect is as follows:
3: setting .netcore program startup
1. Create a service file
vim /etc/systemd/system/testApp.service
Write the following:
[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, set at startup testApp.service Service
systemctl enable testApp.service
3, open the service, and query status
systemctl start testApp.service
systemctl status testApp.service