IoT Shaman

Configuring Raspberry Pi to Run a Node.js Server

by Kyle Brown, July 2nd 2017

Over the last few years Node.js has become my favorite platform for running simple web interfaces / IoT modules. Considering the immense amount of packages to choose from, the minimal amount of scaffolding needed, and the inherent performance benefits of Google's V8 and the non-blocking IO architecture, Node.js is perfectly suited for small, internal projects. Combine that with the low-price and small size of the Raspberry Pi and you have an amazing opportunity for building an impressive, low-cost internal network of web applications and IoT modules.

Before we continue, if you have not already read our article on 'Getting Started with Raspberry Pi' then please do so before reading any further. If you are already up and running with your Raspberry Pi then please continue.

Installing Node.js and Git

To get started, go ahead and open an SSH terminal session with your Raspberry Pi (alternatively you could plug into a monitor and use a keyboard / mouse combo and open the Gnome terminal). Make sure to login with a user that has sudo privileges. Once you have your terminal open, enter the following commands:

$ sudo apt-get install nodejs npm node-semver -y 
$ sudo npm install n -g
$ sudo n stable
$ sudo npm install npm@latest -g

We also want to install git, since almost every project you will want to interact with will be available in a git repository. To download and install, enter the following command into the terminal:

$ sudo apt-get install git -y

After installing these packages you will need to restart your RPi. Enter sudo reboot and wait for it to reboot.

Download Sample Node.js Application

We will want a Node.js server up and running to test our RPi configuration, so lets go ahead and download one and start it up. If you already have a Node.js server project in mind, skip this step and install your own project. In your terminal, navigate to a folder where you want to download the sample project, then enter the following commands:

$ git clone https://github.com/kbrown333/api_starter.git
$ cd api_starter
$ npm install
$ npm start

Once this is complete, you should have a Node.js application downloaded, installed and running. To verify that the application is being served, open a web browser and navigate to the below address (make sure to replace [internal-ip] with your Raspberry Pi's internal IP address). If you do not know the internal address of your RPi, in your terminal enter sudo ifconfig and locate the value for 'inet address' (probably in wlan0 if you are running on wifi).

http://[internal-ip]:3000

A webpage should render with the message of 'Coming Soon!'. Congratulations, you now have a running Node.js server! Let's not stop there, lets continue on and place this behind an Apache Virtual Host, which will give us more options for security and allow us to access it via a domain name, instead of IP address and port.

Install and Configure Apache

In this final step we will be downloading, installing and configuring apache2. You might be asking yourself "I already have my server up and running now, why would I need to do this?" The short answer is, you don't. However, there are some instances where this might add some desperately needed value. Here are some things you should consider before making a decision:

  • Apache can return an 'Under Maintenance' page when your application is down or unavailable
  • Apache offers load-balancing capabilities, if you might need to scale your resources
  • If you want to serve content via multiple technology stacks, like PHP for web pages and Node.js for REST API's

To begin setting up apache2, we need to start by downloading and installing it. Enter the following command into your terminal:

$ sudo apt-get install apache2 -y

Once apache2 is installed, we need to configure a couple modules:proxy and proxy_http. Enter the following commands into your terminal:

$ sudo a2enmod proxy
$ sudo a2enmod proxy_http
$ sudo service apache2 restart

The last thing we need to do is set the default apache site to point towards our Node.js port, using the proxy mods we just configured. Navigate to the folder /etc/apache2/sites-available. Let's first backup the default configuration file so we can rollback if needed. Enter the following command:

sudo cp ./000-default.conf ./000-default.conf.backup

Once this is complete, open the file '000-default.conf' in a text-editor (sudo nano ./000-default.conf). Make the file look like following example:

<VirtualHost *:80>

        ServerName sample-server.local
        ServerAlias www.sample-server.local

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        ProxyRequests off
        <Proxy *>
                Order deny,allow
                Allow from all
        </Proxy>
        <Location />
                ProxyPass http://localhost:3000/
                ProxyPassReverse http://localhost:3000/
        </Location>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

The above file contents will configure the default virtual host to act as a reverse proxy to your Node.js application. A similar process can be followed to create more virtual hosts pointing towards even more Node.js applications. For a detailed article explaining apache2 Virtual Hosts, check out this article.

Go ahead and save / close your editor. Once you are back in your terminal, enter the following command to restart the apache2 service.

$ sudo service apache2 restart

One Final Step

You should now be officially up and running, you can now simply enter the internal IP address of your RPi into the address bar of your browser and your Node.js server should return a landing page. To make it possible to simply enter a website name into the address bar, you will want to configure the host file on either your computer or router. Since there are many different places a host file might be, please read this article. Below is a sample entry in a generic hosts file that routes the host name 'sample-server.local' and 'www.sample-server.local' to an example internal IP address (192.168.1.33):

192.168.1.33 sample-server.local
192.168.1.33 www.sample-server.local

This concludes our article on configuring your Raspberry Pi to run a Node.js server, we hope you found it very informative and helpful. For more articles like this, please visit our blog page, or to get involved in one of our open-source project, please visit our projects page. Have a great day!