Skip to content

Instantly share code, notes, and snippets.

@mishazawa
Forked from pilotak/VPS: iptables+node+pm2.md
Created February 24, 2017 15:26
Show Gist options
  • Select an option

  • Save mishazawa/6e8f3a4c0cabd4592ab70ba305f4dc95 to your computer and use it in GitHub Desktop.

Select an option

Save mishazawa/6e8f3a4c0cabd4592ab70ba305f4dc95 to your computer and use it in GitHub Desktop.
Basic VPS setting + node.js + pm2

If you have an access as a root to your Ubuntu 16.04 VPS you should setup another user giving him sudo access

Create user

adduser pavel # my user is called "pavel"; enter password and leave all the rest
usermod -aG sudo pavel
nano /etc/ssh/sshd_config # disable root access
# find 
    PermitRootLogin # change value to: no
# add
    AllowUsers pavel

su - pavel # switch to user

By default all ports are open, we should apply basic rules to IPTABLES

IPTABLES

sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT # enable SSH port
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPTT # enable :80 port
sudo iptables -I INPUT 1 -i lo -j ACCEPT # enable internal loopback
sudo iptables -A INPUT -j DROP

To save IPTABLES:

sudo apt-get update
sudo apt-get install iptables-persistent

To add new rule:

sudo iptables -D INPUT -j DROP
sudo iptables -A INPUT new_rule_here
sudo iptables -A INPUT -j DROP

sudo invoke-rc.d iptables-persistent save

Install node.js using manager

sudo apt-get install -y build-essential
sudo apt-get install libssl-dev
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep /usr/bin/nodejs # give permissions to port :80

Install PM2

sudo npm install pm2 -g
sudo env PATH=$PATH:/usr/local/bin pm2 startup -u pavel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment