ASP. NET Core Application release and Deployment Guide

Source: Internet
Author: User
Tags dotnet stop script nginx reverse proxy

First, prefaceWhat is the main content of this article?
    1. Publish the project to a local directory
    2. Transfer items to the server and configure startup & Boot Auto-start
    3. Configure the reverse proxy as an access entry for Nginx
This article environmental information
    • Development environment:
Use Tools & Versions
Operating system Windows 10
Development tools Visual Studio 2017 (15.7.5)
Sdk . NET Core SDK 2.1
Transfer tool FlashFxp
    • Deployment environment
Use Tools & Versions
Linux Server CentOS 7
Sdk . NET Core SDK 2.1
Transfer tool Vsftp
Reverse Proxy Nginx 1.12.2
preparatory work
    • ASP. NET Core Sample Project
Items Description
Sdk . NET Core SDK 2.1
Project templates ASP. NET Core Web App (MVC)
Project name Helloweb
Solution Name Helloweb
Solution root Directory D:\Projects\Test\HelloWeb

After the project is created, you need to modify the Program.cs file
Manually specify the startup URL as: http://*:5000

  public class program {public static void main (string[] args) {createwebhostbuilder (args). Build (). Run (); } public static IWebHostBuilder createwebhostbuilder (string[] args) = Webhost.createdefaultbuilder (args). Usestartup<startup> (). Useurls ( "http://*:5000");}          

http://*:5000Can be compatible,, and http://localhost:5000 http://127.0.0.1:5000 easy to http://所在机器ip:5000 test after we deploy to Linux server

    • CentOS 7 Deployment environment

CentOS 7 Virtual machine installation: Https://ken.io/note/hyper-v-course-setup-centos

    1. Deploying the FTP service: Https://ken.io/note/centos-quickstart-ftpserver
    2. Deploying the. NET Core SDK 2.1:https://ken.io/note/centos7-.netcore2.1-setup
    3. Deploying Nginx:https://ken.io/note/centos-quickstart-nginx-setup
second, publish to the local1. Command Line Publishing

operation on cmd command line

#进入项目根目录(HelloWeb.csproj所在目录)d: && cd D:\Projects\Test\HelloWeb\HelloWeb#执行publish命令dotnet publish -c release#dotnet publish -h可以查看publish可选参数#-c release 表示以Release方式进行编译
2. Visual Studio Graphical interface operation
    • In the item's right mouse button menu, select: Publish

    • Select a folder on the left and create a configuration

    • Select Publish in the Publish Actions panel

3. Run the test locally

After publishing is complete, you can view the published file output in D:\Projects\Test\HelloWeb\HelloWeb\bin\Release\netcoreapp2.1\publish.

On the cmd command line operation:

#进入发布输出目录d: && cd D:\Projects\Test\HelloWeb\HelloWeb\bin\Release\netcoreapp2.1\publish#启动应用dotnet HelloWeb.dll#启动成功会输出以下信息Hosting environment: ProductionContent root path: D:\Projects\Test\HelloWeb\HelloWeb\bin\Release\netcoreapp2.1\publishNow listening on: http://[::]:5000Application started. Press Ctrl+C to shut down.

Access via browser: localhost:5000, verify if normal

Iii. Deployment1. Environment Configuration & Start-up Test

Connect to the CENTOS7 server via Xshell

    • Create a Site directory and authorize
#创建站点根目录sudo mkdir -p /webroot/helloweb#创建站点应用目录sudo mkdir -p /webroot/helloweb/app#创建站点日志目录sudo mkdir -p /webroot/helloweb/logs#目录授权sudo chmod 777 /webroot/helloweb/appsudo chmod 777 /webroot/helloweb/logs
    • Open ports
#添加可访问端口sudo firewall-cmd --add-port=5000/tcp --permanent#重新加载防火墙策略sudo firewall-cmd --reload
    • Launch the App

Transfer to/webroot/helloweb/app via FTP

Start with command:

