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

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.

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

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

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

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.

Step 4: Install the packages in the requirements.txt file. This step includes 2 small steps as follows:
- Type “requirements.txt” and click the Add button.
- Click the Run Pip Install button and select requirements.txt.

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.

Step 6: Run FastAPI in Terminal.
Open Terminal in 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.

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.
- No HTTPS (SSL) causes the browser to display “Not Secure”.
- The Port part at the end makes the URL look unprofessional and unfinished.

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

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]

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.

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.
Command | Chức năng |
---|---|
screen -S myfastapi | Create a new screen named myfastapi |
screen -ls | List all running screens |
screen -r myfastapi | Back to the screen that myfastapi is running |
Ctrl + A, D | Exit the screen but keep the process running |
exit | Exit and close the screen |
We should use it screen
for the following two reasons:
- 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.
- Avoid CORS error with POST when you have configured
allow_origins
inadd_middleware
of FastAPI. This is just my experience when not usingscreen
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.