Home > english, Programming, Technologie > Use your Raspberry Pi as your local NodeJS Webserver

Use your Raspberry Pi as your local NodeJS Webserver

webtool_banner

*** An updated version of the article is available here ***

A couple of months ago, I tinkered around with NodeJS on my local machine. After a while I got kinda addicted to the NodeJS Plattform for webservice (SOA) applications as well as Desktop Apps (Electron/NodeWebkit). Hence more complicated NodeJS experiments will start, I decided to set-up my own webserver environment for my (local) network with a Raspberry Pi 2 B. My tools of choice are:

  • Raspian Jessie Lite from 2016-05-27
  • GIT v2.1.4: Version control for source code
  • NodeJS v6.2.0: for REST APIs and Websocket applications
  • Nginx Webserver: Deliver static files, Reverse Proxy for NodeJS, and Load Balancing
  • MySQL Server and Client: for Relational Databases
  • CouchDB v1.6.1: as my NoSQL Database with strong JavaScript tools
  • PM2: NodeJS Process Manager for starting and stoping NodeJS Apps
  • Nodeadmin: Admin Tool for MySQL (like PHPmyAdmin)

Let's get started:

 

Download Raspian Jessie Lite from the foundation website and write the image on your class 10 SD Card. For more information please read this tutorial (in German).

 

Configure your Raspian Jessie Lite

 

After the booting or Raspberry PI (rPI), please login with the standard user 'pi' and password 'raspberry'. If you successfully logged in, please run:

Now please configure your Raspberry based on your location and wishes (e.g. edit your timezone, region, keyboard settings, extend the memory space of your SD Card, make the GPU RAM free, if needed overclock carefully your rPI, change the Hostname and Password if needed). I would recommend to connect your rPI directly with your Wifi Router via classic LAN. It is mostly very stable and fast. But if you don't care what I think, please configure your wireless connection (wifi). A tutorial is available here.

After all of this initial configuration please update the already provided library via those shell commands:

By the way, for future times here is a nice list of very useful rPI commands.

 

Adding Version Control

 

In my early developer days CVS became the dead version control system and SVN was the hot shit. Time is changing and nowadays most people are using Git / Github / Bitbucket for their code version control instead of SVN. For making our workflow easier we install the Git Client.

 

Installing NodeJS and PM2

 

My main purpose of the testserver is to tinker around with NodeJS on a reliable backend level. In future I might want to play around with some Internet-of-Things stuff (e.g. connecting plants to the internet), too. NodeJS and Raspberry Pi is a killer prototyping tool for that. Installing NodeJS on a Raspberry Pi 2 is very obvious, because of its base ARM v7. The Raspberry Pi 3 seems to run a ARM v8 architecture, but that is not really true. The Raspian Jessie Lite still runs as a 32bit OS. As far as I understood that is the reason, why we still have to use the NodeJS ARM v7 version for the Raspberry Pi 3, too. However that makes the shell commands for this tutorial a little bit easier (the shell code is based on this thread).

NodeJS is executed as a single-thread. In that context it is not so simple to run several NodeJS apps simultaneously. This problem is solved by PM2, the NodeJS Process Management tool, which is our great companion. PM2 is the successor of the older process management tool forever. PM2 holds a nice interface and I like the console output for registered NodeJS apps much more than other tools. Therefore here we go:

 

Installing the MySQL Database

 

Since the MEAN Stack as a Fullstack tool got very popular, the emerging NoSQL Databases started competing hard against the widely used Relational Databases like MySQL. In my opinion it depends on your data and application which database approach is the appropriate one for you. In my past developer times MySQL was very often a great help and its need is still valid for me. Therefore In my opinion MySQL is vital tool for my local testserver environment.

After we have entered a password for our 'root' user, and we have hopefully installed MySQL successful, the next little companion starts to arrive. In my early PHP coding days I really like phpMyAdmin for editing, deleting and query datasets in MySQL. Luckily the NodeJS community implemented a similar tool for us. Our new buddy is called Nodeadmin. Please navigate to your folder, where you want to place your NodeJS apps. In my case it is the home user folder '/home/pi/', which feels not to be the smartest idea (I am still in the process to figure out where to place my nodejs project files, recommendations are very welcome!).

Unfortunately, nodeadmin does not start directly from the beginning. We have to create a NodeJS app by ourselves. It sounds worse than it is. If have typed in all the shell commands included the nano editor call, then we write our first NodeJS code.

