Skip to content

Instantly share code, notes, and snippets.

@wprater
Forked from aronwoost/README.md
Last active January 3, 2016 04:49
Show Gist options
  • Save wprater/8411417 to your computer and use it in GitHub Desktop.
Save wprater/8411417 to your computer and use it in GitHub Desktop.

#How to install node.js on a Google Compute Engine instance

Start and ssh into instance

Make sure you have a Google Compute engine account, have a project created and the gcutil command line tool installed.

Since want to ssh without the gcutil tool, you need to a your ssh key to the instance in addition to the already existing google_compute_engine key (used for gcutil).

cd $HOME/.ssh
gcutil addinstance --project=<project id> <instance name> --authorized_ssh_keys=username:google_compute_engine.pub,username:id_rsa.pub

Select region, select instance type, use persistance boot disc, use debian 7.

gcutil ssh --project=<project id> <instance name>

Install node.js

sudo apt-get install python g++ make checkinstall
mkdir ~/src && cd $_
wget -N http://nodejs.org/dist/node-latest.tar.gz
tar xzvf node-latest.tar.gz && cd node-v*
./configure
sudo checkinstall

In the dialog remove the "v" in front of the version number.

Test it:

node
> 1+1
ssh -L 5984:127.0.0.1:5984 <external ip of instance>

Create node app

sudo mkdir /var/www/mydomain
cd /var/www/mydomain
sudo nano app.js

Copy, paste and save:

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');

Install and config nginx

sudo apt-get install nginx
cd /etc/nginx/sites-available
sudo nano mydomain.com

Copy, paste and save:

server {
  listen 80;
  server_name mydomain.com mydomain;
  access_log /var/log/nginx/yourdomain.log;

  # pass the request to the node.js server with the correct headers and much more can be added, see nginx config options
  location / {
    proxy_pass http://127.0.0.1:3000;
  }
}
sudo ln -s mydomain /etc/nginx/sites-enabled/mydomain.com
sudo /etc/init.d/nginx restart

Start node app

sudo npm install -g forever
forever start /var/www/mydomain/app.js
# to kill
# forever list
# forever stop 0 (for example)

Test

Open external IP of instance (find it gcutil getinstance --project=<project id> <instance name>) in browser.

Sources

Installing node
Node and nginx
Setup Ghost with nginx on Debian
Google Compute Engine - Instances and Networks
Google Compute Engine - Connecting to an Instance Using ssh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment