Launching a CentOS7 Virtual Machine on Windows10: Part 4: API Server

In my previous post, Launching a CentOS7 Virtual Machine on Windows10: Part 3: Shared Folder, I went through the steps I took to add a shared folder to a CentOS7 virtual machine. In this post, I’ll go through how to configure the virtual machine as an API server. For this example, I’ll be using python’s FastAPI.

Once the server is configured as an API host, the virtual machine will:

  1. Serve API docs and responses via a web browser on the host machine.
  2. Accept curl requests to the API from the virtual machine or another machine on the local network, returning a response.

Prerequisites

Before you start, you will need to set up the virtual machine, PuTTY SSH, networking and a shared folder by following the steps in:

  1. Launching a CentOS7 Virtual Machine on Windows10
  2. Launching a CentOS7 Virtual Machine on Windows10: Part 2: Networking
  3. Launching a CentOS7 Virtual Machine on Windows10: Part 3: Shared Folder

Step 1: Update Networking Default Route

In order for your virtual machine IP to be routed correctly from your host machine’s browser, you will need to update the default route in your virtual machine networking configuration to be that of the host-only ethernet adapter.

  1. Power on your virtual machine (by clicking ‘Start’ in VirtualBox).
  2. Once booted, SSH onto the machine via PuTTY.
  3. Run the following:
    $ sudo su
    $ cd /etc/sysconfig/network-scripts
    $ echo "DEFROUTE=yes" >> ifcfg-enp0s8
    
  4. Run:
    $ vim ifcfg-enp0s3
    
    With this file open, remove the “DEFROUTE=yes” line. Save and close the file by pressing the ESC key and then running:
    :wq
    
  5. Reboot the virtual machine.
  6. SSH back into the virtual machine via PuTTy and try running:
    $ ping google.com
    
    If the network is up, you should get a response like:
    PING google.com (142.250.186.142) 56(84) bytes of data.
    64 bytes from fra24s07-in-f14.1e100.net (142.250.186.142): icmp_seq=1 ttl=55 time=33.1 ms
    ...
    

Step 2: Open port 8000 in Firewalld

The API service is going to run on port 8000, so we need to open that port in the CentOS7 firewall service, firewalld.

  1. SSH to your virtual machine using PuTTY and run:
    $ sudo firewall-cmd --permanent --zone=public --add-port=8000/tcp
    

Step 3: Install Python

The API service will be running on python, so we need to install python3.

  1. SSH to your virtual machine using PuTTY and run:
    $ sudo yum install -y python3
    

Step 4: Install FastAPI

These steps are partly taken from https://fastapi.tiangolo.com/tutorial/, so see the tutorial at that link for more info/examples of FastAPI.

  1. SSH onto the virtual machine via PuTTY (if not already).
  2. Run the following:
    $ cd
    $ python -m venv .venv-fastapi
    $ . .venv-fastapi/bin/activate
    (.venv-fastapi) $ pip install --upgrade pip
    (.venv-fastapi) $ pip install "fastapi[all]"
    
  3. Now create a new directory in the shared directory where the API code will go (where vboxshare should be replaced with the name of your shared directory):
    $ mkdir -p /home/osboxes/vboxshare/fastapi
    
  4. Next, create a new file in the fastapi directory called main.py:
    $ cd /home/osboxes/vboxshare/fastapi
    $ vim main.py
    
  5. Save the following code into main.py:
    from fastapi import FastAPI
    
    app = FastAPI()
    
    
    @app.get("/")
    async def root():
        return {"message": "Hello World"}
    
  6. Start the uvicorn API server, setting host 0.0.0.0.0 so it will listen on any available interface:
    $ uvicorn --host 0.0.0.0 main:app --reload
    

Step 5: Test FastAPI

  1. With the API server running, navigate to http://YOUR_VIRTUAL_MACHINE_IP:8000 in your browser, replacing YOUR_VIRTUAL_MACHINE_IP with the IP set up in Part 2. If you need a reminder as to your virtual machine IP, in a new PuTTY window, run:
    $ ip addr | grep enp0s8
    
    The IP will be listed after ‘inet’, on the second line of the output.
  2. In your browser, you should see the response:
    {"message":"Hello World"}
    
  3. Now try navigating to http://YOUR_VIRTUAL_MACHINE_IP:8000/docs in your browser. You should see the swagger documentation for the /Root endpoint. If you click on it, then click ‘Try it out’, then ‘Execute’, you should see this response in the ‘Response Body’ box:
    {
    	"message":"Hello World"
    }
    
  4. Start a new PuTTY SSH session to your virtual machine.
  5. Try running:
    curl -X 'GET' 'http://YOUR_VIRTUAL_MACHINE_IP:8000/' -H 'accept: application/json'
    
    You should get the response:
    {"message":"Hello World"}
    
  6. Try running:
    curl -X 'GET' 'http://127.0.0.1:8000/' -H 'accept: application/json'
    
    You should get the response:
    {"message":"Hello World"}
    

Conclusion

You should now have a CentOS7 virtual machine running as a FastAPI API server, which can receive requests over curl from within the local network or from a browser on your host machine.

Thanks for reading!