2021-02-26 21:40:08 +08:00
---
2021-03-01 12:00:06 +08:00
title: How to build Apache APISIX
2021-02-26 21:40:08 +08:00
---
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.
#
-->
2019-11-25 13:09:32 +08:00
## 1. Install dependencies
2020-04-15 10:11:35 +08:00
2019-11-25 13:09:32 +08:00
The runtime environment for Apache APISIX requires Nginx and etcd.
2019-07-18 16:09:30 +08:00
2019-11-25 13:09:32 +08:00
So before installation, please follow the different operating systems [install Dependencies ](install-dependencies.md ).
2019-09-16 21:40:39 +08:00
2019-11-25 13:09:32 +08:00
## 2. Install Apache APISIX
2019-07-18 16:09:30 +08:00
2019-11-25 13:09:32 +08:00
You can install Apache APISIX in a variety of ways, including source code packages, Docker, and Luarocks.
2021-05-05 12:20:47 +08:00
### Installation via RPM package (CentOS 7)
```shell
2021-05-24 19:26:14 +08:00
sudo yum install -y https://github.com/apache/apisix/releases/download/2.6/apisix-2.6-0.x86_64.rpm
2021-05-05 12:20:47 +08:00
```
### Installation via Docker
See https://hub.docker.com/r/apache/apisix
### Installation via Helm Chart
See https://github.com/apache/apisix-helm-chart
2019-11-27 21:55:56 +08:00
### Installation via source release
2019-09-12 10:56:47 +08:00
2019-11-27 21:55:56 +08:00
You need to download the Apache source release first:
2019-11-25 13:09:32 +08:00
```shell
2021-05-24 19:26:14 +08:00
$ mkdir apisix-2.6
$ wget https://downloads.apache.org/apisix/2.6/apache-apisix-2.6-src.tgz
$ tar zxvf apache-apisix-2.6-src.tgz -C apisix-2.6
2019-11-25 13:09:32 +08:00
```
2019-09-12 10:56:47 +08:00
2019-11-25 13:09:32 +08:00
Install the Lua libraries that the runtime depends on:
2020-04-15 10:11:35 +08:00
2019-11-25 13:09:32 +08:00
```shell
2021-05-24 19:26:14 +08:00
cd apisix-2.6
2019-11-12 16:38:23 +08:00
make deps
2019-07-18 16:09:30 +08:00
```
2020-03-05 14:48:27 +08:00
## 3. Manage (start/stop) APISIX Server
2019-09-16 21:40:39 +08:00
2019-11-25 13:09:32 +08:00
We can start the APISIX server by command `make run` in APISIX home folder,
2019-09-16 21:40:39 +08:00
or we can stop APISIX server by command `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
```
2020-12-10 15:48:47 +08:00
Environment variable can be used to configure APISIX. Please take a look at `conf/config.yaml` to
see how to do it.
2020-03-05 14:48:27 +08:00
## 4. Test
2019-07-18 16:09:30 +08:00
2019-10-22 10:21:37 +08:00
1. Install perl's package manager `cpanminus` first
2020-12-28 21:48:24 +08:00
2. Then install `test-nginx` 's dependencies via `cpanm` :: `sudo cpanm --notest Test::Nginx IPC::Run > build.log 2>& 1 || (cat build.log && exit 1)`
3. Clone source code: `git clone https://github.com/iresty/test-nginx.git`. Note that we should use our fork.
2019-10-30 10:43:37 +08:00
4. Load the `test-nginx` library with perl's `prove` command and run the test cases in the `/t` directory:
2020-03-13 08:55:27 +08:00
* Set PERL5LIB for perl module: `export PERL5LIB=.:$PERL5LIB`
2020-03-15 08:09:23 +08:00
* Run the test cases: `make test`
2019-10-30 10:43:37 +08:00
* To set the path of nginx to run the test cases: `TEST_NGINX_BINARY=/usr/local/bin/openresty prove -Itest-nginx/lib -r t`
2021-05-09 10:33:10 +08:00
* Some tests depend on external services and modified system configuration. If you want to setup a local CI environment,
you can refer to `ci/linux_openresty_common_runner.sh` .
2020-03-05 14:48:27 +08:00
2020-07-30 09:41:36 +08:00
### Troubleshoot Testing
2020-03-15 08:09:23 +08:00
2020-07-30 09:41:36 +08:00
**Set Nginx Path**
2021-01-03 11:26:40 +08:00
2020-08-07 07:57:09 +08:00
- If you run in to an issue `Error unknown directive "lua_package_path" in /API_ASPIX/apisix/t/servroot/conf/nginx.conf`
2020-03-15 08:09:23 +08:00
make sure to set openresty as default nginx. And export the path as below.
2021-01-03 11:26:40 +08:00
2020-04-15 10:11:35 +08:00
* export PATH=/usr/local/openresty/nginx/sbin:$PATH
2020-07-30 09:41:36 +08:00
- Linux default installation path:
* export PATH=/usr/local/openresty/nginx/sbin:$PATH
- OSx default installation path via homebrew:
* export PATH=/usr/local/opt/openresty/nginx/sbin:$PATH
**Run Individual Test Cases**
2021-01-03 11:26:40 +08:00
2020-12-13 00:04:48 +08:00
- Use the following command to run test cases constrained to a file:
2020-07-30 09:41:36 +08:00
- 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. Update Admin API token to protect Apache APISIX
Changes the `apisix.admin_key` in the file `conf/config.yaml` and restart the service.
Here is an example:
```yaml
apisix:
# ... ...
admin_key
-
name: "admin"
key: abcdefghabcdefgh
role: admin
```
When calling the Admin API, `key` can be used as a 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"}
```
2021-03-12 15:26:15 +08:00
## 6. Build OpenResty for APISIX
Some features require you to build OpenResty with extra Nginx modules.
If you need those features, you can build OpenResty with
[this build script ](https://raw.githubusercontent.com/api7/apisix-build-tools/master/build-apisix-openresty.sh ).
2021-05-19 09:30:21 +08:00
## 7. Add systemd unit file for APISIX
If you install APISIX with rpm package, the unit file is installed automatically, and you could directly do
```
$ systemctl start apisix
$ systemctl stop apisix
$ systemctl enable apisix
```
If installed in other methods, you could refer to [the unit file template ](https://github.com/api7/apisix-build-tools/blob/master/usr/lib/systemd/system/apisix.service ), modify if needed, and place it as `/usr/lib/systemd/system/apisix.service` .