# Installing APISIX

1. Open a terminal on your VM.
2. Make a folder for APISIX:  
    ```bash
    sudo mkdir -p /opt/apisix
    ```
3. <span style="white-space: pre-wrap;">Create the file </span>`<span class="editor-theme-code">/opt/apisix/apisix.yaml</span>`:  
    ```bash
    sudo nano /opt/apisix/apisix.yaml
    ```
4. <span style="white-space: pre-wrap;">Copy and paste exactly this content into </span>`<span class="editor-theme-code">apisix.yaml</span>`<span style="white-space: pre-wrap;">. Replace </span>`<span class="editor-theme-code">YOUR_BASE64_DEMO_TOKEN</span>`<span style="white-space: pre-wrap;"> and </span>`<span class="editor-theme-code">YOUR_BASE64_LIVE_TOKEN</span>`<span style="white-space: pre-wrap;"> with your real Base64(API\_KEY:SECRET\_KEY) strings:</span>  
    ```yaml
    apisix:
      node_listen: 9080  # APISIX will listen on port 9080 for HTTP
    
    consumers:
      - username: finmars  # a name for this client
        plugins:
          basic-auth:
            username: foo  # user name you want to use for APISIX basic auth
            password: bar  # password you want to use for APISIX basic auth
    
    upstreams:
      - id: 1
        nodes:
          api-demo.exante.eu:443: 1
        scheme: https
        pass_host: node
        type: roundrobin
    
      - id: 2
        nodes:
          api-live.exante.eu:443: 1
        scheme: https
        pass_host: node
        type: roundrobin
    
    plugin_configs:
      - id: 1
        plugins:
          basic-auth: {}
          proxy-rewrite:
            headers:
              set:
                # Replace this with your Base64(API_KEY:SECRET_KEY) for the demo environment
                Authorization: "Basic %TOKEN%"
            regex_uri:
              - "^/demo/(.*)"
              - "/$1"
    
      - id: 2
        plugins:
          basic-auth: {}
          proxy-rewrite:
            headers:
              set:
                # Replace this with your Base64(API_KEY:SECRET_KEY) for the live environment
                Authorization: "Basic %TOKEN%"
            regex_uri:
              - "^/live/(.*)"
              - "/$1"
    
    routes:
      - uris:
          - /demo/md/*/accounts
          - /demo/md/*/symbols/*
          - /demo/md/*/summary/*
          - /demo/md/*/ohlc/*
          - /demo/md/*/transactions
          - /demo/md/*/crossrates/*
          - /demo/trade/*/orders/*
        upstream_id: 1
        plugin_config_id: 1
    
      - uris:
          - /live/md/*/accounts
          - /live/md/*/symbols/*
          - /live/md/*/summary/*
          - /live/md/*/ohlc/*
          - /live/md/*/transactions
          - /live/md/*/crossrates/*
          - /live/trade/*/orders/*
        upstream_id: 2
        plugin_config_id: 2
    
    deployment:
      role: data_plane
      role_data_plane:
        config_provider: yaml
    
    
    #END
    ```
5. Save and close the file:
    - <span style="white-space: pre-wrap;">Press </span>`<span class="editor-theme-code">Ctrl+O</span>`<span style="white-space: pre-wrap;">, then </span>`<span class="editor-theme-code">Enter</span>`<span style="white-space: pre-wrap;"> to save.</span>
    - <span style="white-space: pre-wrap;">Press </span>`<span class="editor-theme-code">Ctrl+X</span>`<span style="white-space: pre-wrap;"> to exit Nano.</span>

---

## 2. Create a restart script for APISIX

1. In the terminal, make a new script:  
    ```bash
    nano /opt/apisix/restart_apisix.sh
    ```
2. <span style="white-space: pre-wrap;">Copy and paste this into </span>`<span class="editor-theme-code">restart_apisix.sh</span>`:  
    ```bash
    #!/bin/bash
    
    # If a container named "apache-apisix" exists, stop and remove it
    if docker ps -a --format '{{.Names}}' | grep -Eq "^apache-apisix\$"; then
      echo "Stopping and removing existing apache-apisix container..."
      docker stop apache-apisix
      docker rm apache-apisix
    fi
    
    echo "Starting a new APISIX container..."
    docker run -d \
      --name apache-apisix \
      -p 9080:9080 \
      -e APISIX_STAND_ALONE=true \
      -v /opt/apisix/apisix.yaml:/usr/local/apisix/conf/config.yaml \
      -v /opt/apisix/apisix.yaml:/usr/local/apisix/conf/apisix.yaml \
      apache/apisix
    
    echo "APISIX container is now running."
    ```
3. Save and close:
    - <span style="white-space: pre-wrap;">Press </span>`<span class="editor-theme-code">Ctrl+O</span>`<span style="white-space: pre-wrap;">, then </span>`<span class="editor-theme-code">Enter</span>`.
    - <span style="white-space: pre-wrap;">Press </span>`<span class="editor-theme-code">Ctrl+X</span>`.
4. Make the script executable:  
    ```bash
    chmod +x /opt/apisix/restart_apisix.sh
    ```

---

## 3. Run APISIX for the first time

1. In the terminal, run:  
    ```bash
    ./restart_apisix.sh
    ```
2. <span style="white-space: pre-wrap;">Wait a few seconds. APISIX will start in Docker, listening on port 9080, reading your </span>`<span class="editor-theme-code">/opt/apisix/apisix.yaml</span>`<span style="white-space: pre-wrap;"> as both </span>`<span class="editor-theme-code">config.yaml</span>`<span style="white-space: pre-wrap;"> and </span>`<span class="editor-theme-code">apisix.yaml</span>`.
3. Check that it is running:  
    ```bash
    sudo docker ps
    ```
    
      
    <span style="white-space: pre-wrap;">You should see a line for </span>`<span class="editor-theme-code">apache/apisix</span>`<span style="white-space: pre-wrap;"> with </span>`<span class="editor-theme-code">0.0.0.0:9080->9080/tcp</span>`.
4. Look at the logs to ensure no errors:  
    ```bash
    sudo docker logs apache-apisix
    ```
    
      
    You should see messages like “config file … reloaded” and no errors.

---

## 4. Test APISIX locally with curl (no Nginx yet)

1. In the terminal, run:  
    ```bash
    curl -u foo:bar http://127.0.0.1:9080/demo/md/3.0/accounts
    ```
    
    
    - `<span class="editor-theme-code">-u foo:bar</span>`<span style="white-space: pre-wrap;"> sends your Basic Auth.</span>
    - <span style="white-space: pre-wrap;">The path </span>`<span class="editor-theme-code">/demo/md/3.0/accounts</span>`<span style="white-space: pre-wrap;"> matches your route pattern </span>`<span class="editor-theme-code">/demo/md/*/accounts</span>`.
2. If everything is correct, you will see JSON returned from Exante.  
    <span style="white-space: pre-wrap;">If you see </span>`<span class="editor-theme-code">{"error_msg":"404 Route Not Found"}</span>`, double-check that the path matches exactly and that your tokens are correct.

---