Detailed introduction: ASP. NET Core is released to the Linux server, asp. netcore
For a long time. the applications developed by NET can only run on Windows platforms. However, due to cost concerns, the booming Internet companies in China are using a large number of free Linux platforms. NET is a stunt, but it cannot be used in a wide range of ways ,. NET platform is considered only suitable for developing internal enterprise application systems.
In June 27, 2016, Microsoft officially released. NET Core 1.0, ASP. NET 1.0, and Entity Framework Core 1.0. They all use Windows, OS X, and Linux operating systems .. NET Core, as a new generation of cross-platform and open-source. NET platform, has attracted much attention. Some people say that. NET programmers are coming in spring.
This article describes how to publish an ASP. NET Core website to a Linux server.
Environment preparation
The operating system and software versions used in this article are as follows:
- Visual Studio 2017 Enterprise Edition
- . NET Core 1.1.
- CentOS 7X64
Create and release an ASP. NET Core website project
1. Create a project
Open VS2017, create a project, and select ASP. NET Core Web application (. NET Core)
Select a Web application template.
Run F5 to test whether the website is normal.
2. Add the Url configuration file
By default, the project uses the http: // localhost: 5000 Url for listening. We can add a configuration file to modify the Url address at any time.
Add a hosting. json file to the project root directory. The file content is as follows (192.168.57.7 is the Server IP address ):
{ "server.urls": "http://192.168.57.7:8080"}
Edit the Program. cs file and change it to the following content:
public static void Main(string[] args) { var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("hosting.json", optional: true) .Build(); var host = new WebHostBuilder() .UseKestrel() .UseConfiguration(config) .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .UseApplicationInsights() .Build(); host.Run(); }
3. Publish a project
Right-click Project-release and select folder mode.
Compress the published PublishOutput folder into zip format and upload it to the CentOS server.
Install. NET Core
1. download the latest. NET Core version.
Official Website: https://www.microsoft.com/net/download/linux
I use dotnet-centos-x64.1.1.1.tar.gz.
2. Upload and decompress the downloaded Installation File.
$ # Create a dotnet folder $ mkdir ~ /Dotnet $ # copy the dotnet installation file to the dotnet folder $ cp dotnet-centos-x64.1.1.1.tar.gz ~ /Dotnet $ # decompress the Installation File $ tar-xzf ~ /Dotnet/dotnet-centos-x64.1.1.1.tar.gz $ # Add soft connection, you can use $ ln-s ~ globally ~ /Dotnet/usr/local/bin $ # Check whether the installation is successful. The version $ dotnet-version is displayed.
Start a website
$ # Decompress the previously uploaded compressed website file. If unzip is not installed, run yum install-y unzip to install $ unzip ~ /Dotnet/PublishOutput.zip $ # first disable the firewall $ systemctl stop firewalld. service $ # Start a website $ cd PublishOutput $ dotnet TestAspNetCoreWeb. dll $ if the error "Failed to bind to CoreCLR" is reported, run yum install-y libunwind
Started successfully. You can access http: // 192.168.57.7: 8080.
Install and configure the daemon (Supervisor)
Use Supervisor to monitor ASP. NET Core website applications so that the website can continue to run. Otherwise, the website will stop after exiting Shell.
$ # Install Supervisor $ yum install python-setuptools $ easy_install supervisor $ # configure Supervisor $ mkdir/etc/supervisor $ echo_supervisord_conf>/etc/supervisor/supervisord. conf
Modify the supervisord. conf file and modify the final content of the file as follows:
Create the conf. d directory, and create the TestAspNetCoreWeb. conf file under the directory.
The file content is as follows:
[program:TestDotNetCoreWeb]command=dotnet TestAspNetCoreWeb.dll directory=~/dotnet/PublishOutputautorestart=truestderr_logfile=/var/log/TestDotNetCoreWeb.err.logstdout_logfile=/var/log/TestDotNetCoreWeb.out.log environment=ASPNETCORE_ENVIRONMENT=Production user=rootstopsignal=INT
Run supervisord and check whether the process has taken effect.
$ supervisord -c /etc/supervisor/supervisord.conf$ ps -ef | grep TestDotNetCoreWeb
If the configuration file is modified, run the supervisorctl reload command to reload it.
Install and configure Nginx
Http://dl.fedoraproject.org/pub/ access
Download the appropriate version of epel and upload it to the server.
Such as: http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm
$ # Install epel $ rpm-ivh epel-release-7-9.noarch.rpm $ # install nginx $ yum install nginx $ # start nginx $ systemctl start nginx $ # Add nginx To The SELinux whitelist, otherwise, a 502 error is reported. $ Yum install policycoreutils-python $ cat/var/log/audit. log | grep nginx | grep denied | audit2allow-M mynginx $ semodule-I mynginx. pp $ # test whether nginx is normal $ curl http: // 127.0.0.1
Modify the server section in the/etc/nginx. conf configuration file to the following content and configure Nginx to listen to the previous website.
server { listen 80 ; location / { proxy_pass http://192.168.57.7:8080; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }
$ # Reload the configuration file after modification $ nginx-s reload
After configuration, you can use http: // 192.168.57.7 to access the website.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.