I'm going to walk you through a setup of a new, clean VPS[1] with Nginx v1.9.6 & Nodejs v4, based on Ubuntu Server LTS 14.04, with no default packages installed. Nginx will have the role as reverse proxy to the Nodejs application. In the short future Nginx will also be serving the static files.

Prerequisites

  • Basic knowledge about Linux. There is no GUI[1:1], only a terminal available
  • VPS or local hardware with a clean installation of Ubuntu server
  • There is an (open)ssh server installed

Setup

First update the OS with latest patches

Normally you can update the package list and apply the updates as following:

$ sudo apt-get update
$ sudo apt-get upgrade

To upgrade the kernel and other critical stuff

$ sudo aptitude safe-upgrade

After upgrading the kernel, this requires often a reboot of the system.

$ sudo reboot now

Clean up

removing all packages that are not used or redundant

$ sudo apt-get autoclean
$ sudo apt-get clean
$ sudo apt-get autoremove

Install Nginx (mainline)

The default version of Nginx in Ubuntu package management system is really outdated. Therefore we need to add the Nginx repository to PPA[1:2]. This is done by adding the repository signing key into the archive.

$ sudo -i
$ nginx=development
$ add-apt-repository ppa:nginx/$nginx
$ exit

The $ exit command is in this particulair example for exiting the root mode, not closing the SSH connection. You could leave it open ofcourse, but personally don't like that.

$ sudo apt-get update
$ sudo apt-get install nginx
$ nginx -v

$ nginx -v should respond back with nginx version: nginx/1.9.6

When typing your domain or ip number in a browser should giving the following response:

Install Nodejs LTS (4.x)

Today there are newer versions of Nodejs available. I've got my reasons rather to choose for the stable version instead of heaving the newest features. The Nodejs version installed is already too new for the application it's going to run. This is something I need to solve later.
Again the Ubuntu default Nodejs version in package management system is way too old, but this time there is a simpeler method available to add the signing key to the archive. This method is currently not available for Nginx.
What happens here is following: Curl is a command line tool for getting or sending files using URL syntax. The option --location says if the server reports that the requested page has moved to a different location, this option will make curl redo the request on the new place. The | sudo bash executes the just downloaded script as sudo. So be careful here, with immediately executing downloaded scripts as sudo!

$ curl --location https://deb.nodesource.com/setup_4.x | sudo bash
$ sudo apt-get install nodejs
$ sudo apt-get install build-essential

Now we've got Nginx & Nodejs installed. We can create new directories with are going to be the webroot folder.

Create website root folder(s)

Nginx is set up with a basic configuration in place and that means that all of your website's files are located in /usr/share/nginx/html. We'll create one new folder named as your domain name and within this folder we create two new folders called httpdoc and httpsdocs.

$ cd /usr/share/nginx
$ sudo mkdir your-domain-name
$ cd your-domain-name
$ sudo mkdir httpdocs
$ sudo mkdir httpsdocs

The reason why we created separated folders for http traffic & https protocols is to be future proof. Of course both protocols can be steered to use one folder.
Nodejs doesn't comes with a default configuration. This makes it easier, because we don't have to reset default configuration. In the /usr/share folder we're creating the following folders;

$ cd /usr/share/
$ sudo mkdir nodejs 
$ cd nodejs
$ sudo mkdir your-domain-name
$ cd your-domain-name
$ sudo mkdir httpdocs
$ sudo mkdir httpsdocs

Configuring Nodejs

This is the most simple part as there is no configuration so far. We will run Nodejs just in this session and do not keep it alive forever. I will jump into this topic in another post.
We need to create a basic server and copy this script from the Nodejs website. Go to your Nodejs webroot directory and create the following file;

$ cd /usr/share/nodejs/your-domain-name/httpdocs
$ sudo nano server.js
var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(2368, '127.0.0.1');

console.log('Server running at http://127.0.0.1:2368/');

We could start the Nodejs server normal, but then your session is blocked and when you close the terminal all processes are killed. So we will start the node server in a background process;

$ nohup node server &

Nodejs is now running and we can configure Nginx.

Configuring Nginx

The last part is to configure Nginx to proxy all traffic to Nodejs. In the default Nginx installation the folder for the configuration is located in /etc/nginx/sites-available. You will find here the default config file. To be save just create a copy of this file and name it the same as your domain.

$ cd /etc/nginx/sites-available
$ sudo cp default your-domain-name

For this moment we're only going to proxy the your-domain-name website. We need to change the default root in your just created root.

$ sudo nano your-domain-name

There is normally a lot more of config in this file. Please leave that in. Just edit the pieces which are described below.

server {
  listen 80 default_server;
  listen [::]:80 default_server;
  server_name .your-domain-name.nl;
  root /usr/share/nginx/your-domain-name/httpdocs;
  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header HOST $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://127.0.0.1:2368;
    proxy_redirect off;
  }
}

Till now are new configuration file is only available in sites-available. To enable the configuration we need to create a symbolic link into the sites-enabled folder

$ cd /etc/nginx/sites-enabled
$ sudo ln -s /etc/nginx/sites-available/your-domain-name /etc/nginx/sites-enabled/your-domain-name

We also need to remove de default config

$ sudo rm -rf default

After successful editing & linking the new configuration file and removing the old default one, we can let Nginx test the configuration before we restart the server.

$ sudo nginx -t

If the test is successfully, you would see something like this;

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Now it's time to restart the Nginx service and see the result

$ sudo service nginx restart

Go back to your open browser, refresh the page and it should giving the following response:

That's it you now created an application accessed via a reverse proxy!


  1. PPA = Personal Package Archive ↩︎ ↩︎ ↩︎