Please safe the code with the key combination "CTRL+X" and "Enter". Now we can test our first NodeJS Server App on our Server. Let's start the app with our companion PM2:

The console feedback should show you a running Nodeadmin server. You can also test it with your browser. Type the IP Adress and the Portnumber like this 'http://<your-rpi-ip-number>:3333/nodeadmin/'. In my case the website took some time until it should be the logIn screen. Maybe you will have the same problem, be please a little bit patient during the first start.

Nodeadmin Log In with my local IP Number and Port as an example

Nodeadmin LogIn screen with my local address

 

It is nice to have a MySQL Admin Interface (LogIn is the mySQL root user), but at the moment we have to start Nodeadmin everytime after the boot procedure manually. In my opinion that sucks. Let's add Nodeadmin to init.d start procedure:

Unfortunately, the simple startup creation procedure by PM2 does not work on my Debian Jessie Lite. Therefore, we have to write our own start script. Luckily there was a helpful tutorial available (sorry it is in German).

Again safe the file with "CRTL+X" and "Enter". Now we have to make it executable in our Shell.

Now the Raspberry Pi should start with Nodeadmin from the beginning. You can test it through a simple browser call from before 'http://<your-rpi-ip-number>:3333/nodeadmin/'.

However, now you should be able to work with MySQL. During my research I figured out that MySQL is also an old horse. MariaDB seems to be the new successor. Unfortunately the installation for MariaDB and usable Admin Interface is a little bit more complicated in connection with NodeJS. Maybe at sometime I will find an appropriate solution for that circumstance.

 

Installing our NoSQL CouchDB

 

After we have installed the relational database for very structured data, we want to install a NoSQL database with MapReduce functionality applied to less none structured/predefined datasets. Regarding the popularity of the MEAN stack I am sure you expect an installation of MongoDB. Unfortunately, the current MongoDB Version is kinda nasty to install on a Raspberry Pi. Furthermore, CouchDB holds some nice features compared to MongoDB, which makes it more attractive for me. Those features are:

  • CouchDB works very well with JavaScript (e.g. the JS Query Interface in Futon) and NodeJS (NPM works with CouchDB)
  • It can work very easily in clusters and can be connected with other databases and servers/computer
  • CouchDB provides an out-of-the-box Sync solution between different databases in combination with Version Control of data sets.
  • In Offline First App (or using the Google terminology Progressive Web Apps) can make a great use of CouchDB. Please check the great work of PouchDB (Clientside Database) and the Hoodie project.

Those four points summarize my motivation for diving into the CouchDB cosmos very well. Even the installation setup for the new version is more complicated. For the current version 1.6.1 (with an improved authentication user system), we have to compile it on our Raspberry Pi. The both CouchDB tutorials by Volker Koster were a great help for me. And here we go again back to the Shell:

Yeah, now we should have finished the installation of CouchDB on our machine and have even compiled the first software on our fresh rPI! Please check with your browser if Futon - the CouchDB Admin Panel - is running correctly.

CouchDB Futon Start Screen for version 1.6.1

CouchDB Futon Start Screen for version 1.6.1

 

If you want to be 100% sure that your installation was correct, please click the Button at right Panel "Diagnostic > Verify Installation" under the documentation link.

 

Installing Nginx Webserver

 

After the installation of NodeJS, MySQL, and CouchDB it seems all vital tools are available for starting tinkering around. In my opinion that is not true. Therefore, I decided to install an additional webserver, which runs beside our NodeJS apps. The main reasons of running a webserver beside NodeJS are:

  • Deliver static files (html, css, images, and frontend js) is more efficient through NGINX than via NodeJS
  • NGINX can act as a Proxy and Caching Server for NodeJS webservice calls (e.g. Reverse Proxy)
  • Better Security management (secure your HTTP connection and e.g. CVE-2013-4450/Denial of Service attacks)
  • Working with new generation protocol HTTP/2 (e.g. bundling HTTP request and Server Push)
  • Support of Websockets (e.g. for  WebRTC or other Websockets apps like crossbar.io)

The last to points of my arguments are only valid for Nginx bigger than version 1.9.5. Unfortunately, if you install Nginx through the usual 'apt-get' command, you do not get a higher version than 1.6.1. Even if you try to install the stretch repository will fail. This circumstance forces us to compile again our new tool (based on this tutorial). Back to the shell:

