Turn Raspberry pi into your own cloud server

VAIBHAV HARIRAMANI
GEEKY BAWA
Published in
13 min readFeb 18, 2021

--

The following tutorial demonstrates how to host a website from the Raspberry Pi 3. The RaspberryPi will serve the default webapp with NGINX, then make it available on the Internet via the network’s local router. It’s not going to scale like the Cloud, but you’ll learn how to turn this little piece of hardware into a real web server.

Nowadays One thing is undeniable that tech monopolies have a lot of power over out privacy and have a lot of influence over the content and services we consume as end users .So if you’re thinking about creating your own cloud server this post is totally 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 AWS(Amazon Web Services) from scratch.

along the way you’ll learn some cool things about

  • linux
  • networking
  • nginx
  • node.js

and 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 am using the RPi3, with 4 Gb of RAM. I recommend purchasing it as a kit with all the necessary accessories.

Setup your hardware and use the NOOBS to install the Raspberry Pi OS flavor of Linux designed specifically for raspberry pi which feels pretty similar to ubuntu. Physical hardware setup is really just a matter of plugging in different size cords to different sized holes you can find tutorial over the internet for that i am not gonna discuss about how to configure raspberry pi with noobs .

we’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.

Comparison

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 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 world is going to try to hack it to pieces in order to save humanity from the f*******.

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 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.

Host a Website

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 using ‘npm install express’ 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 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 node server.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 serious 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 rapberry pi by default so we need to open up the terminal and run

sudo apt update
sudo apt install nginx
sudo /etc/init.d/nginx start

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 /etc/init.d/nginx start

this runs a default web page on the raspberry pi which we can acccess on its internal ip address

Navigate to http://localhost.

You should now see the nginx welcome screen

Get your Internal IP Address

You can also navigate to the site using your internal IP.

hostname -I

# 192.168.0.26

But what we actually want to do here is proxy it to our nodejs application running on local host 5000.
we can modify the default config in /etc/nginx/SITES-AVAILABLE available directory the only modification that we need to make here is to add proxy_pass to localhost 5000 that basicallly tells any traffic going to the root url to use the nodejs app

if you don’t have admin privilege use sudo nano default
save the file then apply the changes by running

sudo /etc/init.d/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 .

Connect to the Internet

An external ip adddress is provided by your internet service provider like comcast verizon or etc. 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 websitebecause once 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 probabbly the best option luckily there is an alternative there are 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

We can view the app using our internal IP on the private network, but that’s not useful for people who want to access the website from the outside world or Internet. For that, we need to expose the app the Raspberry Pi’s external IP address.

Static IP

First, determine your external IP. In all likelihood, your Internet Service Provider (ISP) has given you a dynamic IP, which means it will change on a regular basis. This is not good for websites because it means the DNS will need to be updated whenever the IP changes. What we really need is a static IP — here are a few potential options:

Option 1 Call you ISP and request a static IP address. Some allow static IPs to be purchased online for business uses, but others may not allow it at all for residential locations.

Option 2 Use a service like NoIP. They take run a background program that checks your dynamic IP every 5 minutes, then updates the DNS when it changes.

Option 3 Use Ngrok to forward localhost to a URL, but keep in mind they can only support a small number of simultaneous connections.

For this Post 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 that are all currently closed these doors are called ports and our app is currently being served on port 80.

Port Forwarding

In order to make the website available with the external IP, the router must be configured to allow outside traffic on port 80. The setup is entirely dependent on your router, so find instructions for your exact hardware.

The basic process will have you connect to the router via it’s IP (usually 192.168.0.1), then setup a forwarding rule for port 80.

The port forwarding setup on a Netgear router

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 connects your internal ip address to any external ip address and remember you can get your internal ip address by running hostname -I .

Once you have that done that 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 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

Advanced Tips

This tutorial is just for fun, but if you’re considering this approach in production here are a few additional RPi web hosting tips.

By YouTuber Anunay:

  1. Don’t use No-IP, They are annoying. I recommend getting a cheap domain, add it to cloudflare and using cloudflare-ddns.
  2. Use nginx-proxy-manager, Its way easier to work with when handling lots of projects + It will handle Lets encrypt SSL for you. (Easy cheap HTTPS), get a SSL wildcard for your domain and easily test stuff on subdomains. 3.Use docker + portainer to host your production builds, Only keep port 80 exposed (and forwarded) to web, keep the rest inside docker virtual network.
  3. Setup wireguard VPN on your RPI for remote working. Do not expose port 22 to internet, (you can if you used key based auth, but this is better and gives you access to internal ports easier)
  4. Take regular RPI backups (trust me, those SD card can give up on you very easily). And also before that make sure your ISP isn’t behind a CGNAT or blocks incoming traffic on certain ports (port 80,443,25 are commonly blocked)

Thank You for reading

Please give 👏🏻 Claps if you like the blog.

GEEKY BAWA

just a silly Geek who love to seek out new technologies and experience cool stuff.

Do Checkout My other Blogs

Do Checkout My Youtube channel

If you want to get in touch and by the way, you know a good joke you can connect with me on Twitter or LinkedIn.

Thanks for reading!😄 🙌

Connect with me on Twitter and LinkedIn

Do find time check out my other articles and further readings in the reference section. Kindly remember to follow me so as to get notified of my publications.

Made with ❤️by Vaibhav Hariramani

Don’t forget to tag us

if you find this blog beneficial for you don’t forget to share it with your friends and mention us as well. And Don’t forget to share us on Linkedin, instagram, facebook , twitter, Github

More Resources

To learn more about these Resources you can Refer to some of these articles written by Me:-

Download THE VAIBHAV HARIRAMANI APP

The Vaibhav Hariramani App (Latest Version)

Download THE VAIBHAV HARIRAMANI APP consist of Tutorials,Projects,Blogs and Vlogs of our Site developed Using Android Studio with Web View try installing it in your android device.

Follow me

on Linkedin, instagram, facebook , twitter, Github

Happy coding ❤️ .

--

--

VAIBHAV HARIRAMANI
GEEKY BAWA

Hi there! I am Vaibhav Hariramani a Travel & Tech blogger who love to seek out new technologies and experience cool stuff.