Setting up a node js website on aws ec2 instance
Download node version manager
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
Activate node version manager, nvm
. ~/.nvm/nvm.sh
Install node version 6.10.3 or any other version of your choice
nvm install 6.10.3
Add these two lines at the end of /home/ec2-user/.bash_profile and /home/ec2-user/.bashrc to initialize node commands on login
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/home/ec2-user/.nvm/versions/node/v6.10.3/bin export PATH
Check node is installed and running correctly
node -e "console.log('Running Node.js ' + process.version)"
Let’s create a simple node server, create a file called hello.js and put the following in it.
// Load the http module to create an http server. var http = require('http'); // Configure the HTTP server to print Hello World! to all requests. var server = http.createServer(function (request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.end("Hello World!\n"); }); // Listen on port 8080 server.listen(8080); console.log('Server available at http://localhost:8080/');
Install pm2 for running your node server
npm install -g pm2
Redirect port 80 to 8080, so your node server can be reached from the browser.
sudo iptables -A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT sudo iptables -A INPUT -i eth0 -p tcp --dport 8080 -j ACCEPT sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
Start the node server hello.js
pm2 start hello.js
Now open a browser and type the ip address of your ec2 server and you should see Hello World! delivered from your ec2 instance.
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts