I will take you through the process of setting up your first server on an Amazon Elastic Compute Cloud (EC2) Ubuntu Server.
- Tip: sign up with a new email if your account is older than a year
- Visit aws ssh key pairs
- I have found it's easier to upload a public key that you've created on your own machine. Visit Github Help if you need help creating your own public/private key pair.
- Google AWS Marketplace
- Search for Ubuntu
- I chose this 64 bit image, you should too for this tutorial.
- click the big yellow continue button
- accept default options, except:
- make sure t1-micro is selected in EC2 Instance Type
- Launch with 1-Click
- Visit your EC2 Dashboard
- instance state will be 'running' eventually
- find Public IP column and note address
ssh ubuntu@PUBLIC-IP-ADDRESS- make an A record on your domain in Route 53 for convenience
The -y option is helpful because apt won't for wait for you to press 'y', it
will just install the packages. Very helpful for when you're trying to script
this entire process.
sudo apt-get update && sudo apt-get install -y build-essential g++ tmux
curl -O http://nodejs.org/dist/v0.10.29/node-v0.10.29.tar.gz
tar -xvzf node-v0.10.29.tar.gz
cd node-v0.10.29
./configure --prefix=/opt/node
make
sudo mkdir -p /opt/node
sudo chown -R ubuntu.ubuntu /opt/node
make install
Add node to your path in ~/.bashrc:
echo "export PATH=/opt/node/bin:$PATH" >> ~/.bashrc
Then reload .bashrc
source ~/.bashrc
Double check to see that node is in your path:
which node => should be /opt/node/bin/node
Now, we need to add node to root's path too. To do this, we will need to use the
visudo command to edit the secure path.
sudo visudo
edit your Defaults secure_path= line, around the thrird line, to look like:
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/node/bin"
The key here is to put the path to node at the end of the secure path.
Go ahead and save the file.
Follow the directions here: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
To summarize:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
sudo apt-get update
sudo apt-get install mongodb-org
Luckily, Chris Lea keeps an up-to-date ubuntu ppa available.
sudo add-apt-repository ppa:chris-lea/redis-server
sudo apt-get update
sudo apt-get install redis-server -y
sudo add-apt-repository ppa:git-core/ppa
sudo apt-get update
sudo apt-get install git -y
mongo
show dbs
- ctrl-d to exit
redis-cli ping--> should seePONG
You can always find the External IP address of your server in the EC2 Dashboard, but I frequently use this shortcut from the command line:
curl icanhazip.com
I mean, install bower and any other global npm packages you use frequently.
npm -g install bower grunt-cli
I'll use one of our example apps.
Make sure you're in the ubuntu home directory: /home/ubuntu
cd
git clone https://github.com/codefellows/javascript-b15-notes.git notes
cd notes
npm install && bower install
To launch your app, and bind on any port under 1000, you need to use sudo to
escalate to root privelege.
sudo -i
PORT=80 node server.js
visit the site http://YOUR-IP-HERE
This will do in a pinch, but it's not a professional setup. What happens if your server reboots? You want something to re-start the server automatically.
npm -g install forever. Forever is a simple CLI tool for ensuring that a given script runs continuously.
Create /etc/init/notes.conf. This is an Ubuntu Upstart script.
You can always use nano if you are afraid of Vim…
/etc/init/notes.conf:
start on startup
stop on shutdown
expect fork
script
PATH=/opt/node/bin:$PATH
exec forever start /home/ubuntu/notes/server.js
end script
pre-stop script
PATH=/opt/node/bin:$PATH
exec forever stop /home/ubuntu/notes/server.js
end script
Then sudo start notes to start the app
You can use use sudo status notes to see the status of the service.