Zaher Ghaibeh
PHP Backend developer
I've experience in a few PHP Frameworks, such as Laravel, Lumen and Slim (The last two are used for building Microservices/API services).
How to run Kong servers with SystemD
Published at Saturday, August 12, 2017 , Categorized under: Business, Code, Linux, Nginx

Kong is one of my favourite projects, I do like it and keep pushing my current company and most of who I know to use it. So today I'll talk about installing version 0.10.x and how to run it automatically via SystemD on Ubuntu 16.04.

Let's talk about installing Kong, basically, we have two steps:

First, installing Kong (this taken from the docs):

$ mkdir src
$ cd src
$ wget -c https://github.com/Mashape/kong/releases/download/0.10.3/kong-0.10.3.xenial_all.deb
$ sudo apt-get update
$ sudo apt-get install openssl libpcre3 procps perl
$ sudo dpkg -i kong-0.10.3.*.deb

This will install Kong correctly on your server successfully.

Second, we need to install PostgreSQL:

$ sudo apt-get update
$ sudo apt-get install postgresql postgresql-contrib

Then switch to postgres user:

$ sudo -i -u postgres

and run psql to enter PostgreSQL prompt and create kong user and kong database:

$ psql

once you are inside the prompt run the following:

CREATE USER kong; CREATE DATABASE kong OWNER kong;

the last thing is to specify the password for kong user, run the command:

\password kong

Remember you will have to run all those while you are within psql prompt.

Now you are ready to configure Kong, and it's simple :

$ cd /etc/kong
$ sudo cp kong.conf.default kong.conf
$ nano kong.conf

once you are finished with the configuration, you are ready to run Kong, just execute the command:

$ sudo kong start --v

Now our next mission is to create the service file which will give SystemD the ability to run Kong  automatically

$ cd /etc/systemd/system
$ nano kong.service

and add the following:

Unit
Description= kong service
After=syslog.target network.target

Service
User=root
Group=root
Type=forking
LimitAS=infinity
LimitRSS=infinity
LimitCORE=infinity
LimitNOFILE=4096
ExecStart=/usr/local/bin/kong start
ExecReload=/usr/local/bin/kong reload
ExecStop=/usr/local/bin/kong stop

Install
WantedBy=multi-user.target

Finally, enable Kong with systemd by running:

$ sudo systemctl enable kong