Objective
Now that ASP. NET Core RC2 has been officially released, you can refer to the following links:
https://blogs.msdn.microsoft.com/dotnet/2016/05/06/net-core-rc2-improvements-schedule-and-roadmap/
https://blogs.msdn.microsoft.com/dotnet/2016/05/16/announcing-net-core-rc2/
Although documentation is available on publish (see Resources section), there are several issues with the current documentation:
- For Ubuntu, our company servers are CentOS, so the installation in the documentation is inappropriate for us, so here's a detailed description of how to publish the. NET Core RC2 application on the CentOS 7.x release.
- This document is obviously outdated, not updated in time, and is now based on RC1, not RC2. (for example, you need to configure commands, which is obviously outdated because commands settings have been removed from RC2)
- This document has errors, clean up the Socket resource section, should use-s instead of-F to determine whether the socket file exists.
Environment Building Operating System
CentOS 7.1 or above is required and CentOS 7.2 is used here
Install the. NET Core
You can refer to the official installation instructions for. NET Core, Https://www.microsoft.com/net/core#centos. A. NET Core installation package cannot be downloaded on a corporate virtual machine when it is actually installed. Solution:
- You can download it locally (which is slower) and upload it to the server root directory.
- Unzip the file into the/root/dotnet directory
- Create a connection file for/root/dotnet/dotnet files in the/usr/local/bin directory: Ln-s/root/dotnet/dotnet/usr/local/bin/dotnet
- Create a new directory where you can run dotnet new, dotnet Restore, dotnet Run to verify that the dotnet run environment is installed successfully
Installing Nginx
Main references Nginx Official document: https://nginx.org/en/linux_packages.html
- Create a/etc/yum.repos.d/nginx.repo with the following content:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
- Yum installs Nginx, you can install the
Installing Supervisor
- Install Setuptools,https://pypi.python.org/pypi/setuptools First
- Then run Easy_install supervisor to complete the supervisor installation.
- To configure supervisor, add supervisord.conf under the/etc/supervisor directory, as follows:
[supervisord]
logfile = /tmp/supervisord.log
logfile_maxbytes = 50MB
logfile_backups=10
loglevel = info
pidfile = /tmp/supervisord.pid
nodaemon = false
minfds = 1024
minprocs = 200
umask = 022
user = root
identifier = supervisor
directory = /tmp
nocleanup = true
childlogdir = /tmp
strip_ansi = false
- Run Supervisord under Bash, if there is no error, it means supervisor can run the
- Configured as a service. In the/usr/lib/systemd/system directory, add the Supervisord.service file with the following content:
[Unit]
Description=Supervisor daemon
[Service]
Type=forking
ExecStart=/usr/bin/supervisord
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s [Install]
WantedBy=multi-user.target
- Start Services, service Supervisord start
Website release
Modify the URL of the site listener to be configurable so that it is easy to modify, the sample code is as follows
IConfiguration configuration = new ConfigurationBuilder().AddJsonFile("config.json").Build();var host = new WebHostBuilder() .UseConfiguration(configuration) .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .Build(); host.Run();
{"server.urls": "http://+:5000" //注意这里用+,而不是localhost,用localhost的话只能在本机访问,其他机器不能访问}
Run the dotnet Publish command to publish the Web site and upload the site to a Linux server. Then go to the Site directory, run dotnet App.dll, start the website. See if the http://serverip:5000/can respond properly in the browser. If this succeeds, it means that the. NET core-related release has been successful. Next, start configuring Nginx.
Configuring Nginx as a reverse proxy server
- Modify Nginx's running account as root. Edit the/etc/nginx/nginx.conf file to change the user configuration to root
- Modify the URL of the Web site listener to listen for UNIX sockets, as modified here: Http://unix:/var/aspnet/WebApplication1/kestrel.sock
- Modify the Nginx configuration, add the webserver configuration of. NET core, and change the Proxy_pass configuration as follows:
server{
listen 8080;
server_name localhost;
location / {
proxy_pass http://unix:/var/aspnet/WebApplication1/kestrel.sock;
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;
}
}
- Access http://serverip:8080 Verify that the response is OK
Some points to be aware of
- Nginx is the web site that forwards requests to. NET core through a UNIX socket, which is implemented by writing a request to a socket file. So running Nginx account needs to have write access to the socket file, I am simply configured as the root account
- If you encounter a shutdown outside the dotnet process, the. sock file may not be automatically cleaned up, resulting in a. Sock resource leak. At this point you need to clean down the. Sock. Otherwise, you will encounter the following error when starting the Web site:
Unhandled Exception: System.AggregateException: One or more errors occurred. (Error -98 EADDRINUSE address already in use) ---> Microsoft.AspNetCore.Server.Kestrel.Networking.UvException: Error -98 EADDRINUSE address already in use
- When listening to a socket, the directory where the. sock file is located must first exist, not exist, or it should be created first, or the following error is reported:
Unhandled Exception: System.AggregateException: One or more errors occurred. (Error -13 EACCES permission denied) ---> Microsoft.AspNetCore.Server.Kestrel.Networking.UvException: Error -13 EACCES permission denied
- If there is a problem, you can look at the Nginx Error.log and Access.log to troubleshoot the problem. Path is:/var/log/nginx
Monitoring website applications
- Add the web.sh Bash script under the root of the website, as follows, the main purpose of this script is to clean up the socket resources before starting the Web site, to avoid the site cannot start because the socket is not cleaned:
if [ -S "/var/aspnet/WebApplication1/kestrel.sock" ]; then
rm "/var/aspnet/WebApplication1/kestrel.sock"
fi
dotnet /root/WebApp2/WebApplication1.dll
- In the above supervisord.conf file, append the following configuration:
[program:WebApplication1]
command=bash /root/WebApp2/web.sh
autostart=true
autorestart=true
stderr_logfile=/root/WebApp2/WebApplication1.err.log
stdout_logfile=/root/WebApp2/WebApplication1.out.log
environment=Hosting__Environment=Production
user=root
stopsignal=INT
- Restart Supervisor Service
service supervisor stop
service supervisor start
Resources
- Https://docs.asp.net/en/1.0.0-rc2/publishing/linuxproduction.html
. NET Core RC2 Cross-platform deployment