Detailed introduction of Asp. Net Core Release and deployment (MacOS + Linux + Nginx) and macosng.pdf

Source: Internet
Author: User

Detailed introduction of Asp. Net Core Release and deployment (MacOS + Linux + Nginx) and macosng.pdf

Preface

In the previous article, we mainly introduced the Dotnet Core Run command. This article mainly explains how to release and deploy Asp. Net Core programs in Linux.

Directory

  • Create a WebApp Project
  • Released to Linux and Mac OS
  • Use Nginx for reverse proxy

Create a WebApp Project

In the Asp. Net Core project, we use the dotnet new-t WebApp command to create a new empty Web application.

Here is my Mac:

The following commands are used:

Mkdir HelloWebApp: Creates a folder named HelloWebApp.

The dotnet new-t Web command uses a Web template to create a WebApp Mvc application.

After creating an application, run the dotnet restore and dotnet run commands to test our application.

You can see that the operation is successful.

Enter http: // localhost: 5000 in the browser to check the effect.

Ps: under Safari, the address bar of the browser cannot see the port number, which is actually port 5000.

Released to Linux and Mac OS

If you publish an application, you need to use the dotnet publish command. You can see some command parameters that can be used by using the -- help parameter.

 -f|--framework <FRAMEWORK>   Target framework to compile for  -r|--runtime <RUNTIME_IDENTIFIER> Target runtime to publish for  -b|--build-base-path <OUTPUT_DIR> Directory in which to place temporary outputs  -o|--output <OUTPUT_PATH>   Path in which to publish the app  --version-suffix <VERSION_SUFFIX> Defines what `*` should be replaced with in version field in project.json  -c|--configuration <CONFIGURATION> Configuration under which to build  --native-subdirectory    Temporary mechanism to include subdirectories from native assets of dependency packages in output  --no-build       Do not build projects before publishing

Run dotnet publish directly to use the default release path. If Published 1/1 projects successfully is displayed, the release is successful. Go to the netcoreapp1.0 folder under the Debug folder under the bin folder, and then you will see a publish folder. This is the default release generated folder. in this folder, we can see all the dependent assembly files of our program.

It is necessary to describe the subfolders In the publish folder after publishing.

  • Appsettiong. json application configuration file
  • . Net fx system Assembly referenced by the refs Application
  • Runtimes runtime environment, you can see that the folder contains win7, linxu, mac OS, and so on. This shows that our application is cross-platform.
  • The views folder stores our mvc view files.
  • The wwwroot folder stores the js library, css style sheets, and images used by the front end.

Then we switch the working directory to the published publish folder. Use dotnet HelloWebApp. dll to test whether the released program runs normally.

Use Nginx for reverse proxy

  1. Mac OS
  2. Linux (Ubuntu)
  3. Notes

Reverse Proxy, that is, some paths of our application are handed over to Nginx for processing, such as static files and images. Another part of dynamic processing is handled by Kestrel. In this way, we can reduce the pressure on backend Kestrel and configure Server Load balancer in Nginx.

Another important advantage is that the cache in the Web will be processed on the proxy server. Let's take a look at the figure below:

As for how to add a cache that can be recognized by the proxy server, follow the blog post I published later.

Mac OS

To install the dotnet environment, see the official website https://www.microsoft.com/net/core?macos.

1. Open the command line in mac, enter brew install nginx, and install nginx first.

2. After the installation is complete, enter nginx-v to check whether the installation is successful. Nginx version: nginx/1.10.1 indicates that the installation is successful.

3. Configure the nginx proxy.

After nginx is installed, the default configuration file path is in the/usr/local/etc/nginx folder. Find the nginx. conf configuration file in this folder and open it with Visual Studio Code. On the Server node, find the location node listening to port 80 and modify the configuration as follows:

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; }

Save and exit. Then run the sudo nginx-s reload command to reload the configuration.

Then, open the browser and enter http: // localhost. Then, we can see that our site has been accessed through nginx.

Ps: under Safari, the address bar of the browser cannot see the port number, which is actually port 80.

Linux (Ubuntu)

To install the dotnet environment, see the official website https://www.microsoft.com/net/core?ubuntu.

First, create a new folder in Ubuntu and copy the published publish folder to Liunx. Then, test whether it can run normally.

1. Open a new command line window in linux (Ubuntu) and enter apt-get install nginx to install nginx first.

2. After the installation is complete, enter nginx-v to check whether the installation is successful. Nginx version: nginx/1.4.6 indicates that the installation is successful.

yxd@ubuntu:~$ sudo nginx -vnginx version: nginx/1.4.6 (Ubuntu)

Test whether nginx runs successfully. Open the browser and enter http: // localhost to check whether the following interface is displayed.

3. Configure the nginx proxy.

After nginx is installed, the default configuration file path is in the/etc/nginx/sites-available/default file. Switch the working directory to/etc/nginx/sites-available/and run the sudo gedit default command to open the default file. On the Server node, find the location node listening to port 80 and modify the content as follows:

server { listen 80; #root /usr/share/nginx/html; #index index.html index.htm; # Make site accessible from http://localhost/ server_name localhost; 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; }}

Save and exit. Then run the sudo nginx-s reload command to reload the configuration.

Then, open the browser and enter http: // localhost. Then, we can see that our site has been accessed through nginx.

Notes

1. When using the nginx proxy to access the site, make sure that the command line window of the command dotnet run HelloWebApp. dll is enabled.

2. Run the following command to run a command line in the background process.

nohup dotnet HelloWebApp.dll &

In this case, you can close the command line window.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.