{"id":26829624,"url":"https://github.com/murat-polat/fastapi-deploymet-example","last_synced_at":"2025-04-30T06:07:25.337Z","repository":{"id":283893472,"uuid":"631557454","full_name":"murat-polat/FastAPI-deploymet-example","owner":"murat-polat","description":"FastAPI production deployment with secure SSL/HTTPS, and Caddy server","archived":false,"fork":false,"pushed_at":"2025-03-22T20:54:28.000Z","size":252,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-04-30T06:07:24.664Z","etag":null,"topics":["caddy","deployment","fastapi","https-proxy","python","rest-api","supervisor"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/murat-polat.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-04-23T12:02:51.000Z","updated_at":"2025-03-22T20:54:32.000Z","dependencies_parsed_at":null,"dependency_job_id":"48a40b82-b9e4-45cf-ae90-443b4e62b85a","html_url":"https://github.com/murat-polat/FastAPI-deploymet-example","commit_stats":null,"previous_names":["murat-polat/fastapi-deploymet-example"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/murat-polat%2FFastAPI-deploymet-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/murat-polat%2FFastAPI-deploymet-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/murat-polat%2FFastAPI-deploymet-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/murat-polat%2FFastAPI-deploymet-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/murat-polat","download_url":"https://codeload.github.com/murat-polat/FastAPI-deploymet-example/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251651231,"owners_count":21621715,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["caddy","deployment","fastapi","https-proxy","python","rest-api","supervisor"],"created_at":"2025-03-30T13:17:50.999Z","updated_at":"2025-04-30T06:07:25.322Z","avatar_url":"https://github.com/murat-polat.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"### This  is a simple example  for deploying a FastAPI application, to production environment. And probably the easiest way to run an application with secure SSL/HTTPS, and with Caddy server magic. So, you can focus just your application and code, not deployment process :)\n\n\n\n\n### What we need ?\n\n- Linux machine/VM \n- Domainname\n- FastAPI application\n- Caddy server and reverse proxy configuration for HTTPS/SSL\n\n### Linux VM :\nYou can order a Linux VM from the Digital Ocean, Contabo, Vultr, Linode etc. For this tutorial, we choose an Debian instance on Contabo.\nAfter  first login, run:\n\n`sudo apt-get update`\n\nIf sudo not working on Debian:\n\n`apt install sudo`\n\nIt's optional add a new user, or continue with \"root\" user. But we choose and recommend a new user, and give it admin privileges.\n\n`sudo adduser \u003cnewuser\u003e`\n\n`sudo usermod -aG sudo \u003cnewuser\u003e`\n\nChange root user to new user\n\n`su \u003cnewuser\u003e`\n\n\n`python3 -m pip install fastapi[all] gunicorn uvicorn`\n\nIf \"pip\" not working, so install python3 pip\n\n`sudo apt install python3-pip`\n\n\n### Domain configuration\n\nWe need a domain name for publishing our application in World Wide Web. We choose a domainname from the https://www.namecheap.com. And we must tell the domain provider which server/IP provider will be used for publishing. Our FastAPI application will be served on Contabo. From  domain list =\u003e management =\u003e Nameservers =\u003e Custom DNS add three nameservers (ns1.contabo.net, ns2.contabo.net, ns3.contabo.net) and save.\n\n![](/src/custom_DNS.png)\n\nFrom Contabo \"DNS Zone Management\", add your domainname to \"Domain\", and  \"Target IP address\" select your VM instance/IP from dropdown menu =\u003e then click \"create zone\".\n\n![](/src/DNS_Zone_mgmt.png)\n\n### FastAPI application\n As we mentioned above that, this is just a demo application.  For demonstration perposes\n\n `nano main.py`\n\n ```\nfrom fastapi import FastAPI\n\napp = FastAPI()\n\n\n@app.get(\"/items/{item_id}\")\nasync def read_item(item_id: int):\n    return {\"item_id\": item_id}\n \n  \n ```\nFor testing just run:\n\n`uvicorn main:app --reload`\n\nIf it's running without error, so vi need make a \"service\" or \"supervisor\" for our FastAPI application. The reason for that, to do application more robust and relable for pruduction. After that you can start, stop and see status just from the command line.\n\nTo do that run:\n\n`sudo nano /etc/systemd/system/fastapi.service`\n\nand copy-paste the code block below\n\n```\n\n[Unit]\nDescription=fastapi service\nAfter=network.target\n\n[Service]\nUser=\u003cnewuser\u003e\nGroup=www-data\nWorkingDirectory=/home/\u003cnewuser\u003e/fastapi\nExecStart=gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000\n\nRestart=always\n\n[Install]\nWantedBy=multi-user.target\n\n```\n\nHit \"Ctrl+X\" then \"Y\" for save the \"fastapi.service\" (For Nano editor)\n\nNow our FastAPI application more robust and easy to control. For start and enable the service, run:\n\n` sudo systemctl start  fastapi`\n\nTo enable the service:\n\n` sudo systemctl enable  fastapi`\n\nTo stop the service:\n\n` sudo systemctl stop  fastapi`\n\nTo see status of the service:\n\n\n` sudo systemctl status  fastapi`\n\n```\n● fastapi.service - fastapi service\n     Loaded: loaded (/etc/systemd/system/fastapi.service; enabled; vendor preset: enabled)\n     Active: active (running) since Sat 2023-04-22 21:52:48 CEST; 15h ago\n   Main PID: 333920 (gunicorn)\n      Tasks: 5 (limit: 9507)\n     Memory: 82.6M\n        CPU: 8min 26.889s\n     CGroup: /system.slice/fastapi.service\n             ├─333920 /usr/bin/python3 /usr/bin/gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000\n             ├─333921 /usr/bin/python3 /usr/bin/gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000\n             ├─333922 /usr/bin/python3 /usr/bin/gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000\n             ├─333923 /usr/bin/python3 /usr/bin/gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000\n             └─333924 /usr/bin/python3 /usr/bin/gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000\n\nApr 22 21:52:48 vmi1180878.contaboserver.net gunicorn[333924]: [2023-04-22 21:52:48 +0200] [333924] [INFO] Application startup complete.\nApr 22 22:03:18 vmi1180878.contaboserver.net gunicorn[333923]: [2023-04-22 22:03:18 +0200] [333923] [WARNING] Invalid HTTP request received.\n\n```\n\nNow everything is working properly with HTTP on port 8000. But we need SSL/HTTPS,and configure Caddy server for reverse proxy.\n\n### Caddy Server configuration\n#### Installation:\n\nhttps://caddyserver.com/docs/install#debian-ubuntu-raspbian \n\n`sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https`\n\n\n`curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg`\n\n`curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list`\n\n`sudo apt update`\n\n`sudo apt install caddy`\n\n#### Configuration reverse proxy with Caddyfile\n\n`sudo nano /etc/caddy/Caddyfile`\n\n```\nyourdomainname.net {\n\n        reverse_proxy  localhost:8000\n\n}\n\n```\n\n`sudo systemctl reload caddy`\n\n\nDone ! \n\nHappy coding with awasome FastAPI :)\n\n![](/src/browser.png)\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmurat-polat%2Ffastapi-deploymet-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmurat-polat%2Ffastapi-deploymet-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmurat-polat%2Ffastapi-deploymet-example/lists"}