These are the steps needed to set-up a simple node.js server on an Amazon EC2 instance. Based on Shawn Van Every's "Live Web" class at NYU ITP. Make sure to follow all steps. Steps that are optional will be marked accordingly. Steps: ************************************************************ 1. Set up an Amazon Web Services accounts (AWS) ************************************************************ Sign-up: http://aws.amazon.com/free/ Then: https://console.aws.amazon.com/console/home Then: Click on EC2 Then: Check what region you are in (top right - you may want to change to a region closer to you) Then: Click on "Launch Instance" ************************************************************ 2. Create the EC2 Instance ************************************************************ Choose the Amazon Linux 64-bit instance Make sure it is a micro instance so it'll be free (it should be; you can always change this / scale up later if need be) Click "Review and Launch" and then "Launch" ************************************************************ 3. Set up your Key Pair ************************************************************ To access your instance, you need a key pair. (optional) If you already have one, then you know about this, and can just use an existing key pair and skip the rest of this step. Otherwise, create a new one by choosing "Create a new key pair" and give it a name (remember this name). Download the key pair. Before proceeding, let's get the key pair where it needs to be on your computer (leave your browser window, we'll come back in a second). Open "Terminal" on your computer (if you don't know where that is, just search Terminal in top right) First, we need to navigate to where the key paid has been downloaded to. This will probably be your downloads folder, so enter the following: cd (then press enter) cd Downloads (then press enter) You are now in the Downloads folder and can see that the key pair should be in the folder by entering: ls (then press enter) So now that we're in the correct folder, enter the below to move it to where we'll need it later (very important: make sure to change "keypairname" below to the actual keypairname you created at Amazon above! it won't work otherwise): mv keypairname.pem ~/.ssh/. chmod 400 ~/.ssh/keypairname.pem (it is now in the ssh directory where we want it, with the correct access permissions). ************************************************************ 4. Configure Your Amazon Instance ************************************************************ Go back to your browser window and click "Launch Instance" Click "View Instances" in bottom right (or in several other places in the console) Let's go ahead and configure it: -- First, click under "Name" and name it something so you can remember what you're using it for -- Then, make a note of the public DNS as you'll need it going forward (it will be something like "ec2-54-208-9-225.compute-1.amazonaws.com") -- Finally, we need to to open up some ports on our server to outside traffic. Amazon has a GUI tool in the console that allows us to do so. Find "Security Groups" in your console under the information for your instance and take note of the name (right-hand column). Mine is "launch-wizard-1". Then go to "Security Groups" on the left hand navigation and select the appropriate name. Choose the "inbound" tab and select "HTTP" next to "Create a new rule". Click "Add Rule" then "Apply Rule Changes". It should show up on the right as "80 (HTTP) 0.0.0.0/0". Now, in that same area, select "Custom TCP rule" and enter 8080 as the Port Range. Again, Click "Add Rule" then "Apply Rule Changes" and it will show on the right. ************************************************************ 5. Log into Your EC2 Instance from Terminal ************************************************************ Go back to Terminal You can login to your Amazon EC2 Instance (machine) by using ssh with the key pair you created earlier. Enter the below in Terminal (very important, you need to change the name of keypairname and AmazonPublicDNS to the actual keypairname and AmazonPublicDNS that we took note of earlier): ssh -i ~/.ssh/keypairname.pem ec2-user@AmazonPublicDNS so, for example, it would look something like ssh -i ~/.ssh/itpwebkey.pem ec2-user@ec2-54-200-30-78.us-west-2.compute.amazonaws.com (if you run into problems here, make sure you are getting the names right) ************************************************************ 6. Configure Your EC2 Instance from Terminal ************************************************************ We need to run the following commands (press enter after each one, type in y when prompted (press enter), let it run): sudo yum update sudo yum install gcc-c++ make sudo yum install openssl-devel sudo yum install git git clone git://github.com/joyent/node.git cd node git checkout v0.10.18-release make (this will take some time) sudo make install (this actually installs node on the cloud machine) Let's create a directory to house our app/server mkdir ~/app cd ~/app Now that node is installed, and we have a directory for our app, we can install some node modules which will help us out. So that we can keep our server up and running even when we log out, we will install Forever, a node module that allows us to run a node server in the background. Enter the following in Terminal: npm install forever (optional if you want to work with Web Sockets) To install socket.io, enter the following in Terminal: npm install socket.io *************************************************************************** 7. Build your app (server.js / index.html) and get it on your EC2 instance *************************************************************************** Our EC2 instance is now configured, but we need to get an actual node server to the EC2 instance. Skip to the bottom of this document and you can use Shawn Van Every's server.js and index.html as an example (make sure to name these files server.js and index.html, and change the configurations as detailed up top). To get these files to our EC2 instance, we are going to use Cyberduck (you can use Terminal too). Open Cyberduck In Cyberduck, choose "New Connection", "SFTP" instead of "FTP", put in your hostname from Amazon (like http://ec2-54-208-9-225.compute-1.amazonaws.com/), put in "ec2-user" as the username and finally check the box that says Use Public Key Authentication (at bottom). This will give you the opportunity to select your key file (make sure to choose the .pem file with the correct name from above). Click into "home > ec2-user > app" Now, put your server.js and index.html into this folder Your server and index.html are now at the EC2 instance! Once last step... you need to start the server. Go back to Terminal, and enter the following (if you named your server file different than server.js, you need to change the file name below): node_modules/forever/bin/forever start server.js Your server is now started and will run forever in the background. Go to your site --> which is your public DNS you've been using, with :8080 tacked on to the end. For example: http://ec2-54-200-152-226.us-west-2.compute.amazonaws.com:8080/ If you ever make a change to your server.js file, you will need to 1. Re-upload the updated file to the EC2 instance, using Cyberduck as described above 2. Stop and restart the server so that the updated server is running. Example --> STOP THE SERVER: node_modules/forever/bin/forever stop server.js RESTART THE SERVER: node_modules/forever/bin/forever start server.js **************************************************************************************************************************************************************************************************************************************************************************************************************************************************************** END OF TUTORIAL, SAMPLE/STARTER FILES BELOW *************************************************************************** Sample/Starter Files *************************************************************************** *************************************************************************** server.js (make a file called server.js and save it with below code) *************************************************************************** // HTTP Portion var http = require('http'); var fs = require('fs'); // Using the filesystem module var httpServer = http.createServer(requestHandler); httpServer.listen(8080); function requestHandler(req, res) { // Read index.html fs.readFile(__dirname + '/index.html', // Callback function for reading function (err, data) { // if there is an error if (err) { res.writeHead(500); return res.end('Error loading index.html'); } // Otherwise, send the data, the contents of the file res.writeHead(200); res.end(data); } ); } var io = require('socket.io').listen(httpServer); io.sockets.on('connection', function (socket) { socket.on('message', function (data) { console.log("message: " + data); socket.broadcast.emit('message',data); }); }); ******************************************************************************************************** index.html (make a file called index.html and save it with below code) very important - change CHANGEME below with the actual IP of your Amazon instance, with :8080 at the end like "http://ec2-54-208-9-225.compute-1.amazonaws.com:8080/" ********************************************************************************************************
No Messages Yet