ASP. NET Core Publishing and Deployment (MacOS + Linux + Nginx) Preface
In the previous article, I focused on the Dotnet Core Run command, which is mainly about how to publish and deploy ASP. NET core programs in Linux.
For more information on how to deploy in Jexus, see my other article:
Http://www.cnblogs.com/savorboard/p/dot-net-linux-jexus.html
Directory
- Create a new WEBAPP project
- Publish to Linux,mac OS
- Using Nginx for reverse proxy
Create a new WEBAPP project
In the ASP. NET Core project, we use dotnet new -t WebApp
commands and create a new, empty WEB application.
Here's what I have on my Mac:
The main ones are the following commands:
mkdir HelloWebApp
The command is to create a folder named Hellowebapp.
dotnet new -t Web
This command uses a Web template to create a new WebApp MVC application.
After creating the application, use the dotnet restore
and dotnet run
commands to test our application.
You can see that it has been successfully run.
Let's look at the effect by opening the browser input http://localhost:5000.
Ps:safari below, the browser address bar does not see the port number, which is actually port 5000.
Publish to Linux,mac OS
If the application is published, we need to use dotnet publish
the command to --help
see some command parameters that can be used by using parameters.
-f|--framework <FRAMEWORK> Target FrameworkTo compileFor-r|--runtime <RUNTIME_IDENTIFIER> Target RuntimeTo publishFor-b|--build-base-path <OUTPUT_DIR> DirectoryIn which to place temporary outputs-o|--output <OUTPUT_PATH> PATH in which to publish the app--ver Sion-suffix <VERSION_SUFFIX> defines what ' * ' should is replaced with on 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
We run directly dotnet publish
using the default publishing path, and when you see Published 1/1 projects successfully
it, the description has been published successfully. Then go to the netcoreapp1.0 folder under the Debug folder under the Bin folder and you will see a publish folder. This is the default publish-generated folder where you can see all of the assembly files that our program depends on.
It is necessary to explain the subfolders within the Publish folder after publishing.
appsettiong.json 应用程序的配置文件refs 应用程序引用的.net fx系统程序集runtimes 运行时环境,可以看到里面的文件夹包含 win7、linxu,mac os 等,说明我们这个应用程序是跨平台的。views 这个文件夹存放的就是我们的 mvc 的视图文件。wwwroot 文件夹,存放的是前端使用的 js 库,css 样式表,和图片等。
Then we switch the working directory to the published publish folder. Use dotnet HelloWebApp.dll
a program that has been published after the test is running correctly.
Using Nginx for reverse proxy
- Mac OS
- Linux (Ubuntu)
- Precautions
Reverse proxy , that is, a part of our application path to the Nginx processing, such as static files, pictures and so on. Another part of the dynamic is handed over to Kestrel for processing. This reduces the pressure on our back-end Kestrel, as well as the ability to configure load balancing in Nginx.
Another important advantage is that the cache in the Web is handled in the proxy server step. Let's look at the following picture:
As to how to add a cache that the proxy server can recognize, follow the blog post that I posted later.
Mac OS
Install dotnet Environment See the official website Https://www.microsoft.com/net/core#macos.
1, open the command line in the Mac, enter the brew install nginx
first install Nginx.
2. After the installation is complete, enter to nginx -v
see if the installation is successful. The display nginx version: nginx/1.10.1
indicates that the installation was successful.
3, configure Nginx Proxy.
After installing Nginx, the default profile path is in the /usr/local/etc/nginx
folder. Locate the nginx.conf configuration file in this folder, open it using Visual Studio Code, and in the Server
node, locate the location node that listens on 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 use the sudo nginx -s reload
command to reload the configuration.
Then we open the browser input http://localhost, found at this time through Nginx to visit our site.
Ps:safari below, the browser address bar does not see the port number, which is actually port 80.
Linux (Ubuntu)
Install dotnet Environment See the official website Https://www.microsoft.com/net/core#ubuntu.
First create a new folder in Ubuntu and copy the Publish folder we have published to Liunx. Then test to see if it works.
1. Open a new command-line window in Linux (Ubuntu), enter the apt-get install nginx
first installation of Nginx.
2. After the installation is complete, enter to nginx -v
see if the installation is successful. The display nginx version: nginx/1.4.6
indicates that the installation was successful.
[email protected]:~$ sudo nginx -vnginx version: nginx/1.4.6 (Ubuntu)
Test Nginx is running successfully, open the browser, enter http://localhost to see if the following interface is displayed.
3, configure Nginx Proxy.
After installing Nginx, the default configuration file path is in the /etc/nginx/sites-available/default
file. Switch working directory to /etc/nginx/sites-available/
, use the sudo gedit default
command to open the default file. In the Server
node, locate the location node where the 80 port is listening, and modify the contents 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 use the sudo nginx -s reload
command to reload the configuration.
Then we open the browser input http://localhost, found at this time through Nginx to visit our site.
Precautions
1, when using the Nginx proxy to access the site, keep command dotnet run HelloWebApp.dll
line window must be opened.
2. You can use the following command to have a command line run in a background process.
nohup dotnet HelloWebApp.dll &
You can turn off the command-line window at this time.
MacOS + Linux + Nginx