Skip to content

Instantly share code, notes, and snippets.

@sandfox
Last active December 16, 2015 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sandfox/403a501a6e1046537cf1 to your computer and use it in GitHub Desktop.
Save sandfox/403a501a6e1046537cf1 to your computer and use it in GitHub Desktop.
Install scripts for a simple nodejs server to run generic node scripts/sites. Maybe one day replace with some chef / opsworks magic

The script should runnable as cloud init script supplied during instance creation by literally pasting the contents of cloud-init inside the box provided in the AWS web console, or by running the following command on already running instance :: curl -L https://gist.github.com/sandfox/403a501a6e1046537cf1/raw/node-runner-install.sh | sudo bash -s

Be warned that this script is not that idempotent - running it multiple times on a server may produce different results, and running it on a server that you have made other changes too may result in overwriting / trashing your changes.

The upstart related stuff comes from this gist, I might update that from time to time.

educational note

If you need lots of these servers and for them to always be the same it would probably make more sense to use this script to generate a server, then turn that into an AMI and create new servers from that AMI. Making AMI's can be a little time consuming though so probably not worth for one or two servers, and it also a faff if you constantly update your base server settings and packages as you'd need to create new AMI's each time.. (but that is what automation is for)

Also running this script at different times may render different results in terms of what version of certain ubuntu package are installed. Unlikely to be a problem but do beware!

This script will leave you with a bare bones server ready to run nodejs projects that are reverse-proxied through nginx.

To run a project all you have to do is

  1. get the source code (preferably through git) into it's own folder under /www

  2. npm install any stuff it needs

  3. create an nginx vhost for it in /etc/nginx/site-available and sym-linking to it from /etc/nginx/site-available.

$ pwd /etc/nginx/sites-enabled $ sudo ln -s ../sites-available/mysite ./


4. check the config with `sudo /etc/init.d/nginx configtest`
4. create an upstart script in `/etc/node/` for the site. e.g. `/etc/node/mysite.conf`
4. start the nodejs script `sudo start node NAME=mysite`
5. reload nginx `sudo /etc/init.d/nginx reload`
6. Make sure the domains DNS is pointing at the right load balancer/server and your security groups are sane.
7. Profit!
8. 

-------------

### Todo

1. Add some basic log-rotation scripts otherwise by default we'll eventually fill up the disks
2. Add some simple scripts to ease creation of node and nginx site configs
#include https://gist.github.com/sandfox/403a501a6e1046537cf1/raw/node-runner-install.sh
server {
##Change this!
server_name example.com;
##If SSL support is needed for websockets uncomment these three lines and make sure the cert paths are correct for the domain
ssl on;
ssl_certificate /etc/nginx/ssl/XX.crt;
ssl_certificate_key /etc/nginx/ssl/XX.key;
access_log /var/log/nginx/access-$server_name.log
error_log /var/log/nginx/error-$server_name.log
##Send everything to the proxied server
location / {
##Change this port number to match whatever port your upstart script is listening
proxy_pass http://localhost:9999;
include http-proxy-params.conf;
}
}
##Generic sane proxying settings that uses keep-alive
proxy_next_upstream error timeout invalid_header http_500 http_502 http_50$
proxy_redirect off;
proxy_buffering off; ##buffering on causes problems with web sockets
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade; #this allows websockets stuff
proxy_set_header Connection $connection_upgrade; #this allows websocket stuff
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#!/bin/sh
##Add some Fresh repos so we can have superfly versions of cool stuff like nginx/nodejs
##If you are a slightly less gung-ho person, use nginx-stable and comment out the dev ppa
#add add-apt-repository ppa:chris-lea/nginx-stable -y
add-apt-repository ppa:chris-lea/nginx-devel -y
add-apt-repository ppa:chris-lea/node.js -y
apt-get update
apt-get dist-upgrade -y
apt-get install -y nodejs git nginx-light
##install 'n' to allow multiple versions of node
npm install -g n
##install latest stable version of node
## Add other versions too if you would life them to always be around
n --stable
##create folder for projects to go in
mkdir /www
chown ubuntu:ubuntu /www
##copy down the generic proxy config
curl https://gist.github.com/sandfox/403a501a6e1046537cf1/raw/nginx-proxy.conf > /etc/nginx/http-proxy-params.conf
##nuke the existing default site
rm /etc/nginx/sites-enabled/default
##create template for sites
curl https://gist.github.com/sandfox/403a501a6e1046537cf1/raw/nginx-nodejs-template.conf > /etc/nginx/sites-available/template.conf
##create folders for ssl certs
mkdir /etc/nginx/ssl
##start nginx - because well designed packages don't autostart on install!
/etc/init.d/nginx start
##copy down the base node upstart script
curl https://gist.github.com/sandfox/403a501a6e1046537cf1/raw/node-upstart.conf > /etc/init/node.conf
##make the 'per site' configs for node upstart
mkdir -p /etc/node/
##copy down an example 'per site' upstart script
curl https://gist.github.com/sandfox/403a501a6e1046537cf1/raw/node-upstart-example.conf > /etc/node/node-example.conf
##make the logs folder for node projects
mkdir -p /var/log/node
NODE_VERSION="0.10.5"
NODE_PATH="/www/node-test"
LOG_PATH="/var/log/node/node-test.log"
NODE_SCRIPT="index.js"
NODE_ENV="production"
PORT=9999
description 'nodejs sites upstart script'
author 'sandfox'
start on (local-filesystems and net-device-up)
stop on shutdown
instance "Node - $NAME"
respawn
respawn limit 5 60
script
##Loads the config file the name provided
. /etc/node/$NAME.conf
chdir ${NODE_PATH}
exec sudo -u www-data NODE_ENV=${NODE_ENV} PORT=${PORT} /usr/bin/n use ${NODE_VERSION} ${NODE_SCRIPT} >> ${LOG_PATH} 2>&1
end script
@squarefeet
Copy link

Line 43 in node-runner-install.sh:

mkdir -r /etc/node/

Needs to be:

mkdir -p /etc/node/

@sandfox
Copy link
Author

sandfox commented May 14, 2013

Fixed! Thanks

@squarefeet
Copy link

nginx-nodejs-template.conf is missing semicolons on lines 11 and 12

@squarefeet
Copy link

nginx-proxy.conf line 3 should read:

proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;

@squarefeet
Copy link

nginx-proxy.conf line 5 should read:

proxy_set_header Connection "upgrade";

The $connection_upgrade var is undefined.

@squarefeet
Copy link

sudo npm install -g n
npm http GET https://registry.npmjs.org/n
npm http 304 https://registry.npmjs.org/n
/usr/bin/n -> /usr/lib/node_modules/n/bin/n
npm WARN package.json github-url-from-git@1.1.1 No repository field.
npm WARN package.json assert-plus@0.1.2 No repository field.
npm WARN package.json ctype@0.5.2 No repository field.

I'll dig deeper into this, but looks like the n package is borked.

EDIT: Found the issue. It wasn't the package.json for the n package, but rather that n [version] was never called. I specifically executed n 0.10.10 and my errors (Error 0.10.10 is not installed) went away.

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