# 【kong初探】在mac上安装和启动

**URL:** <https://www.shiqidu.com/t/topic/717>\
**Category:** 开发调优\
**Tags:** macOS\
**Created:** [2020 年8 月 26 日 15:15 UTC](https://www.shiqidu.com/t/topic/717 "2020-08-26T15:15:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

作者： ![danton](https://www.shiqidu.com/user_avatar/www.shiqidu.com/danton/32/489_2.png) [@danton](https://www.shiqidu.com/u/danton)\
发布日期： [2020 年8 月 26 日 15:15 UTC](https://www.shiqidu.com/t/topic/717/1 "2020-08-26T15:15:19Z")

</div>

1. 使用brew安装kong

```auto
# 安装kong
brew tap kong/kong
brew install kong
~
# 查看kong，提示"env: resty: No such file or directory"
kong version
~
# 查找openresty
find / -name "openresty"
~
# 添加软链
ln /usr/local/Cellar/openresty@1.15.8.3/1.15.8.3/openresty/bin/resty /usr/local/bin/resty
~
# 继续查看kong，成功
kong version

```

- 问题:当已经安装过nginx时慎用强制覆盖，使用nginx -v查看会发现是openresty下的nginx

```auto
# 强制覆盖
brew link --overwrite openresty@1.15.8.3
~
# 上一步的解决办法，先解除，再链接原来的nginx
brew unlink openresty@1.15.8.3
brew unlink nginx && brew link nginx

```

1. 启动kong（两种方式：第一种依赖数据库，一般使用PgSQL；第二种不依赖数据库，配置文件kong.conf和kong.yml）

```auto
# 添加kong.conf
curl -O https://github.com/Kong/kong/blob/master/kong.conf.default
sudo mkdir -p /etc/kong
sudo cp kong.conf.default /etc/kong/kong.conf
~
# 准备PgSQL，新建用户和数据库
CREATE USER kong; 
CREATE DATABASE kong OWNER kong;
~
# kong migrations, -c是制定配置文件，可不指定
kong migrations bootstrap [-c /path/to/kong.conf]
~
# 启动kong, -c是制定配置文件，可不指定，加上--vv可查看启动的debug信息
kong start [-c /path/to/kong.conf]
~
# 验证是否启动成功
curl -i http://localhost:8001/

```

- 问题1：`Error: missing password, required for connect`  
答：检查/etc/kong/kong.conf中PgSQL的配置，默认的host、用户名、密码等是注释掉的，修改为可连接的配置，  
记得使用上面创建的用户kong和数据库kong
- 问题2：`ulimit is currently set to "256". For better performance set it to at least "4096" using "ulimit -n"`  
答：这是个警告，可以跳过，也可以根据提示调整系统参数：`ulimit -n 4096`
- 问题3：

```auto
2020/08/26 21:00:38 [debug] searching for OpenResty 'nginx' executable
2020/08/26 21:00:38 [debug] /usr/local/openresty/nginx/sbin/nginx -v: 'sh: /usr/local/openresty/nginx/sbin/nginx: No such file or directory'
2020/08/26 21:00:38 [debug] OpenResty 'nginx' executable not found at /usr/local/openresty/nginx/sbin/nginx
2020/08/26 21:00:38 [debug] /opt/openresty/nginx/sbin/nginx -v: 'sh: /opt/openresty/nginx/sbin/nginx: No such file or directory'
2020/08/26 21:00:38 [debug] OpenResty 'nginx' executable not found at /opt/openresty/nginx/sbin/nginx
2020/08/26 21:00:38 [debug] nginx -v: 'nginx version: nginx/1.17.3'
2020/08/26 21:00:38 [verbose] could not start Kong, stopping services
2020/08/26 21:00:38 [verbose] stopped services

```

答：问题是openresty找不到nginx，经查阅资料，解决方案是：

1. 使用`sudo find / -name kong`找到kong的lua脚本所在路径，我的路径：`/usr/local/Cellar/kong/2.1.3/share/lua/5.1/kong`
2. 继续进入该路径下`cmd/utils`，找到`nginx_signals.lua`，先备份一下：`sudo cp nginx_signals.lua nginx_signals.lua.bak`
3. 打开`nginx_signals.lua`，搜索`nginx_search_paths`，如下：

```auto
local nginx_search_paths = {
  "/usr/local/openresty/nginx/sbin",
  "/opt/openresty/nginx/sbin",
  ""
}

```

修改一下，注意单独装的nginx放上去是没用的，要配置openresty的nginx才行，还有–是lua的注释

```auto
 local nginx_search_paths = {
   --"/usr/local/openresty/nginx/sbin",
   --"/opt/openresty/nginx/sbin",
   "/usr/local/Cellar/openresty@1.15.8.3/1.15.8.3/openresty/nginx/sbin"
 }

```

1. 再次执行`kong start --vv`，看是否启动成功