After the compilation and the configuration part of Nginx, we have to edit the common Nginx setting file in the nano Texteditor. Please change the content of the file to:

Then save it with "CTRL + X" and "Enter". Afterwards, we have to activate the webserver settings through a restart. That can be done very easily through the shell/console:

Now you can visit the starter content of our Nginx Webserver (which listens on the standard port 80 for HTTP). Just type in your IP number like that: http://<your-ip-number>

 

Nginx version 1.10.1 Screenshot running on Raspberry PI

Nginx version 1.10.1 running on Raspberry PI

 

Now Nginx should run properly and you can modify the settings to a Reverse Proxy and/or Load Balancer for your NodeJS apps. If you want to add your frontend webcode and HTML files, please use the folder. It is kinda your web root folder.

/var/www/

 

 

Summary

 

After all this installation effort, we have finally a running webserver toolbox for our local network. But please consider: don't connect this server directly to the internet (via Port Forwarding) . Someone can hack your server and can get access to your complete Wifi traffic. If you really want to make your webserver accessible to your friends/other people via Internet, please follow some additional settings and configurations. Here are some helpful tutorials for preparing your server for the public. (For German people check here)

At the end of all action, I asked myself two questions (maybe later more):

  • if it is really useful to make several user profiles for each software (see example nginx and CouchDB user). If yes, then I have to create an additional user for NodeJS and MySQL and maybe put all those user profiles in a "webserver" user group. Does this make sense from a Server Admin / DevOps perspective?
  • The other question belongs to the folder structure of your system. At the moment you can put your NodeJS apps to the standard Pi user home folder. Does it make more sense to put NodeJS apps to a new shared folder like "/var/node-apps/"? How and in which folder does Webserver Admins install their software?

