During the last summer, I have tinkered around to build my own LocalDev Server with a Raspberry Pi. Since then some new software version emerged. For this reason, here is an updated version of my LocalDev Server config and installation scripts. A short overview, which software we will install during this article:
- Couch DB 2.0 with the new Admin Interface Fauxton
- NodeVersion Manager: Running various set of nodeJS runtimes
- Node Process Manager pm2 for running several node apps in parallel
- Nginx Webserver for delivering static files
I will start again with a clean Raspian Jessie Lite installation. Set up the standard configuration with the shell command:
1 |
sudo raspi-config |
Then configure your time zone, Keyboard, expand your File System, enable SSH, change the hostname to ‘localdev’, and assign some memory split from the graphic chip to the whole system. If you don’t know what I am talking about, please check this documentation.
After rebooting the system, you start updating all libraries and software tools:
1 2 3 4 |
# update system and be sure that we get everything sudo apt-get update sudo apt-get dist-upgrade sudo apt-get upgrade |
Then we install git on our machine. That enables us to deploy directly from our source code repositories.
1 2 |
# install a code version control system sudo apt-get install git |
Now we are ready to start the real interesting stuff.
Installing NodeJS and NPM
If stumbled on a interesting article how to run different versions of node and npm on an Ubuntu system. The maintaining effort can be reduced by tools like the Node Version Manager (NVM). Luckily, it is pretty straightforward to install on a Raspberry Pi. Let’s start:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# ># Install dependencies sudo apt-get install build-essential checkinstall sudo apt-get install libssl-dev # Download and run the installation script, please check for the current version curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash #test installation of nvm command -v nvm #install nodejs current node runtimes. Depends on your wishes nvm install 6.9.5 nvm install 7.5.0 # check your nodejs installed runtimes nvm ls # choose your preferred nodejs runtime and assign it to your default nvm use 7.5.0 nvm alias default node #test your running node and npm. The output should be the same as your chosen runtime node -v npm -v |
I highly recommend installing an additional process manager for NodeJS. It enables to run several node apps simultaneously. In my case it is pm2.
1 2 3 4 5 |
# optional install a node process manager npm install -g pm2 #restart your system sudo reboot |
Next step is running a persistent data storage. In my case it is the NoSQL database Couch DB Version 2.0.
Installing Couch DB 2.0 on Raspberry Pi
The new version of Couch DB is able to run in a cluster mode. That means you can connect several Raspberry PIs to one big Couch DB Server. However, I think that is for the most cases a little bit too much. The new admin interface Fauxton is really cool. It makes everything much more intuitive. I think Couch DB makes it very easy to support distributed software in an offline and online mode. Furthermore it allows a stable (redundant) backup procedure through its replications feature. Most of this installation script is based on the tutorial by Andy Felong and his readers comments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Install dependencies cd wget http://packages.erlang-solutions.com/debian/erlang_solutions.asc sudo apt-key add erlang_solutions.asc sudo apt-get update sudo apt-get install -y erlang-nox erlang-dev erlang-reltool sudo apt-get install -y build-essential sudo apt-get install -y libmozjs185-1.0 libmozjs185-dev sudo apt-get install -y libcurl4-openssl-dev libicu-dev #Setup the couchdb user and create a home folder for the 'couchdb' user sudo useradd -d /home/couchdb couchdb sudo mkdir /home/couchdb sudu chown couchdb:couchdb /home/couchdb |
Now you have to download the source code from the CouchDB homepage (or directly from the mirror page). In my case it was like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# download source code wget http://mirror.symnds.com/software/Apache/couchdb/source/2.0.0/apache-couchdb-2.0.0.tar.gz tar zxvf apache-couchdb-2.0.0.tar.gz cd apache-couchdb-2.0.0/ #start compiling ./configure make release # changing the owner and location of couch db running system! # Here you can adapt the everything on your personal needs cd ./rel/couchdb pwd /home/pi/apache-couchdb-2.0.0/rel/couchdb sudo cp -Rp * /home/couchdb sudo chown -R couchdb:couchdb /home/couchdb #configure the admin UI System cd /home/couchdb/etc # open the config file with an editor like 'nano' or 'vi' sudo nano local.ini |
Edit the chttp section and change this line to enable remote access from your local IP address.
1 2 3 4 |
from #bind_address = 127.0.0.1 to bind_address = 0.0.0.0 |
Save and Exit your editor. Then we start the first time our CouchDB 2.0 server. Back to the terminal:
1 2 3 |
# run couch DB 2.0 the first time sudo -i -u couchdb /home/couchdb/bin/couchdb # Don't be scared it throws errors all the time, but it will run smoothly |
Now, you must switch to your browser. We call the Fauxton Admin Interface via that URL:
http://yourIP:5984/_utils/
You should see the classic Fauxton Interface as described by Andy Felong. If you can see it, then please click the green button ‘Verify‘ > ‘Verify Installation‘. Please have a look on the screenshots by Andy Felong.
For some administration eye candy stuff, we will create a couchdb systemd service for our Raspberry Pi system. That services starts the CouchDB server after every reboot. Back to the terminal:
1 2 |
# Optional make a start/boot routine for your system sudo nano /etc/systemd/system/couchdb.service |
Add this service code to our file
1 2 3 4 5 6 7 8 9 10 11 12 |
[Unit] Description=Couchdb service After=network.target [Service] Type=simple User=root ExecStart=/home/couchdb/bin/couchdb -o /dev/stdout -e /dev/stderr Restart=always [Install] WantedBy=multi-user.target |
Save and Exit file. After the creation of the file, we have to enable it. That works with these commands:
1 2 3 4 5 6 7 |
#Register your systemd service systemctl daemon-reload systemctl start couchdb.service systemctl enable couchdb.service #reboot sudo reboot |
After the restart, please check if the Couch DB 2.0 service was executed properly. If everything runs smoothly, please go ahead with the last step of this article or check these nice CouchDB tutorials (Part 2).
Installing your nginx webserver
The Raspberry Pi is pretty limited in hardware resources. Luckily, nginx is more lightweight than the famous Apache Webserver and allows a wide range of configurations. It works pretty well together with NodeJS and Websockets. That is the reason why I have chosen it instead another great lightweight webserver tool lighttpd. Unfortunately, the ‘apt-get install nginx’ command only delivers the version 1.6 instead the current version 1.10. Therefore, I recommend to compiling it. Ok enough, chitchat… Let’s start:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# Install dependencies sudo apt-get install libpcre3 libpcre3-dev sudo apt-get update sudo apt-get install libssl-dev # go back to your home folder and download the software cd ~; # Please consider: the Download link and tar command depend on the current version wget http://nginx.org/download/nginx-1.10.3.tar.gz tar -xvf nginx-1.10.*.tar.gz cd nginx* #configure the compilation # Please adapt it on your needs. If you want, please add the modules for php5 and so on ./configure --user=nginx --group=nginx --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-file-aio --with-http_realip_module --without-http_scgi_module --without-http_uwsgi_module --without-http_fastcgi_module #compile nginx make && sudo make install #test run the nginx Server. The output should be version 1.10.3 /usr/sbin/nginx -v |
Again some eye candy stuff for administrators. We will create an extra user for nginx and a new service. After our configuration, the nginx webserver will start after every reboot (similar to the previous Couch DB service).
1 2 3 4 5 6 |
# config nginx for user management and booting process of the rPI # create user for nginx server sudo useradd -r nginx; # Open the service file with an editor like nano or vi sudo nano /etc/systemd/system/nginx.service |
Please, type in the source code from underneath. The code of this service is based on the nginx documentation and this tutorial by luxagraf.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# Stop dance for nginx # ======================= # # ExecStop sends SIGSTOP (graceful stop) to the nginx process. # If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control # and sends SIGTERM (fast shutdown) to the main process. # After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends # SIGKILL to all the remaining processes in the process group (KillMode=mixed). # # nginx signals reference doc: # http://nginx.org/en/docs/control.html # [Unit] Description=A high performance web server and a reverse proxy server After=network.target [Service] Type=forking PIDFile=/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;' ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;' ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid TimeoutStopSec=5 KillMode=mixed [Install] WantedBy=multi-user.target |
Save and Exit your editor. The next step is to register and enable the service file. Back to the terminal:
1 2 3 4 5 6 7 |
#Register Service sudo systemctl enable nginx.service sudo systemctl start nginx.service sudo systemctl status nginx.service #Optional for fast restart command via 'xrestart' alias xrestart="sudo systemctl restart nginx.service" |
Now we have to configure our nginx sever a little bit. We link nginx with our www folder, which serves our static files.
1 2 3 4 5 6 7 8 9 10 11 |
#Configure nginx source folder cd /home sudo mkdir nginx cd nginx sudo mkdir www #change user rights of the folder sudo chmod -R 775 /home/nginx/www/ sudo chgrp -R nginx /home/nginx/www/ # open the configuration file with your editor sudo nano /etc/nginx/nginx.conf; |
Again edit and adapt the configuration file:
1 2 3 4 5 6 |
#Change content to location / { root /home/nginx/www/; ## update the server root folder index index.html index.htm; } |
Save and exit the file. Now we have to create a placeholder index.html file.
1 2 3 4 5 6 7 8 9 10 11 |
#Configure nginx source folder cd /home sudo mkdir nginx cd nginx sudo mkdir www #change user rights of the folder sudo chmod -R 775 /home/nginx/www/ sudo chgrp -R nginx /home/nginx/www/ # open the configuration file with your editor sudo nano /etc/nginx/nginx.conf; |
Again edit and adapt the configuration file:
1 2 |
## Placeholder index.html sudo nano /home/nginx/www/index.html |
Add the source code to the file. I know it is not valid html, but for testing if the server works, it is enough.
1 |
<h1>Hello World!</h1> |
Save and Exit the editor. Now restart the server:
1 2 3 4 5 6 7 8 |
#Resart your nginx Webserver xrestart ## Clean the PI folder rm -rf apache-couchdb-2.0.0 rm -rf apache-couchdb-2.0.0.tar.gz rm -rf nginx-1.10.3 rm -rf nginx-1.10.3.tar.gz |
Now switch back your browser and navigate to the URL http://your-ip-address:80/. Then you should see the ‘Hello World!’ headline.
Summary
We have installed a few cool tools for tinkering around with NodeJS and CouchDB. Even more, this environment is useful for your first steps with Express, Hoodie, PouchDB, NodeJS in combination with IoT, MQTT, and other cool stuff.
Please consider one very importan thing!!! Don’t connect this server to the internet. It is not a safe production environment. It is only recommend to use it within your local/private network. How to run your Raspberry Pi for public access is beyond that article. Please check this article by Pestmeester.
What will be next?! I plan to provide an iso image file of this tutorial. So that everyone can start as fast as possible with this setup. Afterwards I would like to add some more tools. In that context it could be MariaDB, Docker (even it might too much for a Raspberry Pi environment), Node-RED, Johnny-Five, etc.
*** Update: An Image file for this tutorial can be downloaded here ***
25 Comments
From here on you can also start to prototype your own server farms, like these two projects:
https://cloudant.com/blog/building-a-cloudant-cluster-of-raspberry-pis/
http://www.datastax.com/dev/blog/32-node-raspberry-pi-cassandra-cluster
https://www.raspberrypi.org/blog/serving-raspberry-pi-3-launch-raspberry-pi-3/
If I run the following command:
http://yourIP:5984/_utils/
and replace yourIP with the local address of my Raspberry, i get the following error from my Webbrowser:
Diese Website ist nicht erreichbar
192.168.x.x hat die Verbindung abgelehnt.
ERR_CONNECTION_REFUSED
Hi Karl,
it sounds that there is no connection available. Can you ping your Raspberry Pi?
on Ubuntu/Linux
PING yourRaspberryIPaddress
For Windows
ping -t yourRaspberryIPaddress
There must be a connection, because i was logged in over Putty…
If you can connect to the Raspberry Pi via Putty, then Couch DB might be inactive. For starting CouchDB. you should log in via Putty to your Raspberry Pi and execute the command
sudo -i -u couchdb /home/couchdb/bin/couchdb
Hopefully that helps.. If not, please check if you have also have changed the chttp – bind_address parameter to 0.0.0.0 as described above.
Running MongoDB got also pretty straightforward with Jessie. Here is a nice tutorial about it:
http://yannickloriot.com/2016/04/install-mongodb-and-node-js-on-a-raspberry-pi/
For the case that the usual Raspberry PI OS Jessie doesnot fit your needs, these two links provice a really nice resource and overview of available OS for the Pi with pros and cons.
https://raspberrypi.org/magpi-issues/MagPi56.pdf at page 60-69
http://raspberrypi-source.blogspot.de/2017/03/operating-systems-for-raspberry-pi.html
Deployements with Docker within localDev Environments
https://www.codementor.io/jquacinella/docker-and-docker-compose-for-local-development-and-small-deployments-ph4p434gb
https://increment.com/development/an-introduction-to-local-development-with-containers/
Great tutorial about systemd with nodejs by nodesource
https://nodesource.com/blog/running-your-node-js-app-with-systemd-part-1/
https://nodesource.com/blog/running-your-node-js-app-with-systemd-part-2/
Another great resource of coding and guide for bash (terminal of love)
https://github.com/Idnan/bash-guide
This article about reproducible development environment with Docker to manage a number of Node.js microservices is pretty interesting
http://mherman.org/blog/2017/05/11/developing-microservices-node-react-docker/#.WXn3xFFpzmH
It is no secret that I like CouchDB. Here is a nice resource as CouchDB Cookbook
https://supermarket.chef.io/cookbooks/couchdb/
Some nice security basics and principles for your raspberry pi server
https://mattwilcox.net/web-development/setting-up-a-secure-home-web-server-with-raspberry-pi
https://opensource.com/article/17/3/iot-security-raspberry-pi
Another nice starter tutorial for CouchDB 2.0
https://blog.couchdb.org/2017/10/10/get-up-to-speed-on-couchdb-2-0-in-45-minutes/
An interesting paid CouchDB course
https://www.udemy.com/understanding-couchdb/
Starter Tutorial for PouchDB
https://techshard.com/2017/09/03/beginners-guide-to-pouchdb/
Some further resources about CouchDB Version 2.0
https://blog.couchdb.org/2016/08/23/check-out-the-road-to-couchdb-2-0-series/
Konfigurieren weiterer Parameter des Raspberry Pis als Server
https://blog.doenselmann.com/eigenen-domainnamen-fuer-dyndns-verwenden/
https://blog.doenselmann.com/ssh-schluessel-zur-authentifizierung/
https://blog.doenselmann.com/firewall-fuer-raspberry-pi-und-banana-pi/
https://blog.doenselmann.com/nginx-und-lets-encrypt-auf-raspberry-pi/
Build a Serverless or other kind of Serverfarm with RaspberryPi
https://blog.alexellis.io/your-serverless-raspberry-pi-cluster/
https://www.hanselman.com/blog/HowToBuildAKubernetesClusterWithARMRaspberryPiThenRunNETCoreOnOpenFaas.aspx
I tried also an alternative configuration for nginx
Additional libraries are needed:
GD lib on Raspberry Pi via this article
sudo apt-get install libgd2-noxpm-dev
GeoIP 1.4.5 via this article
wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP-1.4.5.tar.gz
tar -xvf GeoIP-1.4.5.tar.gz
./configure
make
make check
make install
And the nginx config command:
./configure --user=nginx --group=nginx --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gunzip_module --with-http_secure_link_module --with-http_auth_request_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-file-aio --with-http_realip_module --with-stream --with-stream_ssl_module --with-debug --with-http_flv_module --with-http_mp4_module --with-http_dav_module --with-http_sub_module --with-http_geoip_module --with-http_addition_module --with-http_v2_module --with-http_image_filter_module --with-http_degradation_module --without-http_scgi_module --without-http_uwsgi_module
Another addition to the nginx configuration could be “rtmp” module and ffmpeg:
Setting up HLS live streaming server using NGINX
https://docs.peer5.com/guides/setting-up-hls-live-streaming-server-using-nginx/
I am pretty curious how this performs on a Raspberry Pi, because this device is not very famous for its network/streaming/data processing power
Some nice guideline how to deal with NodeJS / NPM global and local installations
https://codeburst.io/maybe-dont-globally-install-that-node-js-package-f1ea20f94a00
Great tutorial how to set up your Raspberry Pi in a Lite way (with NodeJS and Samba File Server for Windows User)
http://thisdavej.com/create-a-lightweight-raspberry-pi-system-with-raspbian-lite/
Really nice helper tool for Admin and Relpicate CouchDB Instances
https://medium.com/ibm-watson-data-lab/cloudant-and-couchdb-replication-with-couchreplicate-79ea6e898e6e
Nginx and HTTP2 Push feature is coming in 1.13.9…
http://mailman.nginx.org/pipermail/nginx-announce/2018/000207.html
Nginx and HTTP2 Push feature is coming in 1.13.9 and explained here
https://www.nginx.com/blog/nginx-1-13-9-http2-server-push/
Some further interesting Nginx Webcast
NGINX: Access Management and Security Controls – EMEA
https://www.nginx.com/resources/webinars/nginx-access-management-security-controls-emea/
MRA AMA Part 8: Secure Inter-Service Communication
https://www.nginx.com/watch-on-demand/?id=microservices-secure-inter-service-communication
HTTP2 Push feature with Nginx explained here
https://www.nginx.com/blog/nginx-1-13-9-http2-server-push/
Some further interesting Nginx Webcast
NGINX: Access Management and Security Controls – EMEA
https://www.nginx.com/resources/webinars/nginx-access-management-security-controls-emea/
MRA AMA Part 8: Secure Inter-Service Communication
https://www.nginx.com/watch-on-demand/?id=microservices-secure-inter-service-communication
Some further nice advices for managing your config files and your app states across different devices
Config with NodeJS
https://medium.com/ibm-watson-data-lab/environment-variables-or-keeping-your-secrets-secret-in-a-node-js-app-99019dfff716
Local Data and App State Management with CouchDB
https://medium.com/ibm-watson-data-lab/cloudant-couchdb-pouchdb-local-documents-b8cef869f9bd
Some nice tipps for working with your local dev server:
Upgrade your SSH keys!
Starter Tutorial with some basic Linux, Nginx and PHP Tooling
https://serversforhackers.com/s/start-here
https://serversforhackers.com/dockerized-app
Some nice starter code snippets with nginx
https://medium.freecodecamp.org/an-introduction-to-nginx-for-developers-62179b6a458f
Very interesting approach of PiBakery for rapid prototype a Serverless API Infrastructure with OpenFaaS on Raspberry Pi
https://medium.com/@JockDaRock/serverless-at-the-edge-up-and-running-w-openfaas-docker-on-a-raspberry-pi-multi-node-cluster-e0957f4d8a49
After the first setup, your API Gateway could be design like this with an additional Object Storage Server Minio
https://medium.com/statuscode/integrating-openfaas-and-graphql-experimental-1870bd22f2a
https://blog.alexellis.io/openfaas-storage-for-your-functions/
https://www.thepolyglotdeveloper.com/2017/02/using-raspberry-pi-distributed-object-storage-minio/
https://blog.alexellis.io/hardened-raspberry-pi-nas/
If you have to do Remote NodeJS debugging on your Raspberry Pi that little tutorial will help
https://dev.to/attilavm/inspect-your-node-application-on-a-different-machine-3laf