Installing and Running .NET Core on CentOS 7

.NET Core is a reimagined, open-source development framework developed by Microsoft along with its continually expanding community. Designed for cross-platform use, .NET Core empowers developers to build fast, modern applications across a wide range of environments.

This tutorial will guide you through the process of setting up .NET Core on a CentOS 7 server and launching a fully operational .NET Core-based web application.

Requirements Before Installation

  • An up-to-date CentOS 7 x64 server — for this guide, the server IP is 203.0.113.1.
  • Access to a user account with sudo privileges.
  • The server should already be updated to the latest stable release using the EPEL YUM repository.

Integrating the .NET Product Repository

.NET Core offers prebuilt binary packages for different operating systems. For CentOS 7, you can set up the Microsoft-signed YUM repository by running the commands below with sudo access:

sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
sudo sh -c 'echo -e "[packages-microsoft-com-prod]\nname=packages-microsoft-com-prod \nbaseurl= https://packages.microsoft.com/yumrepos/microsoft-rhel7.3-prod\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/dotnetdev.repo'

 

Install the Latest .NET SDK with YUM

After configuring the .NET YUM repository, you can proceed to install the latest version of the .NET SDK. This includes the .NET Core runtime along with essential dependencies. Use the following commands on your system:

sudo yum update -y
sudo yum install libunwind libicu -y
sudo yum install dotnet-sdk-2.1.4 -y

To ensure everything was installed successfully, you can build and launch a simple “Hello World” app using the .NET Core CLI:

cd
dotnet new console -o helloworldApp
cd helloworldApp
dotnet run

The command dotnet new console -o helloworldApp will generate a folder named helloworldApp in your home directory and use the console template to create all required project files inside it.

Running dotnet run will compile and execute the app, and you should see the output Hello World! printed to your terminal.

Build a .NET Core Razor Web Application

You can now create and launch a Razor-based .NET Core web application. Razor Pages are a part of the ASP.NET Core MVC ecosystem, specially designed for scenarios that are page-focused:

cd
dotnet new razor -o myfirstwebapp
cd myfirstwebapp
dotnet run

Executing the dotnet run command will initiate a .NET Core web server, which listens by default on http://localhost:5000.

Even if you’re using a command-line-only server, you can verify the app is running by opening another terminal window and entering:

curl http://localhost:5000

This command will display the HTML source of your web app’s default page. To terminate the app, simply press Ctrl + C.

To further develop your web application, you can modify files inside the generated project directory. For more guidance, consult the official .NET documentation.

Once your app is ready, use the commands below to publish it:

cd ~/myfirstwebapp
dotnet publish

The compiled and published web application will be located at:

~/myfirstwebapp/bin/Debug/netcoreapp2.0

(Optional) Using Supervisor to Keep the .NET Core Web App Running

Application processes can sometimes crash. To ensure continuous availability of your .NET Core web application, it’s wise to use a process management utility like Supervisor. It helps by automatically restarting your application if it goes down unexpectedly.

To install Supervisor on CentOS 7, run the following command:

sudo yum install supervisor -y

Next, create a dedicated Supervisor configuration file for your application:

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

Inside the configuration file, insert the following content:

[program:myfirstwebapp]
command=dotnet myfirstwebapp.dll
directory=/home/sudouser/myfirstwebapp/bin/Debug/netcoreapp2.0/
environment=ASPNETCORE__ENVIRONMENT=Production
user=root
stopsignal=INT
autostart=true
autorestart=true
startsecs=1
stderr_logfile=/var/log/myfirstwebapp.err.log
stdout_logfile=/var/log/myfirstwebapp.out.log

Save and exit the file by entering:

Now, update the main Supervisor configuration file to ensure it includes files with the .conf extension:

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

Scroll to the end of the file and locate:

files = supervisord.d/*.ini

Replace it with:

files = supervisord.d/*.conf

Save and close the file:

Enable and start the Supervisor service:

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

Load the updated Supervisor configuration settings:

sudo supervisorctl reread
sudo supervisorctl update

To view the current status of your application, execute the following:

The output will resemble:

myfirstwebapp RUNNING pid 3925, uptime 0:08:45

To simulate a crash, you can manually terminate the process using its PID:

Wait briefly and then recheck the application status:

The new output will confirm the app has been restarted automatically:

myfirstwebapp RUNNING pid 3925, uptime 0:00:06

(Optional) Configure Nginx as a Reverse Proxy

To allow easier access for users, you can set up Nginx to function as a reverse proxy that forwards incoming traffic to your .NET Core application running on port 5000.

Start by installing Nginx using the following command:

Next, edit the default Nginx configuration file:

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

Within the http {} block, find the existing location / {} section. Insert the following six lines between the curly braces:

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 exit the file:

Now, start the Nginx service and ensure it launches on system boot:

sudo systemctl start nginx.service
sudo systemctl enable nginx.service

Configure Firewall Access

To allow external traffic on ports 80 (HTTP) and 443 (HTTPS), apply the following firewall rules:

sudo firewall-cmd --zone=public --permanent --add-service=http
sudo firewall-cmd --zone=public --permanent --add-service=https
sudo firewall-cmd --reload

With all components now properly configured, your .NET Core application is accessible via http://203.0.113.1.

Conclusion

By following the steps outlined in this guide, you’ve successfully installed and configured a .NET Core environment on CentOS 7. From setting up the official Microsoft repository, installing the .NET SDK, and deploying a Razor-based web application, to configuring process monitoring with Supervisor and integrating Nginx as a reverse proxy — each component contributes to a stable and production-ready deployment workflow.

This setup not only ensures reliable application performance but also provides flexibility and maintainability for future development and scaling. With your .NET Core application now accessible via a public-facing URL, you are well-equipped to build and deliver robust web solutions on a Linux-based server.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: