Setup FastAPI in cPanel managed server
Published on 16 Aug 2022
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
IDE architecture" cli crontab crypto csv database docker editor error exceptions fastapi ffmpeg filename git go history http iteration javascript linux logging me mysql numpy orm pdo pentaho php postgres python random rename rsync rust selenium server solana sql sqlite ssh typescript user w3m wordpress