Deploy ASP.NET Core Application on CentOS

01 Jan 2020 25 min See Original (spanish)

 

In this post we will see how to install the .NET Core runtime on our CentOS system so we can run applications written in .NET within our Linux environment.

Together with our previous post, we will use a web application with NGINX.

 

1 - Add the Microsoft repository

As we saw before, we need to install the Microsoft repository, since it does not come by default with a Linux installation. 

To do this, run the following command. 

$ sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm

 

2 - Install the .NET Core SDK and runtime

Currently the most recent version is 3.1 so you can change the version number as needed, whether higher or lower. 

To install the SDK simply run the following command

$ sudo yum install dotnet-sdk-3.1

and to install the runtime we will run a very similar command

$ sudo yum install dotnet-runtime-3.1

 

3 - Install the ASP.NET Core runtime

Since we are going to run a web page, we will also install the ASP.NET runtime 

To do so, run this command and you're good to go. 

$ sudo yum install aspnetcore-runtime-3.1

Now we have the netcore and aspnetcore 3.1 runtimes installed on our Linux server. Remember, if you want to use any other runtime you will need to install it manually.

 

4 - Publish the application 

At this step, go to Visual Studio, or the command line if you prefer.

In Visual Studio right click on the web project (or console app) -> publish 

publicar aplicacion asp.net core

Or its command line version:  dotnet publish -c Release

The default path for published files will be $Project\bin\Release\netcoreapp3.1\publish\

4.1 - "Self contained" deployment 

If you do not want to depend on the .NET Core version on the server, you can deploy your application using a command called "self contained deployment" with the following command dotnet publish -c Release -r <rid> --self-contained. This means it does not matter if the server has version 1.1 or 3, your app will work perfectly on the server. 

5 - Copy the files to the server

Ideally all these processes should be done with continuous delivery or continuous deployment, but for now - since it is the first time - we will do the steps more manually. 

As we saw a few posts ago, we need to have FTP installed on our server so we can copy the files inside the publish folder to our server.

On the server, you can choose any folder (we will configure the Linux service to read from this folder later), typically /var/www is used, and you create a folder for each web page, in this case /var/www/webEjemplo

 

6 - Run our web app 

Now we have copied the files, but as you can see, there is a number of .dll and other files that do not make our life much easier.

To run an ASP.NET Core application on Linux, go to the folder where you copied the files and run the command dotnet ejemploApp_assembly.dll in my example case it's dotnet webejemplo.dll and that will launch or run the web page inside your Linux server. 

You will see a message in the terminal telling you the app is listening on the url http://localhost:5000 (you can change the port in the app manually)

 

6.1 - Creating a service in Linux

Right now, as things are configured, we need to run the dotnet command every time we restart the server, and well, that's not ideal.

So let's automate its execution.

First, go to the folder where Linux services are stored

cd /etc/systemd/system

And here we will create a service file; to make things clearer, we'll add the extension .service, so create the file:

sudo vi webEjemplo.service

and paste the following content - remember to change the path to your application 

[Service]
WorkingDirectory=/var/www/webEjemplo
ExecStart=/usr/bin/dotnet /var/www/webEjemplo/ejemploApp_assembly.dll
Restart=always
# RestartService after 10secs if dotnet service crashes
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=ejemploApp
user=NOMBRE_USUARIO
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=fasle

[Install]
WantedBy=multi-user.target

Save the file, using :wq!

And we just need to enable the service 

sudo systemctl enable webEjemplo.service

You can check its status with the following command:

sudo systemctl status webEjemplo.service

If everything is correct, you will see a message like this

webEjemplo.service - webEjemplo App runing in fucking CentOS 7
   Loaded: loaded (/etc/systemd/system/webEjemplo.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-12-28 22:43:36 CET; 13h ago
 Main PID: 16371 (dotnet)
    Tasks: 17
   CGroup: /system.slice/netmentor.service
           └─16371 /usr/bin/dotnet /var/www/webEjemplo/ejemploApp_assembly.dll

 

7 - Configure NGINX as a reverse proxy

With this, our application is running on our server on localhost. This means you CANNOT access it from the outside.

To access it from outside, we need to point our NGINX web server  to the URL where the application is listening, in our case localhost:5000

To do so, go to the NGINX path

cd /etc/nginx

If you run the ls command you'll see lots of files and folders; one of them is a file called nginx.conf which contains the basic configuration for the NGINX server.

View the file, and make sure it contains the following line inside the http brackets: include /etc/nginx/conf.d/*.conf;

This line indicates that it will include all files with the .conf extension in the /etc/nginx/conf.d/ folder

Exit the file and move to that folder with 

cd /etc/nginx/conf.d

Create a file, I recommend one file per site, although you could create a single file for all web apps. 

sudo vi webEjemplo.conf

Paste the following content - remember to change the info for your web

server {
    server_name webEjemplo.es;
    location / {
        proxy_pass         http://localhost:5000;
       }
}

As you can see, it is very simple. Let's explain it quickly

Inside the server brackets we need to include one of our destinations. For example, if we have 10 web apps, we should have 10 "server" options in the file. 

  • server_name contains the URL(s) to listen on. In this case, netmentor, it could be netmentor.es you can include more than one, separating them with commas. For example, you could use the IP as we saw when setting up the NGINX web server
  • Location is where we send our internal request on our server.

In our case, it is sent to the app we just published, which is listening on localhost:5000

For the changes to take effect, you must restart the nginx service, run

sudo systemctl restart nginx.service

7.1 - Redirect to www with NGINX

A very important aspect of the website is to use the www subdomain. For that, we need to redirect any calls to our web, without a subdomain, to www

The process is very simple, you have to change the one we created earlier to www.webEjemplo.es and add a new one, which contains the redirect.

The file will look like this: 

server {
    listen 80;
    server_name www.webEjemplo.es;
    location / {
        proxy_pass         http://localhost:5000;
       }
}

server {
    listen 80;
    server_name webEjemplo.com;
    return 301 http://www.webEjemplo.com$request_uri;
}

As you can see, besides changing the main server section, we have created another server section and inside it we have added a single rule, which redirects to www.webEjemplo.com passing along the corresponding URI.

For the changes to take effect, you must restart the service. 

sudo systemctl restart nginx.service

You will need to add the www subdomain in your DNS zone on the VPS server for it to work correctly 

 

Conclusion

In today's post we've learned how to install both ASP.NET Core and .NET Core on our CentOS server.

So you can publish a web application, which is accessible from the outside using NGINX as a reverse proxy.

This post was translated from Spanish. You can see the original one here.
If there is any problem you can add a comment bellow or contact me in the website's contact form

Uso del bloqueador de anuncios adblock

Hola!

Primero de todo bienvenido a la web de NetMentor donde podrás aprender programación en C# y .NET desde un nivel de principiante hasta más avanzado.


Yo entiendo que utilices un bloqueador de anuncios como AdBlock, Ublock o el propio navegador Brave. Pero te tengo que pedir por favor que desactives el bloqueador para esta web.


Intento personalmente no poner mucha publicidad, la justa para pagar el servidor y por supuesto que no sea intrusiva; Si pese a ello piensas que es intrusiva siempre me puedes escribir por privado o por Twitter a @NetMentorTW.


Si ya lo has desactivado, por favor recarga la página.


Un saludo y muchas gracias por tu colaboración

© copyright 2025 NetMentor | Todos los derechos reservados | RSS Feed

Buy me a coffee Invitame a un café