How to deploy FastAPI on cPanel using Uvicorn

When you start FastAPI but are familiar with hosting’s cPanel instead of VPS/Server, you can still deploy FastAPI on cPanel using Uvicorn instead of WSGI. We do not need dedicated hosting for Python. Follow the instructions below to save time and money.

Deploy FastAPI on cPanel

To quickly Deploy FastAPI on cPanel, we will start with a simple FastAPI project with 2 files as follows:

main.py

from fastapi import FastAPI
from pydantic import BaseModel

class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None


app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World"}


@app.post("/items/")
async def create_item(item: Item):
    return item

requirements.txt

fastapi
pydantic
uvicorn
Simple project to demonstrate deploying FastAPI on cPanel
Simple project to demonstrate deploying FastAPI on cPanel

Step 1: Make sure you have pointed the domain name to the correct IP of the hosting and added the domain name to the hosting. Don’t forget to enable the secure HTTPS protocol for the domain name.

Add a domain name for your FastAPI application
Add a domain name for your FastAPI application

Step 2: Upload your project files to your domain’s folder in File Manager.

Tải source code của dự án FastAPI vào thư mục của tên miền
Download the source code of the FastAPI project into the domain directory

Step 3: Create a Python application. Go to Setup Python App in cPanel.

Go to Setup Python App to deploy a Python application
Go to Setup Python App to deploy a Python application

Click the CREATE APPLICATION button to create a Python application in cPanel.

Click the CREATE APPLICATION button
Click the CREATE APPLICATION button

Just fill in the Application root and Application URL as your domain name and click the CREATE button. You don’t need to worry about the other fields.

Just fill in the Application root and Application URL
Just fill in the Application root and Application URL

Step 4: Install the packages in the requirements.txt file. This step includes 2 small steps as follows:

  1. Type “requirements.txt” and click the Add button.
  2. Click the Run Pip Install button and select requirements.txt.
Install Python packages
Install Python packages

Step 5: Click the STOP APP button and copy the command to access the application’s virtual environment.

The reason for stopping is because FastAPI does not work well with WSGI. No matter how you configure the passenger_wsgi.py file, it still gives you an error as [UID:1003][2014879] Child process with pid: 2021889 was killed by signal: 15, core dumped: no. We just borrowed the Setup Python App to create a valid virtual environment on cPanel.

Stop the App and copy the command on cPanel
Stop the App and copy the command on cPanel

Step 6: Run FastAPI in Terminal.

Open Terminal in cPanel.

Open Terminal on cPanel
Open Terminal on cPanel

Create a new screen. I will introduce the command screen in more detail at the end of the article. For now, just follow the instructions. Change the myfastapi name to whatever you want.

screen -S myfastapi

Paste and run the command you copied in step 5, you can also install other packages with Pip here. Then run Uvicorn with this command.

uvicorn main:app --host 0.0.0.0 --port 8000

If you get the error “address already in use” then it means that there is another application running with this local IP. You just need to increase the port, for example, 8001.

Deploy FastAPI on cPanel running with Uvicorn
Deploy FastAPI on cPanel running with Uvicorn

Once Terminal is error-free, access your FastAPI in the domain:port or domain:port/docs format. It’s great that your FastAPI is finally working, but it still has two issues to deal with.

  1. No HTTPS (SSL) causes the browser to display “Not Secure”.
  2. The Port part at the end makes the URL look unprofessional and unfinished.
FastAPI is already working on cPanel
FastAPI is already working on cPanel

Detach the screen before closing the Terminal. Press the Ctrl + A key combination, then press the D key to detach the current screen.

Detach screen running FastAPI
Detach screen running FastAPI

Step 7: Enable HTTPS and set up a proxy for your domain pointing to the local FastAPI address. Add this code to the .htaccess file in your domain directory. Remember to replace fastapi.lucidgen.com:8001 with your local FastAPI address.

RewriteEngine On

# Force HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Reverse Proxy
RewriteRule ^(.*)$ http://fastapi.lucidgen.com:8001/$1 [P,L]
Enable HTTPS and remove the Port when accessing FastAPI
Enable HTTPS and remove the Port when accessing FastAPI

If you don’t see the .htaccess file, it may be hidden, click the Settings button and enable Show Hidden Files (dotfiles) to display hidden files in File Manager. If there is no .htaccess file at all, create it.

Step 8: Go to your official FastAPI domain. HTTPS is now enabled, and Port is no longer in the URL.

FastAPI has been fully deployed on cPanel
FastAPI has been fully deployed on cPanel

Explain about screen

Command screen used to save your session in Terminal, when you close and reopen Terminal, you can easily return to the screen of the previous session.

CommandChức năng
screen -S myfastapiCreate a new screen named myfastapi
screen -lsList all running screens
screen -r myfastapiBack to the screen that myfastapi is running
Ctrl + A, DExit the screen but keep the process running
exitExit and close the screen

We should use it screen for the following two reasons:

  1. You can easily disable and restart your FastAPI. Just go back to the screen where FastAPI is running and press Ctrl + C to disable FastAPI.
  2. Avoid CORS error with POST when you have configured allow_origins in add_middleware of FastAPI. This is just my experience when not using screen and closing the Terminal, you may not have this problem.

Conclusion

I just accidentally thought of how to deploy FastAPI on cPanel as above, and luckily, it works; I am not really an expert on hosting, VPS/Server. If you find this article useful, please share it on forums to help others.

For now, for small and medium-sized FastAPI projects, I find that they work very well on cPanel this way. I don’t have any FastAPI projects with large scale and access to test its stability on cPanel. If you find any problems with this way, please share with me and everyone through the comments below.

Related articles

Hieu Tran Ngoc Minh

Hieu Tran Ngoc Minh

I am currently working as a Data Analyst; before that, I worked in Digital Marketing. Blogging is a joy, helping me share my knowledge and experiences from life and work. You can donate to me here.

Leave a Comment

Feel free to leave your comment, and we will review and respond as soon as possible. Please use a real email to ensure your comment is approved and to receive notifications when we reply. You can also add an avatar to your email.