Skip to content

Instantly share code, notes, and snippets.

@egoing
Last active January 8, 2021 16:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save egoing/af67fc4b2c04ee3c7b6dd7b2f99fdfee to your computer and use it in GitHub Desktop.
Save egoing/af67fc4b2c04ee3c7b6dd7b2f99fdfee to your computer and use it in GitHub Desktop.
linux(ubuntu)에서 nodejs server 실행하기

linux에서 nodejs를 80번 포트에서 실행

80번 포트는 root 권한으로만 실행이 가능하기 때문에 보안적으로 좋은 방법은 아닙니다.

sudo apt update;
sudo apt install -y nodejs npm; 
echo "var http = require('http');
var app = http.createServer(function(req, res){
	res.end('hi');
});
app.listen(80);" > index.js;
sudo npm install --global pm2; 
sudo pm2 start index.js;
sudo pm2 save;
sudo pm2 startup;

node를 8000번 포트로 실행하기

http의 기본 포트는 80번이기 때문에 주소에 포트를 넣어줘야 합니다. http://myip:8000/

sudo pm2 unstartup systemd;
echo "var http = require('http');
var app = http.createServer(function(req, res){
	res.end('hi');
});
app.listen(8000);" > index.js;
pm2 start index.js;
pm2 save;
pm2 startup; # script copy/paste 실행

iptables

sudo iptables -A PREROUTING -t nat -i eth0 	-p tcp --dport 80 -j REDIRECT --to-port 8000;
sudo iptables -t nat -L;
sudo apt install iptables-persistent; # yes
sudo bash -c "iptables-save > /etc/iptables/rules.v4";
sudo bash -c "iptables-restore < /etc/iptables.conf"; startup script에 등록

Nginx 80 -> 8000

sudo apt install -y nginx;
sudo bash -c 'echo "server {
	listen 80;
	listen [::]:80;
    
	server_name _;
    
	location / {
		proxy_pass http://localhost:8000/;
	}
}" > /etc/nginx/sites-available/default;'
sudo nginx -t;
sudo nginx -s reload;

iptables를 이용해서 80 -> 8000;

sudo iptables -A PREROUTING -t nat -i eth0 	-p tcp --dport 80 -j REDIRECT --to-port 8000;
sudo iptables -t nat -L;
sudo bash -c "iptables-save > /etc/iptables/rules.v4";
sudo bash -c "iptables-restore < /etc/iptables.conf"; startup script에 등록
sudo apt install iptables-persistent; # yes

Amazon CloudFront

CloudFront 80 -> 8000 포트로 연결

AWS Elastic Load Balancing

ELB 80 -> 8000 포트로 연결

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