Hosting Asp.Net Core Application on Raspberry Pi

In recent times one of the most discussed frameworks of web development is asp.net core. It is the very latest version of asp.net framework from Microsoft. One of the biggest advantages of asp.net core is it’s cross-platform. That means you can host asp.net core application on Windows, Linux, and Mac.

Among single board computers, raspberry pi is the pioneer and widely used in IoT world. The interesting things may come to our mind that can we combine this two amazing technology together? Host an asp.net core application into raspberry pi? The answer is yes asp.net core support arm build and raspberry pi runs in raspbian which is based on debian. So we can host asp.net core application into a raspberry pi. Today I am going to discuss about the process of hosting asp.net core application on a raspberry pi.

Prerequisites

  1. Raspberry Pi 2 or higher. I will use version 3 model B
  2. An SD card installed Raspbian Jerry  or higher. I will user Raspbian Stretch.
  3. Asp.net core 2.0 or higher.

Create and Asp.Net Core 2.0 Application

I am going to create a mvc application using CLI but you can also use visual studio. At first create a folder named AspNetCoreRaspbian this will be the name of your application. From inside the folder run below command.

This will create and mvc application named AspNetCoreRaspbian. Now you can modify this application to implement your desire functionalities. But to keep this simple I will not do any modification. I will host using default content. Now build the application using the command

To host it on raspberry pi we need to publish the application for linux arm. Using below command we can do that

This will publish the application for arm based computer running linux. So we can use it to host on raspberry pi.

Preparing Pi

All Though asp.net core can be hosted as self contained service but we still need to pepare some underlying things to let asp.net core to work correctly. At first we will update the raspbian then install  libunwind8 into it.

If everything install successfully then our pi is ready to host asp.net core application. Now copy published content from bin\Release\netcoreapp2.0\linux-arm\publish to the pi. From the content root run below command to make the application binary executable

Now run the application using

if everything goes fine you will see your application running into http://localhost:5000 This time it will only accessible from localhost. We can expose the app itself to the internet but its not good practice to expose kestrel directly to the internet. To expose it to intenet world we better use a reverse proxy server. We can use any full features web server like Apache, nginx etc. in our case we will use nginx for it.

Setup nginx

Install the nginx server using below command

Now we need to modify the nginx server so that it pass the request coming to port 80 to our application localhost port 5000. Open /etc/nginx/sites-available/default. Look for the section localhost / Replace it with the content below

After saving the file start nginx using the command

At this point if everything goes fine then you can now browse your application from the internet. Try to browse it from a computer browser. You should see the content.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *