Linux CentOS7 deploy ASP.NET Core application and configure Nginx reverse proxy server

Preface:

  This article is mainly on how to build the operating system Linux CentOS7 .NET Core operating environment and ASP.NET Core publish applications, and configure Nginx reverse proxy server. Because the company's project has been hosted on Window Server IIS, .NET Core project for hosting on a Linux server is very curious. Because of curiosity, so there was this article about how to configure .NET Core configuration of the operating environment in Linux CentOS7 system deployment projects, and reverse proxy servers.

First, development tools introduced

Xshell:

  Is a powerful secure terminal emulation software that supports SSH1, SSH2, and TELNET protocol Microsoft Windows platform. Xshell through the Internet to a remote host a secure connection as well as its innovative design and features to help users enjoy their work in a complex network environments.

Xftp:

  Is a powerful SFTP , the FTP  file transfer software. Use the Xftp later, MS Windows users can securely  UNIX / Linux  to transfer files between and Windows PC. Xftp can simultaneously meet the needs of novice and advanced users. It uses the standard Windows style guide, its simple interface can closely work together with other Windows applications, in addition it also offers many powerful features for advanced users.

Of course, these tools provide free usage rights non-commercial use, you only need to fill in the corresponding information.

Download: https://www.netsarang.com/en/free-for-home-school/

Two, .NET Core environment to build

Environment Download: https://dotnet.microsoft.com/download

Check the operating system version:

lsb_release -a

Install .NET Core Runtime [install .Net Core Operating Environment]:

sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm

sudo yum update

sudo yum install aspnetcore-runtime-2.2

Install .NET Core SDK【安装.Net Core SDK】:

sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm

sudo yum update

sudo yum install dotnet-sdk-2.2

View version information for the installation environment:

dotnet --info

Third, the project release and deployed to the server in CentOS

Use Visual Studio to publish a project:

Deployment Options framework relies

The reason: because we have in front of .Net Core applications needed to run the operating environment and the corresponding SDK installed the up.

Select portable target runtime

The reason: can be applied to all operating systems.

Will release good xftp files uploaded to the server:

Use xshell to see whether the project successfully uploaded:

Run the project:

First to enter the project directory:

cd MyDotNetApplication/

Run the project:

dotnet FirstCore.dll -d &

See if the background to run:

wget http://localhost:5000

or

curl  http://localhost:5000

Run the program prompts questions:

warn: Microsoft.AspNetCore.Server.Kestrel[0]
      Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'.

This warning, then, IPV6s loopback when unreachable, which means you can only access the internal server via a local, but the site can not be accessed by external network ip.

 Solution:

First Stop Website: 
View to run the program port: jobs -l 

ending the current program: kill the port number 

to re-run the program, and finally set the following access methods do like this [course run just to see the effect of the following, we need to configure Nginx and guard service]: 
DOTNET FirstCore.dll --server.urls = "http: // * : 5000"

Then enter the external network ip + port number access, successful access, the page as shown below:

Fourth, configure Nginx reverse proxy:

Using a reverse proxy server advantages:

   Kestrel is ideal for providing dynamic content from ASP.NET Core. However, Web services like server (such as IIS, Apache or Nginx) as feature-rich. The reverse proxy server can uninstall the HTTP server workloads, such as serving static content, the cache request, 
compression and HTTPS requests terminal. Reverse proxy server may reside on a dedicated computer, may also be deployed with the HTTP server.

Reverse proxy server receives the HTTP request from the network and forwards the request to (edge ​​server) the Kestrel, requesting the flowchart as follows:

Download, install, start Nginx command:

First add CentOS 7 EPEL Source:

sudo yum install epel-release 

(Expand) the role of EPEL source added:

  EPEL (Extra Packages for Enterprise Linux) is built by the Fedora community project to provide high-quality packages for RHEL releases its derivatives (such as CentOS, etc.). After the installation EPEL source, as in the Fedora as by "yum install package names", you can install a lot of software needs to be compiled before installation, as well as some of the more commonly used software popular software, such as the now popular nginx, redis Wait. After EPEL source is installed, you can use EPEL easily install the update.

Use the following command yum install Nginx:

sudo yum install nginx

Because it is first installed Nginx, explicitly start by running the following command: 

sudo service nginx start

or: 

sudo systemctl start nginx

View Nginx whether to start: 

Check the status of the Nginx: 

systemctl status nginx 

Ps -ef list with the list of processes, nginx then filtered through grep:

ps -ef | grep nginx

 Nginx is enabled when the system startup settings:

sudo systemctl enable nginx

Viewing System Firewall status:

service iptables status

As shown below my firewall is turned off: 

 If the firewall system is not closed, enter the following command to allow HTTP and HTTPS communication:  

sudo firewall-cmd --permanent --zone=public --add-service=http 
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload 

Enter the server's IP address in your browser: http: //123.xx.xx.88/ to verify whether Nginx ran successfully:

As shown below can see the default page then the forwarding of Nginx Nginx successful operation:

Modify Nginx configuration file:

Nginx using Vim command to open the default configuration:

To configure Nginx to reverse proxy server forwards the request to the ASP.NET Core app, edit the default configuration file Nginx /etc/nginx/nginx.conf open it in a text editor and replace the contents the following:

 vim /etc/nginx/nginx.conf

Original default configuration to the content server replaces the following:

 

 

 Replaced:

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 $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
} 

Save editing, and exit the text editor:

esc +: wq to save the changes and exit the vim editor

Verify the default file Nginx configuration is correct:

nginx -t

 

Check whether the port is listening in normal instances:

netstat -an | grep 80

Restart Nginx:

nginx -s reload

 Last Visitors ASP.NET Core application directly through the server ip address:

 

Guess you like

Origin www.cnblogs.com/Can-daydayup/p/10911067.html