Skip to content

Instantly share code, notes, and snippets.

@piuggi
Last active December 30, 2015 20:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save piuggi/7879659 to your computer and use it in GitHub Desktop.
Save piuggi/7879659 to your computer and use it in GitHub Desktop.
AWS Ubuntu 13.10 Setup Install for Node.js/Forever w. port forwarding for 80 Supports server reboots etc...

###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 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

Now, 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_aws_url.com} -i ~/Downloads/{your_key}.pem 
$ 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
$ su {your_user}
$ sudo ls
$ cd ~/
$ sudo vim /etc/ssh/sshd_config
	--set your ssh settings 
$ sudo service ssh restart
	--test login via ftp/ssh
$ sudo chsh -s /bin/bash {your_user}
$ sudo reboot
  • 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

continue configuring group and user

$ sudo groupadd www
$ sudo adduser {your_user} www
$ sudo adduser root www
$ sudo mkdir /var/www
$ sudo chown -R root:www /var/www
$ sudo chmod -R 775 /var/www
$ sudo chmod -R a+wx /var/www/
$ cd ~/

installing git, mongo, node, and forever

$ 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 0.10
$ which node 
$ n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local
$ sudo reboot
$ sudo npm install forever -g 

####clone your repository and configure iptables

$ cd /var/www
$ git init 
$ git remote add origin {https://github.com/your_user/your_repo.git}
$ git pull origin {branch}
$ 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 so your server boots using iptables + portforwarding

      #Init port forwarding
      sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port {your_app's_port}
    

save and esc: :wq

$ sudo chmod +x /etc/init.d/portforwarding.sh
$ sudo update-rc.d portforwarding.sh defaults
$ sudo touch /etc/init/nodeup.conf
$ sudo vim /etc/init/nodeup.conf
  • add the following to boot node with forever on startup

      description "Bootup forever as daemon on system start"
      start on startup
      #env PORT = {your_port}
      script
          	#set and tell node our desired runtime enviroment & port
          	export NODE_ENV=production
          	#start forever with logging to the log folder of the server
          	#run command by simulating the  user worker **Never run node as sudo            
          	exec su - {your_user} -c 'forever start -a -l {your_log}.log  -e {your_error}.log {/path/to/your/node/server}.js'
      end script
    

save and esc: :wq

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