Skip to content

Instantly share code, notes, and snippets.

@jmsaavedra
Forked from piuggi/AWS_NODE_FOREVER_SETUP.md
Last active April 23, 2018 19:10
Show Gist options
  • Save jmsaavedra/b35462b9e3a65d4bba36 to your computer and use it in GitHub Desktop.
Save jmsaavedra/b35462b9e3a65d4bba36 to your computer and use it in GitHub Desktop.
AWS Ubuntu Server Setup incl Node, Mongodb, Port Forwarding

AWS SERVER CONFIGURATION

Configuring an Ubuntu AWS Box with Node, Mongodb, and Forever with port forwarding


  • Login to aws.amazon.com and create an EC2 instance with a standard Ubuntu (14.04) installation.
  • Create a Security Group that allows type SSH on port 22 and HTTP on port 80.
  • Download the .pem file (during setup) which we'll use to authenticate into your server via terminal.
  • Modify permissions on the .pem file downloaded from AWS
$ chmod 0600 ~/Downloads/{your_key}.pem

SSH to your server and create a user

Login in to your AWS box at it's IP address as it's default user (ubuntu) using your local .pem as the credential.

$ ssh ubuntu@{your-PublicDNS-or-PublicIP} -i ~/Downloads/{your_key}.pem 
# may also be "root" or "ec2-user"
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo useradd {your_user} -m
$ sudo passwd {your_user}
$ sudo visudo
  • add user to sudo group under root:
# User privilege specification
root    ALL=(ALL:ALL) ALL
joe     ALL=(ALL:ALL) ALL
  • hit esc then ':wq' to save
$ su {your_user}
$ cd ~/

set your ssh settings

$ sudo vim /etc/ssh/sshd_config
  • edit the document (type i) and set PasswordAuthentication to yes. save the file by hitting esc and then :wq
$ sudo service ssh restart
$ sudo chsh -s /bin/bash {your_user}
$ sudo reboot
  • SSH and SFTP are both working now. Log in with $ ssh {your_user}@{your-PublicDNS-or-PublicIP}

if, upon logging in again, line starts with only a "$" and not "user@ip-00-0-00-00:~$" (and hitting up arrow is not Bash history) then 'chsh' has failed.

> $ sudo vim /etc/passwd
  • change your user line to end with /bin/bash.
  • example: user:x:1001:1001::/home/user:/bin/bash
  • save and esc: :wq
> $ getent passwd user
  • should return user:x:1001:1001::/home/user:/bin/bash

installing git, mongo, and node via nvm

$ sudo apt-get install git-core
$ sudo apt-get install mongodb
$ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | sh
$ source .profile 
$ nvm install stable        #or specific version, ex: nvm install 0.10
$ nvm use stable            #or version
$ which node                #verify what version got installed
$ nvm alias default stable  #or version
$ sudo reboot

install redis

$ sudo apt-get install make
$ wget http://download.redis.io/redis-stable.tar.gz
$ tar xvzf redis-stable.tar.gz
$ cd redis-stable
$ make

from: http://redis.io/topics/quickstart

clone your repository

$ git init 
$ git remote add origin {https://github.com/user/your_repo.git}
$ git pull origin {branch}

setup port forwarding so :80 points to your app's port

$ sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port {your_port}
$ sudo touch /etc/init.d/portforwarding.sh
$ sudo vim /etc/init.d/portforwarding.sh
  • add the following to portforwarding.sh:

      #Init port forwarding
      sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port {your_app's_port}
    
  • save with esc and then :wq .

    $ sudo chmod +x /etc/init.d/portforwarding.sh
    $ sudo update-rc.d portforwarding.sh defaults

pm2 is the new forever:

$ npm install pm2 -g        #pm2 is the new 'forever'
$ pm2 startup               #follow directions if there is a reply from pm2!
$ pm2 start myapp.js        #run your app with pm2
$ pm2 save                  #save this process to the startup scripts
$ pm2 monit                 #process monitor
$ pm2 logs                  #tail console logs

final reboot. you're done!

$ sudo reboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment