Learn how to set up a FastAPI application and deploy it on DigitalOcean. Our DigitalOcean Support team is here to help you with your questions and concerns.
How to Set up a FastAPI Application with DigitalOcean
Building and deploying applications quickly and efficiently is important in today’s fast-paced development environment.
FastAPI is known for its speed and ease of use. Using it with DigitalOcean’s scalable infrastructure offers an ideal solution for developers.
This guide will walk you through setting up a FastAPI application and deploying it on DigitalOcean, ensuring a seamless and efficient development process.
Benefits of Using FastAPI and DigitalOcean
Let’s take a quick look at some of the benefits of using aFastAPI Application with DigitalOcean:
- FastAPI offers a rapid development experience with its clean syntax and built-in features.
- DigitalOcean offers a flexible and scalable platform to handle the application’s growth.
- DigitalOcean also offers competitive pricing compared to other major cloud providers.
- We get to have full control over the server environment and deployment process.
How to Build a FastAPI Application and Deploying on DigitalOcean
1. Create a FastAPI Application
- First, create a folder called `fast-api-ocean` in the desired directory.
- Then, create a file named `api.py` inside this folder. This file will contain the FastAPI code. To prevent any CORS issues, we have to integrate CORS Middleware.
Here’s the complete code for `api.py`:
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
app = FastAPI()
origins = ['*']
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def root():
return {"message": "Hello from FastAPI"}
if __name__ == "__main__":
uvicorn.run(app, host='0.0.0.0', port=8000)
- We can verify if FastAPI is working with this command:
$ python3 api.py
- Then, go to http://localhost:8000/.
We will see the message “Hello from FastAPI”.
Additionally, we can access the Swagger UI at http://localhost:8000/docs to test the endpoints.
2. Create requirements.txt
- Next, create a `requirements.txt` file to manage and install the dependencies. We can manually add the necessary packages or generate the file using the following command:
$ pip freeze > requirements.txt
- Also, make sure the `requirements.txt` includes the following:
fastapi
uvicorn
gunicorn
3. Create a DigitalOcean Droplet
- First, sign in to the DigitalOcean account.
- Then, click “Create” in the top menu and select “Droplets”.
- After that, choose the server region.
- Now, select an image for the server. In this example, we will use Ubuntu 22.10 x64.
- Next, choose the CPU options.
- After that, select SSH Key in the Authentication Method.
- Then, customize the droplet as needed and click Create Droplet.
- Now, the droplet will be created. We can view it in the “Droplets” section.
4. Access the Droplet via SSH
To access the droplet, we have two options:
- First, click on the “Console” option in the top right corner of the Droplets page.
- Alternatively, access the droplet from the local machine using SSH. So, copy the IPv4 address of the droplet. Then, run this command:
$ ssh root@your_droplet_ip
5. Copy Files via SCP
Now, it is time to transfer our code files (`api.py` and `requirements.txt`) to the remote server. This can be done via SCP.
So, open a terminal and go to the project folder. Then, run this command:
$ scp -r fast-api-ocean root@your_droplet_ip:/root
This command copies the `fast-api-ocean` folder to the root directory of the remote server. After copying, go to `/root/fast-api-ocean` on the droplet to find our files.
6. Create a Virtual Environment
We can create a virtual environment using `virtualenv` in the `fast-api-ocean` directory:
# Install virtualenv if not already installed
root:~/fast-api-ocean# pip install virtualenv
# Create a virtual environment
root:~/fast-api-ocean# virtualenv venv
# Activate the virtual environment
root:~/fast-api-ocean# source venv/bin/activate
By following the above steps, we can easily set up a FastAPI application and deploy it on DigitalOcean, leveraging the power and flexibility of both platforms.
[Need assistance with a different issue? Our team is available 24/7.]
Conclusion
In brief, our Support Experts demonstrated how to set up a FastAPI application and deploy it on DigitalOcean, leveraging the power and flexibility of both platforms.
0 Comments