Regarding backend and webserver technology, I am pretty much a beginner. Some feedback from more experienced reader would be very nice. Thx in advance!

 

 

  1. admin
    June 12th, 2016 at 18:59 | #1

    If you were looking for some NodeJS Tooling, here is a nice list with description
    https://dzone.com/articles/10-areas-where-tooling-makes-nodejs-developers-mor

    Furthermore, the service and Chrome App Postman looks interesting for testing your REST APIs
    http://www.getpostman.com/
    https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop

  2. September 26th, 2016 at 15:58 | #2

    A nice article on HTTP2 and NodeJS with Express

    Optimize Your App with HTTP/2 Server Push Using Node and Express

  3. admin
    October 17th, 2016 at 19:46 | #3

    such a great starter book for Raspberry Pi and Linux beginners:

    ESSENTIALS CONQUER THE COMMAND LINE – Raspberry Pi a TERMINAL GUIDE written by Richard Smedley

  4. October 27th, 2016 at 20:08 | #4

    If someone is interested on running MongoDB on a Raspberry Pi 3B, then check this tutorial

    http://yannickloriot.com/2016/04/install-mongodb-and-node-js-on-a-raspberry-pi/

  5. October 29th, 2016 at 15:31 | #5

    If you want to run several NodeJS & NPM versions, then you can use the Node Version Manager (NVM) tool. The installation is described as Option 1 Install Node.js with Node Version Manager in this tutorial. The shell commands are the same for Raspberry Pi

    http://www.hostingadvice.com/how-to/install-nodejs-ubuntu-14-04/#node-version-manager

  6. October 30th, 2016 at 11:50 | #6

    If we are talking about the hype of server clusters, microservices and docker then these two links will make you happy:

    HybriodOS – an Operating System for your RaspberryPi and your Docker Apps/Images
    http://blog.hypriot.com/
    http://blog.hypriot.com/post/high-availability-with-docker/

    Hardware for your Raspberrry Pi Server Cluster
    https://www.picocluster.com/collections/starter-picocluster-kits/products/pico-5-raspberry-pi-starter-kit?variant=29344698892

    And some resources from the Raspberry Pi foundation:
    https://www.raspberrypi.org/blog/docker-comes-to-raspberry-pi/
    http://blog.alexellis.io/hands-on-docker-raspberrypi/
    http://blog.alexellis.io/getting-started-with-docker-on-raspberry-pi/
    http://blog.alexellis.io/5-things-docker-rpi/

    If you playing around with the Raspberry Pi at a Hackathon, and you wanna make your app accessible from outside without changing the network configuration, then this tool might help you:
    ngrok – Secure tunnels to localhost

    Another great article about Reverse Proxy your NGINX server
    http://blog.alexellis.io/save-and-boost-with-nginx/
    https://www.nginx.com/resources/admin-guide/reverse-proxy/
    https://www.nginx.com/resources/admin-guide/nginx-ssl-termination/

  7. Andrew
    November 5th, 2016 at 07:06 | #7

    I’m not strong on Linux, but after getting this error during the compile:

    sed -e "s|%%PREFIX%%|/etc/nginx|" \
    -e "s|%%PID_PATH%%|/var/run/nginx.pid|" \
    -e "s|%%CONF_PATH%%|/etc/nginx/nginx.conf|" \
    -e "s|%%ERROR_LOG_PATH%%|/var/log/nginx/error.log|" \
    objs/nginx.8
    make[1]: Leaving directory '/home/pi/nginx-1.10.1'
    make -f objs/Makefile install
    make[1]: Entering directory '/home/pi/nginx-1.10.1'
    test -d '/etc/nginx' || mkdir -p '/etc/nginx'
    mkdir: cannot create directory â/etc/nginxâ: Permission denied
    objs/Makefile:1190: recipe for target 'install' failed
    make[1]: *** [install] Error 1
    make[1]: Leaving directory '/home/pi/nginx-1.10.1'
    Makefile:11: recipe for target 'install' failed
    make: *** [install] Error 2
    pi@rpi3:~/nginx-1.10.1 $

    I changed the line after #compile nginx:
    sudo make && make install;

    to
    make && sudo make install;

    This seems to be the correct syntax and the compile completed.

  8. Andrew
    November 5th, 2016 at 07:16 | #8

    Then the following command should be:

    #test run the nginx Server
    /usr/sbin/nginx/nginx -v
    #not /usr/sbin/nginx -v

    Something else is not right for me, as I couldn’t get nginx to start. I just thought I’d leave these comments in case they helped someone else.

  9. admin
    November 5th, 2016 at 14:06 | #9

    Hey Andrew,

    thank you very much for your feedback. The next time I will set up a new Raspberry Pi 3 at home. Maybe I will run in the same issue… thanks for your warning and providing your solution.

    All the best,
    Flo

  10. November 25th, 2016 at 23:16 | #11

    The go based tool acmetools makes the generation process for SSL certificates (e.g. with let’s encrypt) much easier.

    Raspberry Pi relevant links to this topic
    https://blog.meinside.pe.kr/Lets-Encrypt-with-Go-ACME-Client-Utilities/
    https://github.com/hlandau/acme/issues/141
    https://github.com/hlandau/acme/releases

  11. December 4th, 2016 at 21:52 | #12

    Hey, I think you could make this guide much more concise by using Docker containers, it’s seriously easy to run Node.js + NGinx + MySQL etc with very little configuration. I appreciate you’ve spent a lot of time getting the detail right, but there are easier ways. My blog has been quoted in the comments already.. I’m happy to talk to you through it.

    I guess your title here is “NodeJS” but then there’s a good half dozen extra apps you are installing / setting up. A quick way to install Node is with tar and -C so:

    curl -sSLO \
    https://nodejs.org/dist/v4.4.0/node-v4.4.0-linux-armv6l.tar.gz && \
    tar -xf node-*.tar.gz -C /usr/local \
    --strip-components=1

    https://github.com/alexellis/docker-arm/blob/master/images/armhf/nodejs/Dockerfile

  12. December 4th, 2016 at 22:43 | #13

    Hi @Alex Ellis

    thank you very much for your comment! I agree with you. Working with Docker and NodeJS on the Raspberry Pi looks pretty straightforward. At the time of writing this article, I just was not confident enough using Docker. For few weeks, I already asked myself a few times, if the Docker approach eats more resources than the approach without Docker? Do you have any experiences with it? Running into RAM issues or something similar?

  13. admin
    January 25th, 2017 at 15:52 | #14

    CouchDB 2.0 got some really significant features regarding cluster infrastructures, sync and offline capabilities. It seems more powerful and great than ever. Luckily, Andy Felong wrote a turorial how to install CouchDB 2.0 on the Raspberry Pi 3.

    CouchDB 2.0 on Raspberry Pi by Andy Felong

  14. February 8th, 2017 at 18:38 | #15

    Great introduction videos of Couch DB with NodeJS
    https://www.youtube.com/watch?v=R6LUMXrAoCE

    Some Basic CouchDB stuff
    https://www.youtube.com/watch?v=nlqv9Np3iAU

  1. No trackbacks yet.