It seems a number of people have run into the same problem with this, but so far it is proved unfixable. I am hoping someone might have worked it out.
I am trying to run FastAPI on cPanel and keep getting the error [UID:1293][19858] Child process with pid: 20085 was killed by signal: 15, core dump: 0
I set it up in cPanel using the python app (my host provider allows python scripts) with python 3.8.12 and do the pip installs via terminal all of which worked fine (uvicorn, fastapi and various others). On GitHub someone recommended running it on its own subdomain, but that didn't solve the issue it either.
My passenger_wsygi.py code works fine like this to produce "It works!" in the browser when I visit api.mydomain.com
import os
import sys
from a2wsgi import ASGIMiddleware
from main import app # Import your FastAPI app.
application = ASGIMiddleware(app)
# debugging section
async def log_middleware(scope, receive, send):
async def _receive():
msg = await receive()
print(msg)
return msg
async def _send(msg):
print(msg)
await send(msg)
return await app(scope, _receive, _send)
# end of debugging section
sys.path.insert(0, os.path.dirname(__file__))
def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
message = 'It works!\n'
version = 'Python %s\n' % sys.version.split()[0]
response = '\n'.join([message, version])
return [response.encode()]
But if I remove the def application above, then I start getting the child process error.
Here is my test main.py:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def root():
return "Hello World!"
All the code is in the root folder of api.mydomain.com
There are no additional errors in the cPanel error logs and running it from the terminal gives no other clues.
Links I have looked at for solution are here, here and here among many others, but none of which have produced a successful outcome. I haven't yet tried this alternative to a2swgi but I am rapidly losing interest in FastAPI on cPanel, but wanted to give it one last shot at finding a fix.
EDIT:
the only other clue I have is when trying to test uvicorn in the same python environment from terminal in cPanel it wont do anything. I run the python env. then type uvicorn main:app
this announces that Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
I type: curl http://localhost:8000
and it hangs there until I cancel it. no errors in logs. nothing. but no "hello world" either.
EDIT 2: I ran ps aux in a terminal while using api.mysite.com to call the code and see the processes
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
thetempl 17063 0.0 0.0 215176 12544 ? S 19:15 0:00 lsphp
thetempl 17531 0.0 0.0 113636 2044 pts/0 S 19:03 0:00 /bin/bash -l
thetempl 27453 0.0 0.0 153536 1792 pts/0 R+ 19:15 0:00 ps aux
thetempl 30655 0.6 0.0 218740 27752 ? S<l 19:13 0:00 /opt/alt/python38/bin/lswsgi -m /home/thetempl/api.mysite.com/passenger_wsgi.py
thetempl 31022 0.0 0.0 218740 20396 ? S<s 19:13 0:00 /opt/alt/python38/bin/lswsgi -m /home/thetempl/api.mysite.com/passenger_wsgi.py
The above was just before it timed out. Once it timed out the lsphp line was gone and PID 31022 too, but I had to kill off the PID 30655 process manually. In stderr.log the following line showed up.
[UID:1293][30655] Child process with pid: 31022 was killed by signal: 15, core dump: 0
I have been looking at this issue and this one but still no luck getting uvicorn to run in terminal using different ports, or in browser.