blog-details

Prerequisites

Install .Net Core SDK and .Net Core Runtimes

Before installing .NET, you’ll need to:

  • Update yum
  • Register the Microsoft key.
  • Register the product repository.
  • Install required dependencies.

This only needs to be done once per machine.

sudo yum update -ysudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm

Install the .NET Core SDK

Update the products available for installation, then install the .NET Core SDK. In your terminal, run the following command.

If using net core 3.1:

sudo yum install dotnet-sdk-3.1

If using net core 2.1:

sudo yum install dotnet-sdk-2.1

in this case i’m using net core 2.1

Install the ASP.NET Core runtime

Update the products available for installation, then install the ASP.NET runtime. In your terminal, run the following command.

If using net core 3.1:

sudo yum install aspnetcore-runtime-3.1

If using net core 2.1:

sudo yum install aspnetcore-runtime-2.1

If you want to create test project console:

In order to confirm the result, you can create and run a “Hello World” demo .NET Core app:

cddotnet new console -o helloworldAppcd helloworldAppdotnet run

The dotnet new console -o helloworldApp command will create a directory named helloworldApp in your home directory and then use the console template to generate app files in the newly created directory.

Upon executing the dotnet run command, you will see the Hello World! message in the console.

Create a .NET Core web app

Now, create and run a .NET Core application of type razor. Just remember that "Razor Pages" is a new application template of .NET Core MVC that is designed for page-oriented scenarios:

cddotnet new razor -o myfirstwebappcd myfirstwebappdotnet run

By executing the dotnet run command above, you will start a .NET Core web app listening on: http://localhost:5000.

(Optional): add Support for Remote Access

By default, the ASP.NET Core 2.1 project will run on the Kestrel web server on localhost.

If you want to be able to access the website remotely from other computers, you’ll need to bind it differently.

To do this, I went into the newly created project files and opened Program.cs. In here I added a call to the UseUrls method as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace atcsmart
{
    public class Program
    {
        public static void Main(string`` args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string`` args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseUrls("http://0.0.0.0:5000");
                    webBuilder.UseStartup<Startup>();
                });
    }
}

This binds the ASP.NET Core website to all IPs on the server, instead of just localhost, which is the default.

Publish Aplication

Having all of the development tasks done, you can use the following commands to publish your web app:

cd ~/myfirstwebappdotnet publish

You can find the published web app in the ~/myfirstwebapp/bin/Debug/netcoreapp3.1 directory

(Optional): Setup Supervisor to keep your .NET Core web app online

Process crashes happen. In order to keep your web app online, it’s a good idea to have a process managment tool, such as Supervisor, to monitor and restart the crashed web app processes.

(Optional) Before using supervisor you must install EPEL YUM:

yum install epel-release

On CentOS 7, you can install Supervisor using YUM:

sudo yum install supervisor -y

Next, you need to setup a dedicated Supervisor config file for your web app:

cd /etc/supervisord.d 
sudo vi myfirstwebapp.conf

hoặc

nano  /etc/supervisord.d/myfirstwebapp.conf

Populate the file:

`program:apphoclaptrinh`
command=dotnet tuhoclaptrinh.top.dll
directory=/home/app/netcore/tuhoclaptrinh.top/bin/Debug/netcoreapp3.1/publish
environment=ASPNETCORE__ENVIRONMENT=Production
user=root
stopsignal=INT
autostart=true
autorestart=true
startsecs=1
stderr_logfile=/var/log/tuhoclaptrinh.err.log
stdout_logfile=/var/log/tuhoclaptrinh.out.log

Save and quit:

:wq!

Next, you need to modify the default supervisord config file to include the config file we've created:

sudo cp /etc/supervisord.conf /etc/supervisord.conf.bak
sudo vi /etc/supervisord.conf

Find the last line:

files = supervisord.d/*.ini

Replace it:

files = supervisord.d/*.conf

Save and quit:

:wq!

Start Supervisor and set it to automatically start at system startup:

sudo systemctl start supervisord.service
sudo systemctl enable supervisord.service

Load the new Supervisor settings:

sudo supervisorctl reread
sudo supervisorctl update

Now, you can use the following command to show the app’s status:

sudo supervisorctl status

The output will look like the following:

myfirstwebapp                    RUNNING   pid 3925, uptime 0:08:45

Next, you can try to kill the app’s process by specifying the pid 9612:

sudo kill -s 9 9612

Wait for a while, and then check the status again:

sudo supervisorctl status

This time, the output will indicate that the app did break down and automatically started:

myfirstwebapp                    RUNNING   pid 3925, uptime 0:00:06

(Optional): Install Nginx as a reverse proxy

In order to facilitate visitors’ access, you can install Nginx as a reverse proxy to pass web traffic to port 5000.

Install Nginx using YUM:

sudo yum install nginx -y

Edit the default Nginx config file as follows:

sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.baksudo vi /etc/nginx/nginx.conf

Find the following segment within the http {} segment:

location / {}

 

server {
    listen        80;
    server_name  tuhoclaptrinh.top  *.tuhoclaptrinh.top;
    location / {
        proxy_pass   http://127.0.0.1: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;
    }
}
server {
    listen  443 ssl;
    server_name  tuhoclaptrinh.top app.tuhoclaptrinh.top;


        ssl_certificate /etc/letsencrypt/live/tuhoclaptrinh.top/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/tuhoclaptrinh.top/privkey.pem;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;

        # Improve HTTPS performance with session resumption
        ssl_session_cache shared:SSL:50m;
        ssl_session_timeout 1d;

        # DH parameters
        ssl_dhparam /etc/nginx/ssl/dhparam.pem;
        # Enable HSTS
        add_header Strict-Transport-Security "max-age=31536000" always;
}

Insert six lines of reverse proxy settings between the braces as shown below:

location /
 {
proxy_pass http://127.0.0.1:5000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

Save and quit:

:wq!

Start the Nginx service and then set it to start at system startup:

sudo systemctl start nginx.servicesudo systemctl enable nginx.service

(Optional) If you publish not in folder www linux

you must non active selinux to access your folder application

vi /etc/sysconfig/selinux

search SELINUX and change with this :

SELINUX=disabled

restart server

sudo shutdown -r now

Setup firewall rules

Before visitors can access the .NET Core web app on ports 80 and 443, you need to modify firewall rules. so addSupport for remote Access and open port 5000 in firewall

sudo firewall-cmd --add-port=5000/tcp --permanentsudo firewall-cmd --reloadsudo firewall-cmd --list-all

 

Đánh giá bài viết