Source: Using FastAPI to Build Python Web APIs – Real Python
Live at https://api.cstories.app
- $12/month
- Ubuntu 22.04 LTS x64
- 2 GB RAM | 1 CPU | 50GB SSD
- Region: sfo3
- Hostname:
cstories
- IP:
164.92.110.38
- bbest
- cgrant
- jzadra
sudo usermod -a -G sudo <user>
- net-tools
- python-is-python3
- python3.10-venv
- docker
-
Add users to common group
sudo usermod -g staff <user>
-
Make shared directory
sudo mkdir -p /share/github
-
Change group of shared directory and set permissions
sudo chgrp -R staff /share && \ sudo chmod -R 2774 /share && \ # inherit, read, write, execute
-
Clone the repo
cd /share/github && \ git clone https://github.com/cstories-app/api.git
-
Create
.env
filecd api && \ echo "OPENAI_API_KEY=MY_KEY" >> .env echo "PATH_GOOGLE_SA_KEY_JSON=path/to/key.json" >> .env
-
Create the Python virtual environment
python -m venv venv && \ source venv/bin/activate python -m pip install --upgrade pip && \ python -m pip install -r requirements.txt
-
Run the app
uvicorn main:app --reload --host 0.0.0.0 --port 8000
-
Or use the Dockerfile
docker run -it --rm -v $(pwd):/usr/local/app -v ${PATH_GOOGLE_SA_KEY_JSON}:/usr/local $(docker build -q -t fastapi .)
Run the Docker stack in daemon mode: docker compose up -d
Create config: /etc/apache/sites-available/api.conf
:
<VirtualHost *:80>
ServerName api.cstories.app
ServerSignature Off
Header always unset X-Frame-Options
Header set X-Frame-Options "SAMEORIGIN"
Header set X-XSS-Protection "1; mode=block"
Header always set X-Content-Type-Options "nosniff"
ErrorLog /var/log/apache2/redirect.error.log
LogLevel warn
ProxyPreserveHost On
ProxyPass "/" "http://143.198.103.35:8000/"
ProxyPassReverse "/" "http://143.198.103.35:8000/"
#Redirect / https://api.cstories.app
RewriteEngine on
RewriteCond %{SERVER_NAME} =api.cstories.app
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName api.cstories.app
ServerSignature Off
Header always unset X-Frame-Options
Header set X-Frame-Options "SAMEORIGIN"
Header always set Strict-Transport-Security "max-age=63072000"
Header set X-XSS-Protection "1; mode=block"
Header always set X-Content-Type-Options "nosniff"
ErrorLog /var/log/apache2/redirect.error.log
LogLevel warn
ProxyPreserveHost On
ProxyPass "/" "http://143.198.103.35:8000/"
ProxyPassReverse "/" "http://143.198.103.35:8000/"
SSLCertificateFile /etc/letsencrypt/live/api.cstories.app/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/api.cstories.app/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
Create certs and enable the site:
sudo certbot certonly --apache
sudo a2ensite api.conf
sudo systemctl reload apache2