Avatar
Fernando Vásquez is an electronics engineer and software developer currently based in the world. He occasionally blogs about Python and Android programming.

Setup FastAPI in cPanel managed server

Bridge API

At this point I am assuming you alredy have a FastAPI application ready to run. Let’s say the server python file is server.py with the name app:

Run uvicorn in background with our app:

   nohup uvicorn server:app >fastapi.out 2>fastapi.err &

cPanel API setup

1/ Set cPanel user in $user environment variable and domain in $domain:

export user='cpanel_user'
export domain='example.com'

2/ Create include files 1

sudo mkdir -p /etc/apache2/conf.d/userdata/ssl/2_4/$user/$domain/
sudo touch /etc/apache2/conf.d/userdata/ssl/2_4/$user/$domain/include.conf
sudo mkdir -p /etc/apache2/conf.d/userdata/std/2_4/$user/$domain/
sudo touch /etc/apache2/conf.d/userdata/std/2_4/$user/$domain/include.conf

3/ Add proxy directives2 in /etc/apache2/conf.d/userdata/std/2_4/$user/$domain/include.conf, replace __SERVER__ with the domain, in this case example.com:

ProxyPass /.well-known !
Redirect permanent / https://__SERVER__/

4/ Add proxy SSL directives3 in /etc/apache2/conf.d/userdata/ssl/2_4/$user/$domain/include.conf:

SSLEngine on
SSLCertificateFile /var/cpanel/ssl/apache_tls/__SERVER__/combined
SSLUseStapling off
SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
ProxyPass / http://127.0.0.1:8000/
ProxyPassReverse / http://127.0.0.1:8000/
Redirect permanent /__SERVER__ /__SERVER__/
<IfModule mod_headers.c>
	Header set Access-Control-Allow-Origin '*'
</IfModule>

5/ Rebuild Apache conf:

sudo /usr/local/cpanel/scripts/rebuildhttpdconf

6/ Restart Apache:

sudo /usr/local/cpanel/scripts/restartsrv_httpd

References

all tags