{"id":352,"date":"2018-03-31T21:48:16","date_gmt":"2018-03-31T21:48:16","guid":{"rendered":"https:\/\/alexrusin.com\/?p=352"},"modified":"2018-03-31T21:59:34","modified_gmt":"2018-03-31T21:59:34","slug":"node-js-app-on-aws-instance","status":"publish","type":"post","link":"https:\/\/blog.alexrusin.com\/node-js-app-on-aws-instance\/","title":{"rendered":"Node.js App on AWS EC2 Instance"},"content":{"rendered":"
To get started let’s spin up an Ubuntu 16.4 LTS instance on AWS.\u00a0 After instance is up and running, we need to add port 80 to the instance’s security group inbound rules.\u00a0 There should be at least two ports open: 22, and 80.<\/p>\n
Now we can ssh into the instance and install Nginx, Node, and Supervisord.\u00a0 Let’s start with Nginx<\/strong>.<\/p>\n Node.js<\/strong><\/p>\n Supervisord<\/strong><\/p>\n Now let’s create a Node.js server application using Express and make it listen on port 3000.<\/p>\n Let’s make Supervisord “supervise” our app. Create the following file \/etc\/supervisor\/conf.d\/node-server.conf<\/strong><\/p>\n After that reload configuration file.<\/p>\n Let’s configure Nginx to listen on port 80 and proxy all requests to port 3000 by creating the following config file \/etc\/nginx\/sites-available\/nodeserver<\/strong><\/p>\n Enable the site and reload Nginx.<\/p>\n We should have our node application running and accessible via node-server.com In order to check that add to your hosts file line that points node-server.com to ip of the created AWS instance.<\/p>\n To get started let’s spin up an Ubuntu 16.4 LTS instance on AWS.\u00a0 After instance is up and running, we need to add port 80 to the instance’s security group inbound rules.\u00a0 There should be at least two ports open: 22, and 80. Now we can ssh into the instance and install Nginx, Node, and…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[9],"tags":[],"class_list":["post-352","post","type-post","status-publish","format-standard","hentry","category-system-administration"],"yoast_head":"\n sudo add-apt-repository -y ppa:nginx\/development\r\n sudo apt-get update\r\n sudo apt-get install -y nginx\r\n<\/pre>\n
sudo su \r\n curl --silent --location https:\/\/deb.nodesource.com\/setup_8.x | bash -\r\n apt-get update\r\n apt-get install nodejs \u2013y\r\n \/usr\/bin\/npm install -g npm\r\n exit\r\n<\/pre>\n
sudo apt-get install -y supervisor\r\n sudo service supervisor start\r\n<\/pre>\n
cd \/var\/www\/html\r\nsudo mkdir node-server\r\nsudo chown ubuntu: node-server\r\nsudo chmod 770 node-web-server\r\nsudo chmod g+s node-web-server\r\ncd node-server\r\nnpm init -y\r\nnpm install express --save\r\ncat > app.js << EOF1\r\n \r\nvar express = require('express');\r\nvar app = express();\r\n \r\napp.get('\/', function(req, res){\r\n res.send('Hello Express!');\r\n});\r\napp.listen(3000, function(){\r\n console.log('Server is listening on port 3000');\r\n});\r\nEOF1\r\n<\/pre>\n
[program:nodeserver]\r\ncommand=\/usr\/bin\/node \/var\/www\/node-server\/html\/app.js\r\ndirectory=\/var\/www\/html\/node-server\r\nautostart=true\r\nautorestart=true\r\nstartretries=3\r\nuser=ubuntu\r\n<\/pre>\n
supervisorctl reread\r\nsupervisorctl update\r\n<\/pre>\n
server {\r\n listen 80;\r\n server_name node-server.com;\r\n\r\n location \/ {\r\n proxy_set_header X-Real-IP $remote_addr;\r\n proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\r\n proxy_set_header Upgrade $http_upgrade;\r\n proxy_set_header Connection \"upgrade\";\r\n proxy_set_header Host $host;\r\n proxy_http_version 1.1;\r\n proxy_pass http:\/\/127.0.0.1:3000;\r\n }\r\n\r\n access_log off;\r\n error_log \/var\/log\/nginx\/node-server-error.log error;\r\n}\r\n<\/pre>\n
sudo ln -s \/etc\/nginx\/sites-available\/nodeserver \/etc\/ngix\/sites-enabled\/nodeserver\r\nsudo nginx -t\r\nsudo service nginx reload\r\n<\/pre>\n
Resources used<\/h4>\n
\n
Setting up Nginx, PHP, and Laravel<\/a><\/h5>\n<\/li>\n
Monitoring Processes with Supervisord<\/a><\/h5>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"