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
-
get the source code (preferably through git) into it's own folder under
/www
-
npm
install any stuff it needs -
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
nginx-proxy.conf line 5 should read:
proxy_set_header Connection "upgrade";
The
$connection_upgrade
var is undefined.