Introduction: Build a Very Cheap ASP.Net 5 Core Web Server With BeagleBone AI (BBAI)
With all the cloud services offered by Microsoft, Amazon, and Google these days, one might think that a Web Server is an expensive part of owning a business or running a blog site. They are actually free! Webservers can be very light weight apps that can run on a number of devices for free. You can even run them on $5 devices called the ESP8266! To put it in context, home automation devices such as light switches and doorbell cams are all web servers. A web server is nothing more than a way to pass text and binary files using web sockets - which is a very basic part of all common operating systems such as Windows and Linux.
One little known fact is that every Windows computer shipped since the mid 90's shipped with a web server app already installed. You just go to the Control Planel Add/Remove Programs and dig out the IIS program and enable it. You instantly turned your machine into a webserver. Drop some HTML files in its default directory, configure your router to pass traffic to it, and you have a free web server!
With this Instructable, I'm going to get even more thrifty. Instead of tying up your PC as a web server, I'm going to use a BeagleBone AI (BBAI). It's a <$100 US single board computer that runs the Debian Linux Operating System. This approach would also work with the Raspberry Pi. I'm using the BBAI because of its perplexing approach to open source hardware documentation makes it sit in the bottom drawer of my project cabinet. This being the case, there's a chance you can pick up a used one on eBay very cheap (Wireless version is preferred). Another reason to use it is power consumption. Compared to a beastly 100W+ PC, these run on under 10W and are the size of a wallet.
Supplies
BeagleBone AI or BeagleBone Black (Wireless Preffered)
A laptop and an easy chair for a comfortable build.
Step 1: Good Stuff to Know
This section is not necessary instruction to pull off this Instructable, but it will help you understand what you are doing as you execute the listed Linux commands:
Step 2: Initial BBAI Setup
When you first get a BBAI, you'll need to get the latest image from the BeagleBoard site and configure it's WiFi. Here are all the steps needed:
- On your PC, download the latest image - be sure it is for BeagleBone AI:
https://beagleboard.org/latest-images
(I am using https://debian.beagleboard.org/images/am57xx-debi... - Flash the image to your SD card. I use the software called Etcher. Use an SD card that is sized 8 to 32 Gig. (other sizes have reported problems)
- After successfully flashing the SD card, put the card in BBAI, power it, and connect your PC to it by searching for available wifi from your PC:
SSID: BeagleBoneXXXX
Password: BeagleBone - Connect to the BBAI's Cloud9 development environment by navigating your PC to 192.168.8.1.
- Follow on screen instructions on what to type in Cloud9 to connect BBAI to your home wifi. I summarize this below. Note: if prompted for a root password, use temppwd - the default user is debian, but you should not need that.
sudo connmanctl⏎
scan wifi⏎ (wait a few seconds for a response in the terminal window)
services⏎ (copy the long string representing your wifi) agent on⏎
connect ⏎
quit⏎ - Expand the operating system to use the full SD card size you used:
sudo /opt/scripts/tools/grow_partition.sh⏎
sudo reboot⏎ this will reboot the machine and use the full size of your sd card - Update all your software packages in your image to the latest and greatest:
sudo apt update⏎
sudo apt upgrade⏎
This completes the base setup of a BBAI. We'll now install Apache - a very common Linux based web server.
Step 3: Install Apache for Linux
Apache is a Linux Web Server (again just a web socket app) that has been around forever. To make it play with the cutting edge .Net5 Core framework of Microsoft, we'll run it as what is called a "reverse proxy". First, lets get Apache installed and view its default web page.
To do so, type the following in the terminal:
cd ~ #takes you to your default home directory sudo apt-get install apache2 #if prompted for password, type temppwd. Type Y and hit enter if prompted to continue
You'll get some errors when it attempts to auto run at the end of the installation. It's because you already are using what is called a "Port" for Cloud9. Ports are used to secure channels for computers to talk to one another. So, we need to tell Apache to listen on a different port than the default of 80. We are now going to configure apache to run on port 8090 instead so we can still use Cloud9. We will use the "vi" editor to change it.
sudo vi /etc/apache2/ports.conf
In the 'vi' editor, we will change the port to 8090:
- Arrow down and over to just after the 80
- Press the insert key on your keyboard
- Type 90 - which should now show as pictured above (8090)
- Press the ESC key on your PC.
- Type :wq and hit enter (colon wq)
Yes, these commands seemed very awkward. VI is a very, very old editor. However, since it uses just keyboard keys to issue commands, it is also compatible with any environment you are accessing the file under such as Cloud9.
Now, we can start apache and enable it as a service so it loads with every power up of the BBAI.
sudo systemctl start apache2
sudo systemctl enable apache2
We now can test our newly installed basic web server, Apache. To do so, we need to know our host address. Type ifconfig in the terminal and locate your wlan inet address:
ifconfig
Mine is 192.168.1.32. So, to access it in a browser, I would type http://192.168.1.32:8090. See the pictures above for reference.
As you can see above, we have success!! If we wanted an old school static web page to blog on, we'd just edit the files in the directory /var/www/html and be good to go! Skip the next section to configure your router's firewall and you got yourself a great basic webserver.
However, we want to run the most cutting edge web server tech there is right now - .Net5 Core. It allows us to write dynamic web apps that can talk to databases and pull info from web APIs on the fly or even control the BBAI to move a robot. That would be awesome! So, let's proceed to the next page to learn how to do that.
Step 4: Install Microsoft's .Net5 Core Framework
You'd think this would be very complicated. Nope. Just a few lines of commands to install and a couple of config files to update. I must say, Microsoft already has a great tutorial on this here:
Install .NET on Debian - .NET | Microsoft Docs
However, for the BBAI, there are some twists requiring info here:
Manually install .NET on Linux - .NET | Microsoft Docs
That instructs your to install the ARM binaries from here:
Download .NET 5.0 (Linux, macOS, and Windows) (microsoft.com)
I got it sorted for you, so back to the terminal we go in Cloud9. You can copy this and do ctrl V for paste into Cloud9. Remember, if you get prompted for a password, it is temppwd.
NOTE: at the time I wrote this, the dotnet-sdk-5.0.300-linux-arm.tar.gz was the latest. This may have changed. So, visit the Download .Net 5.0 page above replace the filename below with the latest.
The mkdir command below will pause your screen and take some time. It will extract the zip file.
cd ~ wget "https://download.visualstudio.microsoft.com/download/pr/4bbb3a8d-e32a-4822-81d8-b2c570414f0a/aa7659eac0f0c52316a0fa7aa7c2081a/dotnet-sdk-5.0.300-linux-arm.tar.gz" DOTNET_FILE=dotnet-sdk-5.0.300-linux-arm.tar.gz export DOTNET_ROOT=$HOME/dotnet mkdir -p "$DOTNET_ROOT" && tar zxf "$DOTNET_FILE" -C "$DOTNET_ROOT" export PATH=$PATH:$DOTNET_ROOT
To ensure this worked, type dotnet:
dotnet
If you see "Usage: dotnet..." like the picture above, you had success!!!!
Step 5: Make a BBAI .Net5 Core Console App
You can now use .NetCore5 to make console apps. That's a low level app that can be used to do all sorts of things on the BBAI. You could even use it to write the brains of a robot to control and read the pins of the BBAI.
Let's do the simple Hello, World app. Go to the terminal again:
cd ~ dotnet new console -o myfirstapp cd myfirstapp dotnet run
It will take a while to compile and then run to show "Hello World". Whoever made the template omitted the comma after Hello. (lol)
To make it an executable and run it as a standalone application, type this:
dotnet build
After several seconds it should tell you that the Build succeeded. You then run it like this:
./bin/Debug/net5.0/myfirstapp
The bin/Debug stuff is the path where the executable files and the supporting files are stored. You could move that directory with "mv" to somewhere else or have it run at startup. That's getting into the world of coding which is out of scope of this Instructable, but feel free to ask any questions in the comments!
You getting this far means you now have considerable mechatronic and IoT mojo at your disposal.
Now, let's get Apache running as a reverse proxy for .Net5 Core so you have the most cutting edge Web Server on earth.
Step 6: Setup Apache As a .Net5 Core Reverse Proxy
We first need to make an apache config file so it knows to direct traffic to the webapp we create.
To open the "vi" editor, copy and paste this in the Cloud9 terminal:
sudo vi /etc/apache2/sites-available/000-default.conf
Copy this text:
<VirtualHost *:8090> ProxyPreserveHost On ProxyPass / http://127.0.0.1:5000/ ProxyPassReverse / http://127.0.0.1:5000/ ErrorLog /var/log/apache2/aspnetcoredemo-error.log CustomLog /var/log/apache2/aspnetcodedemo-access.log common </VirtualHost>
Once the editor is open, then type the following in Cloud9:
- insert key (delete the original contents)
- CTRL V to paste the stuff from above
- ESC :wq (escape key, then colon, then the letters wq)
You now have the config file you need in place. We now need to configure the .Net side:
sudo vi /etc/systemd/system/kestrel-helloapp.service
Copy this text below and do the insert, paste, escape steps as you did for the prior file:
[Unit] Description=My first .Net5 App [Service] WorkingDirectory=/var/www/helloapp ExecStart=/usr/local/bin/dotnet/dotnet /var/www/helloapp/helloapp.dll Restart=always # Restart service after 10 seconds if the dotnet service crashes: RestartSec=10 KillSignal=SIGINT SyslogIdentifier=dotnet-example User=root Environment=ASPNETCORE_ENVIRONMENT=Production [Install] WantedBy=multi-user.target
A key item here is that the Working Directory is /var/www/helloapp. That is where our published web app should go. So, let's make an app, build it, and publish it there to test our progress.
cd ~ sudo mkdir /var/www/helloapp #creates our publishing directory sudo chmod -R +777 /var/www/helloapp #gives permissions to apache and dotnet to work the directory dotnet new webapp -o helloapp #this will take a few seconds to process<br>cd helloapp<br>dotnet build #this will take a few seconds to process<br>dotnet publish -o /var/www/helloapp #this too will take a bit sudo chmod -R +777 /var/www/helloapp #lets the servers have permission over it
We need to do a few other things to have the webserver and .Net find its binaries:
sudo mv ~/dotnet /usr/local/bin sudo chgrp root /usr/local/bin/dotnet -R sudo chown root /usr/local/bin/dotnet -R
Now, we can activate the .Net server called Kestrel:
sudo systemctl enable kestrel-helloapp.service sudo systemctl start kestrel-helloapp.service sudo systemctl status kestrel-helloapp.service #to ensure it activated
If all worked well, then you should see that it is activated like the screenshot above. If not, repeat this section or comment below for help.
Finally, we make Apache act like a proxy:
sudo a2enmod proxy sudo a2enmod proxy_http sudo systemctl restart apache2 sudo systemctl status apache2 #to check the status to ensure it activated without errors
Now, go to your browser on your PC and visit the page. You should now see an elegant "helloapp" webpage. Success!!!
Step 7: Configure Your Router's Firewall for the World to See
At this point, you have a full fledged .NetCore5 Server. Now we'll open it up to the world.
Unfortunately, all router manufactures have their own firewalls, so I can only give you the gist of how to port forward. You will need to google "how to port forward for ...." for your particular router to get the exact details.
Here is how I do it:
- Log into my router by opening a browser and typing its admin address. Mine is 192.168.1.1.
- Click on the WAN tab
- Click on the Port Forwarding Tab
- Click Add Profile
- Setup the forwarding parameters
- Profile Name: BBAI Web Server
- External Port: 80
- Internal Port: 8090
- Internal IP Address: this is the one discovered by ifconfig in Step 2
While you are in your router configuration, look for your wide area network address. It will not start with 192.
Finally - test it from an "outside device". Go to your PC or mobile phone browser and type in http://WAN_address where WAN_address was found above.
Success!! If not, comment below.
The next thing is to get a GoDaddy.com $1 domain name you like and set it to that WAN address. You now have a free, <10W webserver based on the most cutting edge technology available!
Step 8: Summary
So, I hope this gives you insight as to how web servers work. If you ever thought you couldn't afford one, now you know they can be free, energy efficient, hardly visible or in the way at your house, and fully under your control.
You can apply these techniques to any Linux machine, but some variants refer to "apache2" directories as "httpd". Otherwise, all steps apply in general.
Have fun!
Sean
Our Projects: http://www.raisingawesome.site