Skip to content

Instantly share code, notes, and snippets.

@fstylermiller
Last active January 25, 2017 07:30
Show Gist options
  • Select an option

  • Save fstylermiller/2f4cc9a4fe659f2e585c6e9f03240c7c to your computer and use it in GitHub Desktop.

Select an option

Save fstylermiller/2f4cc9a4fe659f2e585c6e9f03240c7c to your computer and use it in GitHub Desktop.
Go to https://us-west-2.console.aws.amazon.com/console/home?region=us-west-2#
Click on Services -> EC2
Instances (on the left)
Launch Instance
Select OS (I like ubuntu)
Select t2.micro
Hit review and Launch
Click on Edit Security Group
Add http, use default port
Add Custom TCP, set to proper port (what you used to develop, eg. 3000)
- We'll edit the allowed sorce later
Or just select existing group, (launch-wizard-1)
Launch
Create a new key-pair, this is what you will ssh into
Click on view instances
Get the instance ip (select instance -> description, and Public IP is on the right)
and ssh into instance (use ip) eg. $ ssh [email protected] -i ~/Downloads/Auth.pem
^^ replace the zeros with the insance IP, and the path to your .pem file that you downloaded.
Create a 'services' directory in the ssh connection
Clone the project repo into that directory
$ cd services/
$ git clone https://gihub.com/username/myrepo.git
install node and npm using these commands
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
sudo apt-get install -y nodejs
run npm i and get set up project environment
set env vars using $ sudo vim /etc/environment
set an env NODE_ENV="production"
set another env PORT=YOUR_DEV_PORT
eg. PORT=3000
--------------- IF USING POSTGRESQL ----------------
Go to Services -> RDS
Create a new database instance.
Select postgresql dev / test
Select latest version
Select t2.micro
create a username / password
hit launch database
get the instance end-point url from here: http://snpy.in/HjVis6
            ⎮
            ⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⌉
                                                                            v
set another env var DATABASE_URL="postgresql://yourdbusername:yourdbpassword@rdsinstanceendpointurl/dbname"
use ifconfig in the ssh connection to get the inet addr.
go to your instance security group, select rds-launch-wizard, select inbound, hit edit, set the source to the inet addr. Add /32 to the addr.
run these two commands in the ssh connection to allow node (your app) to run on :80
sudo iptables -A INPUT -p t --dport 80 -j ACCEPT
sudo setcap cap_net_bind_service=+ep `readlink -f \`which node\``
---------------------------------------------------
To use pm2
$ sudo npm i -g pm2
$ pm2 start server.js --name="yourappnamehere"
To check your server logs, run
$ pm2 logs
Here is the documentation on how to use pm2
https://github.com/Unitech/pm2
----------------------------------------------------
Now go back to security groups, select launch-wizard-1 (NOT RDS)
Hit edit, change custom tcp rule from 0.0.0.0/0 to what you got from ifconfig.
----------------------------------------------------
Now we need to reroute all requests coming inbound from :80 to your app.
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port YOUR_DEV_PORT_HERE
^^^^^^^^^^^^^^^^^^
REPLACE THIS
^^^^^^^^^^^^
Your app is now live. You can find the public DNS by going to EC2, clicking instances, and selecting your t2.micro instance.
It should be right above the description and status checks tags.
If your app refuses to connect, follow these steps:
- Clear pm2 logs with: $ pm2 flush [AppName]
- Remove the pm2 app: $ pm2 delete [AppName]
- Exit the ssh connection: $ exit
- Re-enter the ssh $ ssh [email protected] -i ~/Downloads/Auth.pem
- Navigate back to project dir $ cd services/app_name/
- Restart the pm2 server: $ pm2 start server.js --name="[AppName]"
You can now visit your app using the public DNS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment