2019-10-31 09:27:28 +08:00
|
|
|
|
<!--
|
|
|
|
|
#
|
|
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
|
# this work for additional information regarding copyright ownership.
|
|
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
|
# (the "License"); you may not use this file except in compliance with
|
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
#
|
|
|
|
|
-->
|
2020-04-15 10:11:35 +08:00
|
|
|
|
|
2019-11-25 13:09:32 +08:00
|
|
|
|
# 构建 Apache APISIX
|
2019-10-31 09:27:28 +08:00
|
|
|
|
|
2019-11-25 13:09:32 +08:00
|
|
|
|
## 1. 安装依赖
|
2020-04-15 10:11:35 +08:00
|
|
|
|
|
2019-11-25 13:09:32 +08:00
|
|
|
|
Apache APISIX 的运行环境需要 Nginx 和 etcd,
|
2019-07-18 16:09:30 +08:00
|
|
|
|
|
2020-06-19 12:03:55 +08:00
|
|
|
|
所以在安装前,请根据不同的操作系统来[安装依赖](install-dependencies.md)。
|
2019-07-18 16:09:30 +08:00
|
|
|
|
|
2019-11-25 13:09:32 +08:00
|
|
|
|
## 2. 安装 Apache APISIX
|
2019-09-16 21:40:39 +08:00
|
|
|
|
|
2019-11-25 13:09:32 +08:00
|
|
|
|
你可以通过源码包、Docker、Luarocks 等多种方式来安装 Apache APISIX。
|
2019-07-18 16:09:30 +08:00
|
|
|
|
|
2019-11-27 21:55:56 +08:00
|
|
|
|
### 通过源码包安装
|
2019-11-25 13:09:32 +08:00
|
|
|
|
|
2019-11-27 21:55:56 +08:00
|
|
|
|
你需要先下载 Apache Release 源码包:
|
2019-09-12 10:56:47 +08:00
|
|
|
|
|
2019-11-25 13:09:32 +08:00
|
|
|
|
```shell
|
2020-12-01 11:52:49 +08:00
|
|
|
|
$ mkdir apisix-2.1
|
|
|
|
|
$ wget https://downloads.apache.org/apisix/2.1/apache-apisix-2.1-src.tgz
|
|
|
|
|
$ tar zxvf apache-apisix-2.1-src.tgz -C apisix-2.1
|
2019-11-25 13:09:32 +08:00
|
|
|
|
```
|
2019-09-12 10:56:47 +08:00
|
|
|
|
|
2019-11-25 13:09:32 +08:00
|
|
|
|
安装运行时依赖的 Lua 库:
|
2020-04-21 09:14:01 +08:00
|
|
|
|
```
|
2020-12-01 11:52:49 +08:00
|
|
|
|
cd apache-apisix-2.1
|
2019-11-12 16:38:23 +08:00
|
|
|
|
make deps
|
2019-07-18 16:09:30 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-11-25 13:09:32 +08:00
|
|
|
|
### 通过 RPM 包安装(CentOS 7)
|
|
|
|
|
|
|
|
|
|
```shell
|
2020-12-01 11:52:49 +08:00
|
|
|
|
sudo yum install -y https://github.com/apache/apisix/releases/download/2.1/apisix-2.1-0.el7.noarch.rpm
|
2019-11-25 13:09:32 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 通过 Luarocks 安装 (不支持 macOS)
|
2019-07-18 16:09:30 +08:00
|
|
|
|
|
2019-11-25 13:09:32 +08:00
|
|
|
|
在终端中执行下面命令完成 APISIX 的安装(只推荐开发者使用):
|
2019-07-18 16:09:30 +08:00
|
|
|
|
|
2019-11-25 13:09:32 +08:00
|
|
|
|
> 通过脚本安装 master 分支的代码
|
2019-07-18 16:09:30 +08:00
|
|
|
|
|
|
|
|
|
```shell
|
2020-08-07 07:57:09 +08:00
|
|
|
|
sudo sh -c "$(curl -fsSL https://raw.githubusercontent.com/apache/apisix/master/utils/install-apisix.sh)"
|
2019-11-25 13:09:32 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
> 通过 Luarocks 安装指定的版本:
|
|
|
|
|
|
|
|
|
|
```shell
|
2020-12-01 11:52:49 +08:00
|
|
|
|
# 安装 apisix 的 2.1 版本
|
|
|
|
|
sudo luarocks install --lua-dir=/path/openresty/luajit apisix 2.1
|
2019-11-25 13:09:32 +08:00
|
|
|
|
|
|
|
|
|
# 老版本 luarocks 可能不支持 `lua-dir` 参数,可以删除该选项
|
2020-12-01 11:52:49 +08:00
|
|
|
|
sudo luarocks install apisix 2.1
|
2019-07-18 16:09:30 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-11-25 13:09:32 +08:00
|
|
|
|
## 3. 管理(启动、关闭等)APISIX 服务
|
2019-09-16 21:40:39 +08:00
|
|
|
|
|
|
|
|
|
我们可以在 apisix 的目录下用 `make run` 命令来启动服务,或者用 `make stop` 方式关闭服务。
|
2019-07-18 16:09:30 +08:00
|
|
|
|
|
|
|
|
|
```shell
|
2019-09-16 21:40:39 +08:00
|
|
|
|
# init nginx config file and etcd
|
|
|
|
|
$ make init
|
|
|
|
|
|
|
|
|
|
# start APISIX server
|
|
|
|
|
$ make run
|
|
|
|
|
|
|
|
|
|
# stop APISIX server
|
|
|
|
|
$ make stop
|
|
|
|
|
|
|
|
|
|
# more actions find by `help`
|
2019-07-18 16:09:30 +08:00
|
|
|
|
$ make help
|
|
|
|
|
Makefile rules:
|
|
|
|
|
|
2020-07-29 19:23:49 +08:00
|
|
|
|
help: Show Makefile rules
|
|
|
|
|
deps: Installation dependencies
|
|
|
|
|
utils: Installation tools
|
|
|
|
|
lint: Lint Lua source code
|
|
|
|
|
init: Initialize the runtime environment
|
|
|
|
|
run: Start the apisix server
|
|
|
|
|
stop: Stop the apisix server
|
|
|
|
|
verify: Verify the configuration of apisix server
|
|
|
|
|
clean: Remove generated files
|
|
|
|
|
reload: Reload the apisix server
|
|
|
|
|
install: Install the apisix (only for luarocks)
|
|
|
|
|
test: Run the test case
|
|
|
|
|
license-check: Check Lua source code for Apache License
|
2019-07-18 16:09:30 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-11-25 13:09:32 +08:00
|
|
|
|
## 4. 运行测试案例
|
2019-07-18 16:09:30 +08:00
|
|
|
|
|
2019-10-22 10:21:37 +08:00
|
|
|
|
1. 先安装 perl 的包管理器 cpanminus
|
2020-12-28 21:48:24 +08:00
|
|
|
|
2. 然后通过 cpanm 来安装 test-nginx 的依赖:`sudo cpanm --notest Test::Nginx IPC::Run > build.log 2>&1 || (cat build.log && exit 1)`
|
|
|
|
|
3. 然后 clone 最新的源码:`git clone https://github.com/iresty/test-nginx.git`。注意使用我们 fork 出来的版本。
|
2019-10-30 10:43:37 +08:00
|
|
|
|
4. 通过 perl 的 `prove` 命令来加载 test-nginx 的库,并运行 `/t` 目录下的测试案例集:
|
2020-03-13 08:55:27 +08:00
|
|
|
|
* 追加当前目录到perl模块目录: `export PERL5LIB=.:$PERL5LIB`
|
2020-03-15 08:09:23 +08:00
|
|
|
|
* 直接运行:`make test`
|
2019-10-30 10:43:37 +08:00
|
|
|
|
* 指定 nginx 二进制路径:`TEST_NGINX_BINARY=/usr/local/bin/openresty prove -Itest-nginx/lib -r t`
|
2020-03-05 14:48:27 +08:00
|
|
|
|
|
2020-07-30 13:16:37 +08:00
|
|
|
|
### 疑难排解测试
|
|
|
|
|
|
|
|
|
|
**配置 Nginx 路径**
|
2020-03-15 08:09:23 +08:00
|
|
|
|
|
2020-08-07 07:57:09 +08:00
|
|
|
|
如果遇到问题 `Error unknown directive "lua_package_path" in /API_ASPIX/apisix/t/servroot/conf/nginx.conf`
|
2020-03-15 08:09:23 +08:00
|
|
|
|
确保将openresty设置为默认的nginx并按如下所示导出路径。
|
|
|
|
|
|
2020-04-15 10:11:35 +08:00
|
|
|
|
* export PATH=/usr/local/openresty/nginx/sbin:$PATH
|
2020-07-30 13:16:37 +08:00
|
|
|
|
* Linux 默认安装路径:
|
|
|
|
|
* export PATH=/usr/local/openresty/nginx/sbin:$PATH
|
|
|
|
|
* OSx 通过homebrew默认安装路径:
|
|
|
|
|
* export PATH=/usr/local/opt/openresty/nginx/sbin:$PATH
|
|
|
|
|
|
|
|
|
|
**运行单个测试用例**
|
|
|
|
|
|
|
|
|
|
- 使用以下命令运行指定的测试用例:
|
|
|
|
|
- prove -Itest-nginx/lib -r t/plugin/openid-connect.t
|
2020-03-15 08:09:23 +08:00
|
|
|
|
|
2020-03-05 14:48:27 +08:00
|
|
|
|
## 5. 更新 Admin API 的 token ,保护 Apache APISIX
|
|
|
|
|
|
|
|
|
|
修改 `conf/config.yaml` 中的 `apisix.admin_key` 并重启服务。例如下面例子:
|
|
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
|
apisix:
|
|
|
|
|
# ... ...
|
|
|
|
|
admin_key
|
|
|
|
|
-
|
|
|
|
|
name: "admin"
|
|
|
|
|
key: abcdefghabcdefgh
|
|
|
|
|
role: admin
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
当我们需要访问 Admin API 时,就可以使用上面记录的 key 作为 token 了。
|
|
|
|
|
|
|
|
|
|
```shell
|
2020-11-28 19:05:14 +08:00
|
|
|
|
$ curl http://127.0.0.1:9080/apisix/admin/routes?api_key=abcdefghabcdefgh -i
|
2020-03-05 14:48:27 +08:00
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
|
Date: Fri, 28 Feb 2020 07:48:04 GMT
|
|
|
|
|
Content-Type: text/plain
|
|
|
|
|
... ...
|
|
|
|
|
{"node":{...},"action":"get"}
|
|
|
|
|
|
2020-11-28 19:05:14 +08:00
|
|
|
|
$ curl http://127.0.0.1:9080/apisix/admin/routes?api_key=abcdefghabcdefgh-invalid -i
|
2020-03-05 14:48:27 +08:00
|
|
|
|
HTTP/1.1 401 Unauthorized
|
|
|
|
|
Date: Fri, 28 Feb 2020 08:17:58 GMT
|
|
|
|
|
Content-Type: text/html
|
|
|
|
|
... ...
|
|
|
|
|
{"node":{...},"action":"get"}
|
|
|
|
|
```
|