ASP. NET Core website published to Linux server

Source: Internet
Author: User
Tags dotnet centos server hosting

Used for a long time. NET development of the application can only run on the Windows platform, and the current domestic booming internet companies due to cost considerations, a lot of use of free Linux platform, which makes. NET has a stunt but can not get the vast amount of space,. NET platform is considered only suitable for the development of enterprise internal application system.

June 27, 2016, Microsoft officially released. NET Core 1.0, ASP 1.0, and entity Framework Core 1.0, with Windows, OS X, and Linux operating systems.. NET core as a next-generation cross-platform, open source. NET platform has attracted much attention, some people say. NET programmer comes in the spring.

This article describes how to publish an ASP. NET core Web site to a Linux server.

Environment preparation

The operating system and software versions used in this article are as follows:
Visual Studio 2017 Enterprise Edition
. NET Core 1.1
CentOS 7 X64

Create a publish ASP. NET core Web site project

1. Create a project
Open VS2017, create a new project, select the ASP. NET Core Web Application


Select the Web Application template.

Direct F5 test Site is normal.

2. Add URL URLs configuration file
The project listens using the http://localhost:5000 URL by default, and we can add a configuration file to modify the URL at any time.
Add a Hosting.json file to the project root directory with the following contents (8080 ports can be changed to your liking):

{  "server.urls": "http://*:8080"}

Edit the Program.cs file and modify it to read as follows:

        public static void Main(string[] args) { var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("hosting.json", optional: true) .Build(); var host = new WebHostBuilder() .UseKestrel() .UseConfiguration(config) .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .UseApplicationInsights() .Build(); host.Run(); }

3. Publish the Project
Right-click Project-Publish, select folder mode.
Compress the published Publishoutput folder into a zip format and upload it to the CentOS server.

Install the. NET Core

1. Download the latest. NET Core version
Official website: https://www.microsoft.com/net/download/linux
The version I'm using is dotnet-centos-x64.1.1.1.tar.gz.
2. Upload and unzip the downloaded installation file.

# 创建dotnet文件夹$ mkdir ~/dotnet$ # 复制dotnet安装文件到dotnet文件夹下$ cp dotnet-centos-x64.1.1.1.tar.gz ~/dotnet$ # 解压安装文件$ tar -xzf ~/dotnet/dotnet-centos-x64.1.1.1.tar.gz$ # 添加软连接,可以在全局使用$ ln -s ~/dotnet/dotnet /usr/local/bin$ # 测试安装是否成功,成功会显示版本号$ dotnet –version 
Launch Web site
#解压之前上传的网站压缩文件,如果没有安装unzip,运行yum install -y unzip zip安装$ unzip ~/dotnet/PublishOutput.zip$ #先关闭防火墙$ systemctl stop firewalld.service$ #启动网站$ cd PublishOutput$ dotnet TestAspNetCoreWeb.dll$ 如果报错Failed to bind to CoreCLR,运行yum install -y libunwind 和 yum install -y icu


Start successfully, can access http://192.168.57.7:8080 open (192.168.57.7 is the virtual machine IP address, local access can also use localhost).

Install the configuration daemon (Supervisor)

Use Supervisor to monitor the ASP. NET core site application so that the site can run continuously, or the site will stop after exiting the shell.

# 安装Supervisor$ yum install python-setuptools$ easy_install supervisor$ #配置Supervisor$ mkdir /etc/supervisor$ echo_supervisord_conf > /etc/supervisor/supervisord.conf

Modify the supervisord.conf file to modify the final contents of the file as follows:

Create directory CONF.D, create files under directory testaspnetcoreweb.conf
The contents of the file are as follows:

[program:TestDotNetCoreWeb]command=dotnet TestAspNetCoreWeb.dll directory=/root/dotnet/PublishOutputautorestart=truestderr_logfile=/var/log/TestDotNetCoreWeb.err.logstdout_logfile=/var/log/TestDotNetCoreWeb.out.log environment=ASPNETCORE_ENVIRONMENT=Production user=rootstopsignal=INT

Run Supervisord and see if the process is in effect.

$ supervisord -c /etc/supervisor/supervisord.conf$ ps -ef | grep TestDotNetCoreWeb


If the configuration file is modified, use the command Supervisorctl reload to reload.
No effect can see if the log file has an error.

Installation Configuration Nginx

Visit http://dl.fedoraproject.org/pub/
Download the appropriate version of Epel and upload it to the server.
such as: http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm

$  #安装epel $ RPM-IVH epel-release-7-9.noarch.rpm$   #安装nginx $ yum install nginx$  #启动nginx $ Systemctl start nginx$  #将nginx添加至SELinux的白名单, otherwise 502 error will be reported. $ yum Install policycoreutils-python$ cat/var/log/ Audit/audit. log | grep nginx | grep denied | audit2allow-m mynginx$ semodule-i mynginx.pp$  #测试nginx是否正常 $ curl Http:// 127.0.0.1                

Modify the server section of the configuration file/etc/nginx/nginx.conf to configure the Web site before the Nginx listens.

server {Listen80 //192.168.57.7:8080; Proxy_http_version 1.1 $http _upgrade< Span class= "OT"; Proxy_set_header Connection keep-alive $host  $http _upgrade404/404.html40x.html {} Error_page 500 502 503 504/50x.html50x.html {}}     
#修改完后重新加载配置文件$ nginx -s reload$#设置开机启动$ systemctl enable nginx.service

Once the configuration is complete, you can use http://192.168.57.7 to access the site.

ASP. NET Core website published to Linux server

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.