NGINX and Node on Ubuntu 12.04

Asif Ahmed

In previous posts, we have covered how to set up an Ubuntu server and how to set up an Apache (web) server. There is, however, another web server that lately has been more popular and is very efficient. The other web server is called nginx. A mentor has mentioned to me several times that nginx (it’s usually spelled lowercase, unlike Apache) is amazing at serving static data (like images, HTML, CSS, JS, and other asset files). Of course because it’s strength is at serving static files it usually works alongside other different technologies for serving up dynamic pages. It can be used with Apache (for serving up PHP mostly), a Rails server (Mongrel, Unicorn, Puma, Passenger), or Node.js for serving up Javascript (it’s all the rage nowadays anyways). For this tutorial we will be going over how to install nginx and Node.js on the current long term support version of Ubuntu (which is 12.04) on AWS (Amazon Web Services).

  1. First log in to your AWS Management console and start up a new micro instance. Be sure to select Ubuntu 12.04 and I’d go with the 64 bit version. Also select a security group that has ports 22, 80, and 443 open. Port 22 is for SSH so that you can access your machine. For good security practices, I would restrict the IPs that can access your port 22 to the IPs you will be developing from. For better security practices, I would use a different port for SSH, but you need to log in at least once on port 22 on Amazon before you can change this (you need to SSH on the server before you can edit services on the server). For best practices, I would combine the previous two suggestions.
  2. Once you have an Ubuntu server up and running, log in using your AWS keyfile  that you specified while setting up your server.
    ssh -i keyfile.pem ubuntu@your-ip-address
  3. Then update your server using the following command:
    sudo apt-get -y update && sudo apt-get -y upgrade
  4. Then install nginx using one beautifully simple command:
    sudo apt-get install nginx
  5. Now start up nginx:
    sudo service nginx start
  6. Afterwards, go ahead and hit your IP so if it nginx is running. You should see something saying “Welcome to nginx!”.
  7. Now, let’s install Node.js. Enter in the commands below (or you can read more at the link here):
    sudo apt-get install -y python-software-properties python g++ make
    sudo add-apt-repository ppa:chris-lea/node.js
    sudo apt-get update
    sudo apt-get install nodejs
  8. We should write a simple web server with Node just so we can test it out. Open up an “example.js” file with vim (or a different editor that comes with Ubuntu) and enter in the following (taken from the official Node.js website):
    var http = require('http');
    http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
    }).listen(1337, '');
    console.log('Server running at');
  9. Then start up the Node server, but run it in the background so it’s doesn’t hijack your terminal:
    node example.js &
  10. Verify that the Node.js server is working correctly by using the wget Linux utility:
    wget localhost:1337
  11. Check the index.html file that was generate by using wget. If you use “cat index.html” you should see that the contents of index.html are “Hello World”.
  12. Okay so we have nginx installed and Node installed, but the two aren’t quite connected…yet. We will need to configure nginx so that it can point to your Node service. Open up the nginx default config file:
    sudo vim /etc/nginx/sites-enabled/default
  13. Paste the following directly under the line “location / {” (around line 30 on my install):
    proxy_pass http://localhost:1337;
  14. Now restart nginx:
    sudo service nginx restart
  15. If you hit your IP and then hit refresh on your browser, you should now be able to see “Hello World” in your browser.

We have successfully set up nginx and Node on our Ubuntu server, but this is the most basic of installs. We still need a couple more things to actually have a useful install of Node with nginx. We will cover those in our next post, but for now we can feel satisfied that we installed nginx and Node on our server!