Raspberry Pi versus AWS // How to host your website on the RPi4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this week in tech amazon took the fastest growing social media app of all time and migrated it to its new serverless platform putting it in the highly undesirable position of requiring a reverse cloud migration most companies migrate from on-prem to the cloud but this unfortunate app needs to do the exact opposite because nobody in town wants to touch it whether you agree with the politics of that or not one thing is undeniable tech monopolies have a lot of power over your business so don't walk around here thinking you have free will because you don't i can crush you anytime i want and a lot of influence over the content we consume as end users that being said if you're some kind of weirdo who doesn't trust the cloud this video is for you we'll take a raspberry pi and turn it into a physical server that can host a web application on the internet or in other words you'll build your own mini amazon web services from scratch along the way you'll learn some cool things about linux networking nginx node.js and why if you have a large scale app and get kicked off of aws you're pretty much screwed to follow along the first thing you'll need is a raspberry pi i'm using the model 4 which has a quad core processor and 8 gigabytes of ram and ironically i bought it as a kit from amazon for about 120 bucks it comes with a power supply all the cords you need as well as a memory card with linux pre-installed the linux flavor is designed specifically for raspberry pi it's called noobs and feels pretty similar to ubuntu now i'm not going to go through the physical hardware setup here which is really just a matter of plugging in different size cords to different sized holes i'll be starting from the noob's operating system with the only extra piece of software installed being vs code now our goal here is to create a node.js web application that can serve html from the raspberry pi to the rest of the internet throughout the world but before we do that let's talk about some of the challenges and how it compares to hosting a website on the cloud the first issue is scale we're working with a fixed amount of computing power it's a great value and only a fraction of the price for an equal amount of computing power on aws when averaged out for three years the issue is that our app can only grow so much if we need to grow or scale up we need to buy more raspberry pi's and figure out how to distribute the load across them and we'd likely want to do that in different parts of the world to make our infrastructure closer to the end user and also to provide fault tolerance just in case some kind of natural disaster destroys a raspberry pi like a volcano or peaceful protesters in addition we also need to think about security because we're dealing with a highly controversial app here as soon as we go back online every coder in the world is going to try to hack it to pieces in order to save humanity from the buffalo guy another major drawback of this on-prem approach is that it takes a lot of manual configuration like we need to plug in cables work with power companies to supply electricity internet service providers to provide ip addresses and the list goes on on the other hand with aws we can do everything from a computer screen it's all fully managed and much easier to work with but once you're kicked off you really only have two options build your own data centers or file an anti-trust lawsuit both of which are terrible options but let's go with the first one from here we're going to build a node.js app directly in the raspberry pi it's a classic web page that tells you how many times the page has been viewed node is already installed so we can open up an empty directory then run npm init y to create a new npm project from there we can install express.js to help us build a web app i am creating a javascript file called server.js for our main source code then instead of a database i'm just adding a text file to the project which we can read and write to to get the current count of people who have viewed the web page and the javascript file will import read file sync and write file sync from node.js and then create a new express app the express app will listen on localhost 5000. from there we just need an endpoint to the root url that will update the view count and then send back the raw html to the browser in express we can create an http endpoint by calling app get followed by the url which in this case will just be the root url we can get the current view count by calling read file sync which we'll set to a variable coming from a text file it's a string so we'll parse it into an integer then add one to it then we'll simply write that value back to the same file the final step is to send a response from the endpoint which is just a raw html string but we'll interpolate the view count into it so the user can see that in the ui that's all it takes to build a web app now we can run it by calling nodeserver.js from the command line now at this point you should be able to open localhost 5000 on the raspberry pi browser and see your web app displayed there at this point we have a working web app but we're building a sirius app here and that means we're going to deploy it with nginx once we add multiple raspberry pi's to our data center we'll be able to use nginx to balance the load across them nginx is not installed on raspberry pi by default so we need to open up the terminal and first run sudo apt update to get the latest information from the package manager then we can run sudo apt install nginx this adds nginx to the etc directory which we can run with sudo followed by the start command this runs a default web page on the raspberry pi which we can access on its internal ip address you can find the ip address by running hostname flag capital i then paste that value into the browser it should say welcome to nginx but what we actually want to do here is proxy it to our nodejs application running on localhost 5000. we can modify the default config in the etc nginx sites available directory i've removed all the comments leaving only the default config the only modification that we need to make here is to add proxy pass to localhost 5000 and that basically tells any traffic going to the root url to use the nodejs app save the file then apply the changes by running sudo nginx restart now that we have our app running the way we want it on the raspberry pi we need to make it available to the internet the internal ip address can be used on your own local network but in order to make the website available to the outside world on the internet we need an external ip address an external ip address is provided by your internet service provider like comcast verizon at t or whatever for most people they provide a dynamic ip address that can change on a regular basis but it's not a great thing if you're trying to host a website because once the ip changes then that means your website is no longer available on the internet that means if we want to serve our web app from a raspberry pi we have two options we could call up the isp and ask them for a static ip address these guys don't exactly have a great reputation for customer service but getting a static ip from the provider is probably the best option luckily there is an alternative there are a few services out there that can monitor your dynamic ip and update it on the dns for a domain when it changes in other words it makes your dynamic ip behave like a static ip i'm going to be using noip.com because it has a free tier and it worked for me just fine when you sign up you pick a free domain name and it will automatically detect your external ip as the a record in the dns but in order for it to detect changes on the raspberry pi we need to install some software there basically you just download a binary execute it and then it will have you log in with your username and password to link the account to the raspberry pi once that's done we effectively have a static ip but there's still one more step left to get this thing working if we try to navigate to the external ip at this point we get a blank screen by default your router will not allow outside traffic to get into your network it's like a giant fence with a bunch of doors in it that are all currently closed these doors are called ports and our app is currently being served on port 80. that means we need to open up port 80 on our router to external traffic through a process called port forwarding the exact steps will depend entirely on the router you use but there is a website called portforward.com that has a breakdown for basically every router out there once you connect to the router you should be able to find port forwarding under the advanced options what you'll do is add a new forwarding rule with starting and ending ports for 80 that connect your internal ip address to any external ip address and remember you can get your internal ip address by running hostname flag capital i from the command line once you have that done you should now be able to navigate to your external ip address and the result should be the node.js app that we built earlier congratulations you just built your own amazon web services from scratch but they may take our lives but they'll never take our freedom [Music] well maybe not exactly but you did host your own website from a raspberry pi that's not entirely dependent on a big cloud monopoly it's not going to scale very well yet but at least it's a start i'm going to go ahead and wrap things up there let me know what you think in the comments thanks for watching and i will see you in the next one
Info
Channel: Fireship
Views: 546,982
Rating: undefined out of 5
Keywords: webdev, app development, lesson, tutorial, aws, the cloud, raspberry pi, rpi4, cloud, web hosting, nodejs, js, nginx
Id: QdHvS0D1zAI
Channel Id: undefined
Length: 8min 39sec (519 seconds)
Published: Thu Jan 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.