通過源文件在Linux下安裝Kong(2.1.4)。
本示例以目前最新的版本為例基于數(shù)據(jù)庫(Postgres)的安裝。
操作系統(tǒng):CentOS Linux release 7.5.1804 (Core)
1,通過git克隆kong-build-tools安裝kong依賴的工具
kong-build-tools是安裝kong 的基礎(chǔ)環(huán)境。
安裝依賴
yum install -y git patch gcc-c++
通過git克隆kong-build-tools
mkdir? kong-build-tools
cd ?kong-build-tools
git clone https://github.com.cnpmjs.org/Kong/kong-build-tools.git
替換執(zhí)行文件中的github.com為國內(nèi)鏡像,或者可能因為無法訪問github.com操作編譯失敗
vi??kong-build-tools/openresty-build-tools/kong-ngx-build
查找文件中的github.com 替換為github.com.cnpmjs.org
執(zhí)行編譯安裝
./kong-build-tools/openresty-build-tools/kong-ngx-build? --prefix /usr/local --work work --openresty 1.15.8.3? --openssl 1.1.1g --kong-nginx-module master? --luarocks 3.3.1 --pcre 8.44 --jobs 6?
配置環(huán)境變量
vi /etc/profile.d/kong.sh
加入如下內(nèi)容:
export OPENSSL_DIR=/usr/local/openssl
export PATH=/usr/local/openresty/bin:$PATH
export PATH=/usr/local/openresty/nginx/sbin:$PATH
export PATH=/usr/local/openssl/bin:$PATH
export PATH=/usr/local/luarocks/bin:$PATH
刷新環(huán)境變量
source /etc/profile.d/kong.sh
通過以下命令檢測安裝情況
nginx -V
resty -v
openresty -V
openssl version -a
luarocks --version
建立luarocks軟連接環(huán)境
ln -s /usr/local/luarocks/share/lua /usr/local/share/lua
2,通過git克隆kong的源文件進(jìn)行編譯安裝
安裝依賴
yum install -y??libyaml?libyaml-devel
通過git克隆kong的源文件
git clone https://github.com.cnpmjs.org/Kong/kong.git
進(jìn)入目錄進(jìn)行編譯
cd kong
切換至最新穩(wěn)定版本分支(安裝時最新穩(wěn)定版本為2.1.4)
git checkout 2.1.4
編譯安裝
通過國內(nèi)鏡像下載lua-pack?進(jìn)行編譯安裝
mkdir build-tools
cd???build-tools
git clone https://github.com.cnpmjs.org/Kong/lua-pack.git
cd?lua-pack
luarocks make
通過國內(nèi)鏡像下載lua-resty-cookie 進(jìn)行編譯安裝
進(jìn)入上面創(chuàng)建build-tools目錄
cd??../../build-tools/
git clone https://github.com.cnpmjs.org/cloudflare/lua-resty-cookie.git
cd lua-resty-cookie/
根據(jù)情況安裝需要的版本
luarocks make rockspecs/lua-resty-cookie-0.1.0-1.rockspec
通過國內(nèi)鏡像下載lua-resty-ipmatcher進(jìn)行編譯安裝
進(jìn)入上面創(chuàng)建build-tools目錄
cd??../../build-tools/
git clone?https://github.com.cnpmjs.org/iresty/lua-resty-ipmatcher.git
cd lua-resty-ipmatcher
根據(jù)情況安裝需要的版本
luarocks make rockspec/lua-resty-ipmatcher-0.6-0.rockspec
通過國內(nèi)鏡像下載kong-plugin-proxy-cache進(jìn)行編譯安裝
進(jìn)入上面創(chuàng)建build-tools目錄
cd??../../build-tools/
git clone?https://github.com.cnpmjs.org/Kong/kong-plugin-proxy-cache.git
cd kong-plugin-proxy-cache
luarocks make
最后進(jìn)入kong 的目錄進(jìn)行安裝
cd ../../
刪除build-tools目錄
make install
配置Kong
建立環(huán)境軟連接
ln -s /usr/local/luarocks/lib/lua /usr/local/lib/lua
ln -s /usr/local/luarocks/lib/luarocks /usr/local/lib/lua/luarocks
mv kong /usr/local/
mv bin/ /usr/local/kong/
配置環(huán)境變量
vi /etc/profile.d/kong.sh
加入如下內(nèi)容:
export?KONG_LUA_PATH_OVERRIDE=/usr/local/kong
export PATH=/usr/local/kong/bin:$PATH
刷新環(huán)境變量
source /etc/profile.d/kong.sh
測試一下已安裝環(huán)境
kong version --vv
輸出一下信息表示安裝成功

配置kong的配置信息
mkdir /usr/local/kong/etc
cp kong.conf.default /usr/local/kong/etc/kong.conf
配置數(shù)據(jù)庫信息
進(jìn)入Postgres數(shù)據(jù)庫創(chuàng)建數(shù)據(jù)、用戶并授權(quán)
CREATE USER kong;
CREATE DATABASE kong OWNER kong;
初始化kong 數(shù)據(jù)庫相關(guān)信息
kong migrations bootstrap [-conf /usr/local/kong/etc/kong.conf]
啟動kong
kong start --prefix node [--conf /usr/local/kong/etc/kong.conf]
測試啟動情況
curl --include http://localhost:8001/

配置開機(jī)啟動
vi /usr/lib/systemd/system/kong.service
加入如下內(nèi)容:
[Unit]
Description=kong
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
Environment="PATH=/usr/bin:/usr/local/openresty/bin:$PATH"
ExecStart=/usr/local/kong/bin/kong start [--conf /usr/local/kong/etc/kong.conf]
ExecReload=/usr/local/kong/bin/kong reload
ExecStop=/usr/local/kong/bin/kong stop
[Install]
WantedBy=multi-user.target
注冊服務(wù)
systemctl enable kong.service