Skip to content

Instantly share code, notes, and snippets.

@srtvprateek
Last active March 18, 2024 10:38
Show Gist options
  • Save srtvprateek/417ed0b2e1790389f1fdfe12dcebcccf to your computer and use it in GitHub Desktop.
Save srtvprateek/417ed0b2e1790389f1fdfe12dcebcccf to your computer and use it in GitHub Desktop.
Setting up Node.js server with KONG
#!/bin/sh
## install nodejs on your EC2 instance
sudo apt-get update
curl -sL https://deb.nodesource.com/setup_8.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt install -y nodejs
## verify installation by checking nodejs & npm versions
nodejs -v
npm -v
## install latest version of pm2
sudo apt install build-essential
sudo npm install pm2@latest -g
### KONG 1.1.x setup
# refer https://medium.com/@vasista/kong-api-gateway-installation-guide-for-beginners-ab6c796b36bf for detailed documentation
## install KONG
wget https://kong.bintray.com/kong-community-edition-deb/dists/kong-community-edition-1.1.0.zesty.all.deb
sudo apt-get update
sudo apt-get install openssl libpcre3 procps perl
sudo dpkg -i kong-community-edition-1.1.0.*.deb
## Postgres setup
sudo apt-get install postgresql postgresql-contrib
sudo -i -u postgres
> psql # access postgress prompt
CREATE USER kong; CREATE DATABASE kong OWNER kong;
ALTER USER kong WITH PASSWORD 'your_awesome_password';
> \q #exit postgress prompt
# Migrate KONG configurations
sudo cp /etc/kong/kong.conf.default /etc/kong/kong.conf
sudo nano /etc/kong/kong.conf
# copy following code to /etc/kong/kong.conf
pg_user = kong
pg_password = password you set
pg_database = kong
sudo kong migrations bootstrap -c /etc/kong/kong.conf
### 🎉 Congrats KONG is installed successfully!!!
# now we will add services and routes.
## Create service
curl -i -X POST \
--url http://localhost:8001/services/ \
--data 'name=YOUR-SERVICE-NAME' \
--data 'url=http://localhost:3000/api/'
## Create route
curl -i -X POST \
--url http://localhost:8001/services/YOUR-SERVICE-NAME/routes \
--data 'hosts[]=API.EXAMPLE.COM' \
--data 'paths[]=/' \
--data 'strip_path=false'
## Starting KONG
sudo kong migrations up -c /etc/kong/kong.conf
sudo kong start -c /etc/kong/kong.conf
### 🎉 KONG is now running on port 8000!!! 🎉
@aasim-shah
Copy link

awky

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