#进入app目录并通过dotnet命令启动站点cd /webroot/helloweb/app/dotnet HelloWeb.dll#启动成功后,将会输出:Hosting environment: ProductionContent root path: /webrootNow listening on: http://[::]:5000Application started. Press Ctrl+C to shut down.

This time through the browser access to http://{centos-ip}:5000 can

If the site style is not loaded properly, it should be 404 when accessing static files under Wwwroot.
This is because ASP. NET core defaults to the directory where the command executes as the application root to read the file
Therefore, be sure to execute the dotnet command in the directory where the HelloWeb.dll is located, otherwise you will not be reading static files in the Wwwroot directory

2. Configure Startup Scripts

Show how it starts, and if you close the Xshell connection window, the app is turned off. So you can start with the Nohup command, the script example:

nohup dotnet HelloWeb.dll &

Nohup because it is a private boot, the application is closed to find the process ID before it can be closed, so it is more convenient to configure the startup, stop the script to operate more easily

    • To create a startup script

Create a start.sh file

sudo vi /webroot/helloweb/start.sh

Script content:

#!/bin/shCD $ (cd  "$ (dirname" $ ")"; pwd) app_name=helloweb.dllecho  " Start begin ... "echo  $APP _name CD appnohup dotnet  $APP _name >>. /logs/info.log 2>> /logs/error.log &CD. Sleep 5if test $ (pgrep-f  $APP _ name|wc-l)-eq 0then echo  "start Failed "else echo " start successed "fi           
    • To create a Stop script

Create a stop.sh file

sudo vi /webroot/helloweb/stop.sh

Script content:

#!/bin/shCD $ (CD $ (dirname "$") "; pwd) App_name=helloweb.dllprocess= ' ps-ef|grep  $APP _name|grep-v grep |awk  ' {print $} ' while: do kill-9  $PROCESS >/dev/null 2>&1 if [$-ne 0]; then break else continuefidone echo  ' stop success! '       
    • Script Test
#启动应用sh /webroot/helloweb/start.sh#启动成功输出start begin...HelloWeb.dllstart successed#停止应用sh /webroot/helloweb/stop.sh#停止成功后输出stop success!
Boot & Reverse proxy configuration1. Configure boot-up
    • To mark a stop script as an executable file
sudo chmod +x /webroot/helloweb/stop.sh
    • Create a Helloweb service
#创建服务文件sudo vi /usr/lib/systemd/system/helloweb.service#文件内容[Unit]Description=hellowebAfter=network.target[Service]WorkingDirectory=/webroot/helloweb/appExecStart=/usr/bin/dotnet /webroot/helloweb/app/HelloWeb.dllExecStop=/webroot/helloweb/stop.shRestart=alwaysRestartSec=10[Install]WantedBy=multi-user.target

If you have more than one version of the. NET core environment, remember to replace the/usr/bin/dotnet with the corresponding path

    • Set up service boot & start service
#设置服务开机启动sudo systemctl enable helloweb#启动服务sudo systemctl start helloweb
2, Nginx reverse proxy configuration
    • Create a Helloweb site configuration
#新建配置文件sudo vi /etc/nginx/conf.d/helloweb.conf#反向代理配置内容server {    listen       80;        #监听80端口    server_name  helloweb.mydomain.com; #绑定的域名    location / {            #转发或处理 proxy_pass http://localhost:5000; } error_page 500 502 503 504 /50x.html;#错误页 location = /50x.html { root /usr/share/nginx/html; }}
    • Heavy-duty Nginx configuration
sudo nginx -s reload
    • Open firewall port
#添加可访问端口sudo firewall-cmd --add-port=80/tcp --permanent#重新加载防火墙策略sudo firewall-cmd --reload
    • Access test:

Point the helloweb.mydomain.com to the server IP by setting the local hosts

Then access through the browser: helloweb.mydomain.com you can

Above, if in doubt, please contact me: https://ken.io/home/about

    • This article was first published: 2018-07-22
    • This text link: https://ken.io/note/asp.net-core-publish-deploy-guide

ASP. NET Core Application release and Deployment Guide

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.