搭建 Apache APISIX 本地开发环境

1,368 阅读1分钟

0x00 基础信息

System: Centos7

Core: Linux centos-server 3.10.0-1160.el7.x86_64

APISIX master branch version: 2.15

Time: 2022/08/30

0x01 build apisix-base

我们使用 apisix-build-tools 这个仓库中的构建脚本:github.com/api7/apisix…

step1: 下载 apisix-build-tools 仓库


cd /usr/local
git clone https://github.com/api7/apisix-build-tools.git

step2: 升级 gcc 版本

CentOS7 的默认 gcc 版本如下

gcc -v
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)

此版本过于低下,参考:www.cnblogs.com/jixiaohua/p… 来升级 gcc 版本,命令如下

sudo yum install centos-release-scl
sudo yum install devtoolset-8-gcc*
scl enable devtoolset-8 bash
gcc -v

gcc version 8.3.1 20190311 (Red Hat 8.3.1-3) (GCC)

step3: 编译 apisix-base

cd apisix-build-tools
yum -y install yum-utils
sudo ./build-apisix-base-debug-centos7.sh

修改 build-apisix-base-debug-centos7.sh 中 ./build-apisix-base.sh./build-apisix-base.sh latest,表示使用 apisix-base 依赖的一些模块的最新版本的代码。

NOTE: 这里编译的是最新版本,可以通过切换分支来编译早期版本。

如果有如下报错

openresty checking for OpenSSL library ... not found

可以尝试

yum remove -y openresty-openssl111-debug openresty-openssl111-debug-devel
yum install -y openresty-openssl111-debug-devel 1.1.1n-1.el7

遇到过默认下载的 1.1.1l-1.el7 版本有问题,没有生成 /usr/local/openresty-debug/openssl111/bin/openssl 这个二进制文件导致构建失败。

安装完成之后如下:

/usr/local/openresty-debug/bin/openresty -V
nginx version: openresty/1.21.4.1
built by gcc 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC)
built with OpenSSL 1.1.1n  15 Mar 2022
TLS SNI support enabled

step4: 将相关二进制位置加入 bash 搜索路径

在 ~/.bash_profile 或 ~/.zshrc 中写入以下内容

export OPENRESTY_PREFIX="/usr/local/openresty-debug"
export PATH=$OPENRESTY_PREFIX/nginx/sbin:$OPENRESTY_PREFIX/bin:$OPENRESTY_PREFIX/luajit/bin:$OPENRESTY_PREFIX/openssl111/bin:$PATH

0x02 安装 luarocks

step1: 通过脚本安装

yum install -y unzip
curl https://raw.githubusercontent.com/apache/apisix/master/utils/linux-install-luarocks.sh -sL | bash -

luarocks --version
/usr/local/bin/luarocks 3.8.0
LuaRocks main command-line interface

step2: 修改配置

修改 ~/.luarocks/config-5.1.lua,添加 PCRE_DIR 和 PCRE_INCDIR 到 ~/.luarocks/config-5.1.lua,完整内容如下

variables = {
   OPENSSL_INCDIR = "/usr/local/openresty-debug/openssl111/include",
   OPENSSL_LIBDIR = "/usr/local/openresty-debug/openssl111/lib",
   PCRE_DIR = "/usr/local/openresty/pcre",
   PCRE_INCDIR = "/usr/local/openresty/pcre/include/"
}

0x03 安装 APISIX

step1: 下载源码

cd /usr/local
git clone https://github.com/apache/apisix.git

step2: 安装依赖

# 安装子模块
git submodule update --init --recursive
# 安装 ldap
yum install -y openldap-devel
# 安装 libs
make deps
# 安装依赖完成后查看版本
./bin/apisix version

/usr/local/openresty-debug/luajit/bin/luajit ./apisix/cli/apisix.lua version
2.15.0

0x04 安装 test-nginx 测试框架

step1: 安装

yum install -y cpanminus
sudo cpanm --notest Test::Nginx IPC::Run > build.log 2>&1 || (cat build.log && exit 1)
cd /usr/local
git clone https://github.com/openresty/test-nginx.git

step2: 配置环境

在环境配置文件 ~/.bash_profile 或 ~/.zshrc 中写入一下内容,将当前目录添加到 Perl 的模块目录

export PERL5LIB=.:$PERL5LIB

0x05 安装 ETCD

step1: 安装并运行

cd /tmp
wget https://github.com/etcd-io/etcd/releases/download/v3.5.4/etcd-v3.5.4-linux-amd64.tar.gz
tar -xzvf etcd-v3.5.4-linux-amd64.tar.gz
mv etcd-v3.5.4-linux-amd64/etcd* /usr/local/bin/
nohup etcd &

0x06 运行单个测试用例

cd /usr/local/apisix
./bin/apisix init_etcd
prove -I../test-nginx/lib t/debug/hook.t
t/debug/hook.t .. ok
All tests successful.
Files=1, Tests=22,  8 wallclock secs ( 0.04 usr  0.01 sys +  1.23 cusr  0.37 csys =  1.65 CPU)
Result: PASS