{"id":18378,"date":"2026-10-14T06:50:12","date_gmt":"2026-10-14T06:50:12","guid":{"rendered":"https:\/\/greenwebpage.com\/community\/?p=18378"},"modified":"2026-10-14T06:55:42","modified_gmt":"2026-10-14T06:55:42","slug":"how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu","status":"publish","type":"post","link":"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/","title":{"rendered":"How to Deploy an AI API with Python and Nginx on Ubuntu"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\n  A model that works in a notebook is not a service yet. To deploy an AI API to a server, you need something that loads the model once, answers HTTP requests, restarts when it crashes, and sits behind a proper web server with HTTPS. This guide walks through that setup on Ubuntu 26.04 using Python, FastAPI, Uvicorn, systemd, and Nginx. \n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  This guide also covers the problems that only show up with machine learning models, such as workers that each load their own copy of the model, timeouts on slow inference, and streamed replies that arrive all at once.\n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Quick Outline<\/strong>\n<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#What-You-Need-to-Deploy-an-AI-API-on-Ubuntu\">What You Need to Deploy an AI API on Ubuntu<\/a><\/li>\n\n\n\n<li><a href=\"#Can-You-Deploy-an-AI-API-for-Free\">Can You Deploy an AI API for Free?<\/a><\/li>\n\n\n\n<li><a href=\"#Deploying-an-AI-API-with-Python-and-Nginx-on-Ubuntu-26.04\">Deploying an AI API with Python and Nginx on Ubuntu 26.04<\/a><\/li>\n\n\n\n<li><a href=\"#When-Should-You-Deploy-an-AI-API-on-Kubernetes\">When Should You Deploy an AI API on Kubernetes?<\/a><\/li>\n\n\n\n<li><a href=\"#post-18378-_s970epn7luh8\">How to Troubleshoot Common AI API Deployment Errors<\/a><\/li>\n\n\n\n<li><a href=\"#conclusion\">Conclusion<\/a><\/li>\n\n\n\n<li><a href=\"#faqs\">FAQs<\/a><br><\/li>\n<\/ul>\n\n\n\n<h2 id=\"What-You-Need-to-Deploy-an-AI-API-on-Ubuntu\" class=\"wp-block-heading\"><a id=\"post-18378-_qoo0jnsowdpq\"><\/a><strong>What You Need to Deploy an AI API on Ubuntu<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Requests come in from the internet to Nginx on ports 80 and 443. Nginx handles HTTPS and rate limiting, then forwards each request to your Python app listening only on 127.0.0.1:8000. The app keeps the model in memory and returns predictions. Because the app never faces the internet directly, nobody can bypass your TLS or rate limits.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  You will need an Ubuntu server with sudo access, a domain with an A record pointing to it, and enough RAM for your model. For a small transformer model, plan on at least 2 GB. Our guide on <a href=\"https:\/\/greenwebpage.com\/community\/how-much-ram-and-cpu-you-really-need-to-run-ai-models-on-a-vps\/\">how much RAM and CPU AI models<\/a> need covers sizing in more detail.\n<\/p>\n\n\n\n<h2 id=\"Can-You-Deploy-an-AI-API-for-Free\" class=\"wp-block-heading\"><a id=\"post-18378-_mitl3gvdjbwk\"><\/a><strong>Can You Deploy an AI API for Free?<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Yes, within limits. The cheapest route is to deploy the AI locally on your own computer while you build, then move to a server when others need to use it. A free-tier cloud VM can host the finished service. Oracle&#8217;s Always Free Arm instances are the usual choice, but allowances and capacity change, so read the current terms. \n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Check that your libraries publish Arm builds, and expect 1 GB servers to be too small for most models. Free hosting suits a small classifier or a demo, not a large language model. You can explore our guide, <a href=\"https:\/\/greenwebpage.com\/community\/how-to-self-host-ai-applications-on-a-linux-vps\/\">How to Self-Host AI Applications on a Linux VPS<\/a>.\n<\/p>\n\n\n\n<h2 id=\"Deploying-an-AI-API-with-Python-and-Nginx-on-Ubuntu-26.04\" class=\"wp-block-heading\"><a id=\"post-18378-_py5fnkk6qhmf\"><\/a><strong>Deploying an AI API with Python and Nginx on Ubuntu 26.04<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To deploy an AI API with Python and Nginx on Ubuntu, build the API using a Python framework such as FastAPI, run it with a production server like Uvicorn, and configure it as a systemd service. Configure Nginx as a reverse proxy to forward HTTP requests to the API, enable HTTPS with an SSL certificate, and <a href=\"https:\/\/greenwebpage.com\/community\/hardening-and-securing-ubuntu-26-04-lts-server\/\">secure the server for production use<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a id=\"post-18378-_g6stkavxdlgs\"><\/a><strong>Step 1: Prepare Ubuntu for Python AI API Deployment<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Update the system and install Python tooling and Nginx:\n<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><p>\n  sudo apt install -y python3 python3-venv python3-pip nginx\n<\/p><\/th><\/tr><\/thead><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Ubuntu blocks system-wide pip install with an externally managed environment error, which is intentional. Use a virtual environment for everything, and give the app its own unprivileged user:\n<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><p>\n  sudo useradd &#8211;system &#8211;home \/opt\/aiapi &#8211;shell \/usr\/sbin\/nologin aiapi\n<\/p><p>\n  sudo mkdir -p \/opt\/aiapi &amp;&amp; sudo chown aiapi:aiapi \/opt\/aiapi\n<\/p><p>\n  sudo -u aiapi python3 -m venv \/opt\/aiapi\/venv\n<\/p><\/th><\/tr><\/thead><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img fetchpriority=\"high\" decoding=\"async\" width=\"995\" height=\"238\" src=\"http:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-2.png\" alt=\"Ubuntu terminal showing commands to create user, directory, and virtual environment for AI API deployment.\" class=\"wp-image-18379\" srcset=\"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-2.png 995w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-2-300x72.png 300w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-2-768x184.png 768w\" sizes=\"(max-width: 995px) 100vw, 995px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Install the packages. On a server without a GPU, use the CPU build of PyTorch. \n<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><p>\n  sudo -u aiapi \/opt\/aiapi\/venv\/bin\/pip install fastapi &#8220;uvicorn[standard]&#8221; transformers\n<\/p><\/th><\/tr><\/thead><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"996\" height=\"496\" src=\"http:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-3.png\" alt=\"Ubuntu terminal installing fastapi, uvicorn, and transformers for AI API deployment.\" class=\"wp-image-18380\" srcset=\"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-3.png 996w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-3-300x149.png 300w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-3-768x382.png 768w\" sizes=\"(max-width: 996px) 100vw, 996px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  The default Linux wheel pulls in several gigabytes of CUDA libraries that a CPU server never uses, and that alone can fill a small disk:\n<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><p>\n  sudo -u aiapi \/opt\/aiapi\/venv\/bin\/pip install torch &#8211;index-url https:\/\/download.pytorch.org\/whl\/cpu\n<\/p><\/th><\/tr><\/thead><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><a id=\"post-18378-_sx983ge5gsk2\"><\/a><strong>Step 2: Build an AI API with Python and FastAPI<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This example serves a small sentiment model with FastAPI. Let\u2019s build an AI API with Python and FastAPI.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Load the Machine Learning Model at Startup<\/strong>\n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Create \/opt\/aiapi\/main.py:\n<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><p>\n  import os\n<\/p><p>\n  import secrets\n<\/p><p>\n  from contextlib import asynccontextmanager\n<\/p><p>\n  from fastapi import Depends, FastAPI, Header, HTTPException\n<\/p><p>\n  from pydantic import BaseModel, Field\n<\/p><p>\n  from transformers import pipeline\n<\/p><p>\n  models = {}\n<\/p><p>\n  @asynccontextmanager\n<\/p><p>\n  async def lifespan(app: FastAPI):\n<\/p><p>\n      models[&#8220;clf&#8221;] = pipeline(\n<\/p><p>\n          &#8220;sentiment-analysis&#8221;,\n<\/p><p>\n          model=&#8221;distilbert\/distilbert-base-uncased-finetuned-sst-2-english&#8221;,\n<\/p><p>\n      )\n<\/p><p>\n      yield\n<\/p><p>\n      models.clear()\n<\/p><p>\n  app = FastAPI(title=&#8221;AI API&#8221;, lifespan=lifespan)\n<\/p><p>\n  API_KEY = os.environ.get(&#8220;API_KEY&#8221;, &#8220;&#8221;)\n<\/p><p>\n  def check_key(x_api_key: str = Header(default=&#8221;&#8221;)):\n<\/p><p>\n      if not API_KEY or not secrets.compare_digest(x_api_key.encode(), API_KEY.encode()):\n<\/p><p>\n          raise HTTPException(status_code=401, detail=&#8221;Invalid API key&#8221;)\n<\/p><p>\n  class TextIn(BaseModel):\n<\/p><p>\n      text: str = Field(min_length=1, max_length=2000)\n<\/p><p>\n  @app.get(&#8220;\/health&#8221;)\n<\/p><p>\n  def health():\n<\/p><p>\n      return {&#8220;status&#8221;: &#8220;ok&#8221;}\n<\/p><p>\n  @app.post(&#8220;\/predict&#8221;, dependencies=[Depends(check_key)])\n<\/p><p>\n  def predict(body: TextIn):\n<\/p><p>\n      out = models[&#8220;clf&#8221;](body.text)[0]\n<\/p><p>\n      return {&#8220;label&#8221;: out[&#8220;label&#8221;], &#8220;score&#8221;: round(out[&#8220;score&#8221;], 4)}\n<\/p><\/th><\/tr><\/thead><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"994\" height=\"735\" src=\"http:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-5.png\" alt=\"Python code for AI API deployment using FastAPI on Ubuntu\" class=\"wp-image-18381\" srcset=\"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-5.png 994w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-5-300x222.png 300w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-5-768x568.png 768w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-5-160x117.png 160w\" sizes=\"(max-width: 994px) 100vw, 994px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Three choices here matter for any service that serves an AI model with FastAPI. The model loads once at startup inside lifespan, never inside the request handler. The endpoint is a plain def, not async def, because FastAPI runs plain functions in a thread pool, while a blocking model call inside an async function freezes the whole event loop. And the endpoint checks an API key, since an open inference endpoint invites abuse and a surprise bill.\n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Create and Test the Prediction Endpoint<\/strong>\n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Run it once by hand to download the model and confirm it works:\n<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><p>\n  cd \/opt\/aiapi\n<\/p><p>\n  sudo -u aiapi env API_KEY=test-key \/opt\/aiapi\/venv\/bin\/uvicorn main:app &#8211;host 127.0.0.1 &#8211;port 8000\n<\/p><\/th><\/tr><\/thead><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\n  In a second terminal, test it:\n<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><p>\n  curl -s http:\/\/127.0.0.1:8000\/health\n<\/p><p>\n  curl -s -X POST http:\/\/127.0.0.1:8000\/predict \\\n<\/p><p>\n    -H &#8220;x-api-key: test-key&#8221; -H &#8220;Content-Type: application\/json&#8221; \\\n<\/p><p>\n    -d &#8216;{&#8220;text&#8221;:&#8221;This deployment was easier than I expected&#8221;}&#8217;\n<\/p><\/th><\/tr><\/thead><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"988\" height=\"214\" src=\"http:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-7.png\" alt=\"Ubuntu terminal showing curl commands for AI API deployment with Python and Nginx.\" class=\"wp-image-18382\" srcset=\"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-7.png 988w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-7-300x65.png 300w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-7-768x166.png 768w\" sizes=\"(max-width: 988px) 100vw, 988px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Stop the server with Ctrl+C. Downloading the model now, instead of on first service start, avoids a confusing timeout later.\n<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a id=\"post-18378-_i97p4tt61e2i\"><\/a><strong>Step 3: Run the Python AI API as a systemd Service<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Store the real API key in a root-owned file:\n<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><p>\n  echo &#8220;API_KEY=$(openssl rand -hex 32)&#8221; | sudo tee \/etc\/aiapi.env\n<\/p><p>\n  sudo chmod 600 \/etc\/aiapi.env\n<\/p><\/th><\/tr><\/thead><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"998\" height=\"265\" src=\"http:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-8.png\" alt=\"Ubuntu terminal commands for deploying an AI API with Python and Nginx.\" class=\"wp-image-18383\" srcset=\"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-8.png 998w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-8-300x80.png 300w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-8-768x204.png 768w\" sizes=\"(max-width: 998px) 100vw, 998px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Create \/etc\/systemd\/system\/aiapi.service that <a href=\"https:\/\/greenwebpage.com\/community\/how-to-configure-network-settings-on-almalinux-10\/\">configure Linux server network settings<\/a>:\n<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><p>\n  [Unit]\n<\/p><p>\n  Description=AI API (FastAPI with Uvicorn)\n<\/p><p>\n  After=network.target\n<\/p><p>\n  [Service]\n<\/p><p>\n  User=aiapi\n<\/p><p>\n  Group=aiapi\n<\/p><p>\n  WorkingDirectory=\/opt\/aiapi\n<\/p><p>\n  EnvironmentFile=\/etc\/aiapi.env\n<\/p><p>\n  ExecStart=\/opt\/aiapi\/venv\/bin\/uvicorn main:app &#8211;host 127.0.0.1 &#8211;port 8000 &#8211;workers 1\n<\/p><p>\n  Restart=always\n<\/p><p>\n  RestartSec=5\n<\/p><p>\n  NoNewPrivileges=true\n<\/p><p>\n  PrivateTmp=true\n<\/p><p>\n  [Install]\n<\/p><p>\n  WantedBy=multi-user.target\n<\/p><\/th><\/tr><\/thead><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"986\" height=\"737\" src=\"http:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-9.png\" alt=\"Ubuntu systemd service file configuration for AI API deployment using Python and Uvicorn\" class=\"wp-image-18384\" srcset=\"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-9.png 986w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-9-300x224.png 300w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-9-768x574.png 768w\" sizes=\"(max-width: 986px) 100vw, 986px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Enable and start it:\n<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><p>\n  sudo systemctl daemon-reload\n<\/p><p>\n  sudo systemctl enable &#8211;now aiapi\n<\/p><\/th><\/tr><\/thead><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"986\" height=\"207\" src=\"http:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-10.png\" alt=\"Ubuntu terminal showing commands to reload systemctl daemon and enable aiapi service\" class=\"wp-image-18385\" srcset=\"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-10.png 986w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-10-300x63.png 300w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-10-768x161.png 768w\" sizes=\"(max-width: 986px) 100vw, 986px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Why Should You Use One Worker for a Machine Learning API?<\/strong>\n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Every worker process loads its own full copy of the model. Four workers with a 1.5 GB model need roughly 6 GB before the operating system takes anything. Start with one worker, measure memory and latency, and add more only if RAM allows. Heavy models rarely get faster with more processes anyway, because PyTorch already uses multiple CPU cores for a single prediction.\n<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a id=\"post-18378-_ca4gtbr03ce6\"><\/a><strong>Step 4: Configure Nginx as a Reverse Proxy for Your AI API<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You <a href=\"https:\/\/greenwebpage.com\/community\/how-to-install-nginx-on-ubuntu-22-04\/\">installed Nginx<\/a> in Step 1. Confirm it is running and open the firewall for web traffic and SSH:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><p>\n  sudo systemctl status nginx\n<\/p><p>\n  sudo ufw allow OpenSSH\n<\/p><p>\n  sudo ufw allow &#8216;Nginx Full&#8217;\n<\/p><p>\n  sudo ufw enable\n<\/p><\/th><\/tr><\/thead><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"992\" height=\"789\" src=\"http:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-11.png\" alt=\"Nginx status active and running on Ubuntu, showing systemctl and ufw commands.\" class=\"wp-image-18386\" srcset=\"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-11.png 992w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-11-300x239.png 300w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-11-768x611.png 768w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-11-220x175.png 220w\" sizes=\"(max-width: 992px) 100vw, 992px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Do not open port 8000. The app listens on localhost only. Now create the reverse proxy at \/etc\/nginx\/sites-available\/aiapi:\n<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><p>\n  limit_req_zone $binary_remote_addr zone=aiapi:10m rate=5r\/s;\n<\/p><p>\n  server {\n<\/p><p>\n      listen 80;\n<\/p><p>\n      server_name api.example.com;\n<\/p><p>\n      client_max_body_size 20m;\n<\/p><p>\n      location \/ {\n<\/p><p>\n          limit_req zone=aiapi burst=10 nodelay;\n<\/p><p>\n          proxy_pass http:\/\/127.0.0.1:8000;\n<\/p><p>\n          proxy_http_version 1.1;\n<\/p><p>\n          proxy_set_header Host $host;\n<\/p><p>\n          proxy_set_header X-Real-IP $remote_addr;\n<\/p><p>\n          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n<\/p><p>\n          proxy_set_header X-Forwarded-Proto $scheme;\n<\/p><p>\n          proxy_connect_timeout 5s;\n<\/p><p>\n          proxy_read_timeout 120s;\n<\/p><p>\n          proxy_send_timeout 120s;\n<\/p><p>\n      }\n<\/p><p>\n  }\n<\/p><\/th><\/tr><\/thead><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"979\" height=\"771\" src=\"http:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-12.png\" alt=\"Nginx configuration for AI API deployment on Ubuntu with Python\" class=\"wp-image-18387\" srcset=\"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-12.png 979w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-12-300x236.png 300w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-12-768x605.png 768w\" sizes=\"(max-width: 979px) 100vw, 979px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Enable the site and reload:\n<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><p>\n  sudo ln -s \/etc\/nginx\/sites-available\/aiapi \/etc\/nginx\/sites-enabled\/\n<\/p><p>\n  sudo nginx -t &amp;&amp; sudo systemctl reload nginx\n<\/p><\/th><\/tr><\/thead><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"994\" height=\"260\" src=\"http:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-13.png\" alt=\"Ubuntu terminal showing Nginx configuration test and reload commands for AI API deployment.\" class=\"wp-image-18388\" srcset=\"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-13.png 994w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-13-300x78.png 300w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-13-768x201.png 768w\" sizes=\"(max-width: 994px) 100vw, 994px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Three settings in that file exist because of AI workloads. proxy_read_timeout defaults to 60 seconds, and a slow model can exceed that, which produces a 504 error. client_max_body_size defaults to 1 MB, so image or audio uploads fail with a 413 error until you raise it. And limit_req caps how often one client can hit an expensive endpoint.\n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Streaming Replies<\/strong>\n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  If your API streams tokens, Nginx will buffer the whole response by default and deliver it at the end. Add a separate location for the streaming route:\n<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><p>\n  location \/stream {\n<\/p><p>\n      proxy_pass http:\/\/127.0.0.1:8000;\n<\/p><p>\n      proxy_http_version 1.1;\n<\/p><p>\n      proxy_buffering off;\n<\/p><p>\n      proxy_read_timeout 300s;\n<\/p><p>\n  }\n<\/p><\/th><\/tr><\/thead><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\n  You can also send the response header X-Accel-Buffering: no from FastAPI&#8217;s StreamingResponse, and Nginx will honor it for that response.\n<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a id=\"post-18378-_fch02538qdl3\"><\/a><strong>Step 5: Enable HTTPS for Your Python AI API with Nginx<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Install Certbot and request a certificate:\n<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><p>\n  sudo apt install -y certbot python3-certbot-nginx\n<\/p><\/th><\/tr><\/thead><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"993\" height=\"534\" src=\"http:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-14.png\" alt=\"Ubuntu terminal showing apt install command for certbot python3-certbot-nginx\" class=\"wp-image-18389\" srcset=\"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-14.png 993w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-14-300x161.png 300w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-14-768x413.png 768w\" sizes=\"(max-width: 993px) 100vw, 993px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Now, request a certificate.\n<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><p>\n  sudo certbot &#8211;nginx -d api.example.com\n<\/p><\/th><\/tr><\/thead><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"991\" height=\"465\" src=\"http:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-15.png\" alt=\"Certbot command to register API certificate for api.example.com on Ubuntu.\" class=\"wp-image-18390\" srcset=\"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-15.png 991w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-15-300x141.png 300w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-15-768x360.png 768w\" sizes=\"(max-width: 991px) 100vw, 991px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Certbot edits the Nginx config to serve HTTPS and redirect HTTP, and the dry run confirms automatic renewal works. Test the live endpoint:\n<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><p>\n  curl -s https:\/\/api.example.com\/health\n<\/p><\/th><\/tr><\/thead><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"991\" height=\"98\" src=\"http:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-16.png\" alt=\"Ubuntu terminal showing curl command to check AI API health endpoint\" class=\"wp-image-18391\" srcset=\"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-16.png 991w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-16-300x30.png 300w, https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/word-image-18378-16-768x76.png 768w\" sizes=\"(max-width: 991px) 100vw, 991px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  That is all from deploying an API with Python and Nginx on Ubuntu.\n<\/p>\n\n\n\n<h2 id=\"When-Should-You-Deploy-an-AI-API-on-Kubernetes\" class=\"wp-block-heading\"><a id=\"post-18378-_742lwlgyb8cz\"><\/a><strong>When Should You Deploy an AI API on Kubernetes?<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\n  A single server is enough for many projects. If you later deploy Nginx on Kubernetes, note that the community ingress-nginx controller stopped receiving updates in March 2026, and the Kubernetes project recommends moving to the Gateway API. \n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Pick a current controller or Gateway implementation for new clusters. The lessons above still apply, including timeouts, body limits, and buffering for streams; only the configuration syntax changes. If you want to package the API and its dependencies into containers before moving toward orchestration, start with our guide to <a href=\"https:\/\/greenwebpage.com\/community\/how-to-install-docker-on-ubuntu-26-04\/\">install Docker on Ubuntu 26.04 LTS<\/a>.\n<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a id=\"post-18378-_s970epn7luh8\"><\/a><strong>How to Troubleshoot Common AI API Deployment Errors<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\n  To fix Linux server errors, you can explore our guide on <a href=\"https:\/\/greenwebpage.com\/community\/how-to-use-chatgpt-to-troubleshoot-linux-server-errors\/\">Using ChatGPT to Troubleshoot Linux Server Errors<\/a>.\n<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>502 Bad Gateway.<\/strong> The app is down or listening on a different port. Run systemctl status aiapi and curl http:\/\/127.0.0.1:8000\/health to troubleshoot <a href=\"https:\/\/greenwebpage.com\/community\/troubleshooting-nginx-502-bad-gateway-errors-on-linux\/\">Nginx 502 Bad Gateway errors on Linux<\/a>.<\/li>\n\n\n\n<li><strong>504 Gateway Timeout.<\/strong> The model is still loading, or inference exceeds proxy_read_timeout. Check the logs, then raise the timeout.<\/li>\n\n\n\n<li><strong>413 Request Entity Too Large.<\/strong> Raise client_max_body_size.<\/li>\n\n\n\n<li><strong>401 Invalid API key.<\/strong> Check the header name. FastAPI reads x-api-key, and the key must match \/etc\/aiapi.env.<\/li>\n\n\n\n<li><strong>The service restarts in a loop.<\/strong> Look for an out-of-memory kill with sudo dmesg -T | grep -i oom. Use a smaller model or fewer workers.<\/li>\n\n\n\n<li><strong>Permission denied writing the model cache.<\/strong> The aiapi user must own its home directory, \/opt\/aiapi.<br>  <\/li>\n<\/ul>\n\n\n\n<h2 id=\"Conclusion\" class=\"wp-block-heading\"><a id=\"post-18378-_r65a7guub13p\"><\/a><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Python API deployment for a machine learning model comes down to a few habits. Load the model once, keep the app on localhost, let systemd restart it, and let Nginx handle the public side. Then tune the parts that are specific to AI, namely worker count, timeouts, upload size, and buffering. Get those right and a modest Ubuntu server will serve a real model reliably.\n<\/p>\n\n\n\n<h2 id=\"faqs\" class=\"wp-block-heading\"><a id=\"post-18378-_pz07ugm2v2j5\"><\/a><strong>FAQs<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1. Can I deploy an AI API for free?<\/strong> \n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Yes, for small models. Run it locally while developing, or use a free-tier cloud VM for a light classifier or demo. Free servers are usually too small for large models, and their limits and terms change, so check them before you commit.\n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2. Should I use FastAPI or Flask for an AI model API?<\/strong> \n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  FastAPI is the better default. It validates requests, generates interactive docs, and runs plain functions in a thread pool. Flask is simpler and works well with Gunicorn if you only need a small endpoint. Both sit behind the same Nginx setup.\n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3. Why does Nginx return 504 Gateway Timeout when my model is slow?<\/strong> \n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Nginx waits 60 seconds by default for the app to respond. If inference takes longer, raise proxy_read_timeout. If you use Gunicorn, also raise its worker timeout, which defaults to 30 seconds.\n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>4. How many workers should a machine learning model API use?<\/strong> \n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  Start with one. Each worker loads a separate copy of the model, so memory use multiplies. Add workers only after confirming you have the RAM and that the extra processes actually improve throughput.\n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>5. Do I need Nginx in front of Uvicorn or Gunicorn?<\/strong> \n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n  For anything public, yes. Nginx handles HTTPS, rate limiting, request size limits, and slow clients, and it keeps your app server off the open internet. Running Uvicorn directly on a public port skips all of that.\n<\/p>\n\n    <div class=\"xs_social_share_widget xs_share_url after_content \t\tmain_content  wslu-style-1 wslu-share-box-shaped wslu-fill-colored wslu-none wslu-share-horizontal wslu-theme-font-no wslu-main_content\">\n\n\t\t\n        <ul>\n\t\t\t        <\/ul>\n    <\/div> \n","protected":false},"excerpt":{"rendered":"<p>A model that works in a notebook is not a service yet. To deploy an AI API to a server, you need something that loads the model once, answers HTTP requests, restarts when it crashes, and sits behind a proper web server with HTTPS. This guide walks through that setup on Ubuntu 26.04 using Python, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":18399,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[256,1124,226,1054],"class_list":["post-18378","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-ai","tag-api","tag-python","tag-ubuntu-26-04"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Deploy an AI API with Python and Nginx on Ubuntu - Greenwebpage Community<\/title>\n<meta name=\"description\" content=\"This guide also covers the problems that only show up with machine learning models, such as workers that each load their own copy of the model, timeouts on slow inference, and streamed replies that arrive all at once.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Deploy an AI API with Python and Nginx on Ubuntu - Greenwebpage Community\" \/>\n<meta property=\"og:description\" content=\"This guide also covers the problems that only show up with machine learning models, such as workers that each load their own copy of the model, timeouts on slow inference, and streamed replies that arrive all at once.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/\" \/>\n<meta property=\"og:site_name\" content=\"Greenwebpage Community\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-14T06:50:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-14T06:55:42+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/How-to-Deploy-an-AI-API-with-Python-and-Nginx-on-Ubuntu-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"600\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Karim Buzdar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Karim Buzdar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/\"},\"author\":{\"name\":\"Karim Buzdar\",\"@id\":\"https:\/\/greenwebpage.com\/community\/#\/schema\/person\/467c100c1d017bc081473ee0440680c8\"},\"headline\":\"How to Deploy an AI API with Python and Nginx on Ubuntu\",\"datePublished\":\"2026-10-14T06:50:12+00:00\",\"dateModified\":\"2026-10-14T06:55:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/\"},\"wordCount\":2035,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/greenwebpage.com\/community\/#organization\"},\"image\":{\"@id\":\"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/How-to-Deploy-an-AI-API-with-Python-and-Nginx-on-Ubuntu-1.jpg\",\"keywords\":[\"AI\",\"API\",\"Python\",\"Ubuntu 26.04\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/\",\"url\":\"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/\",\"name\":\"How to Deploy an AI API with Python and Nginx on Ubuntu - Greenwebpage Community\",\"isPartOf\":{\"@id\":\"https:\/\/greenwebpage.com\/community\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/How-to-Deploy-an-AI-API-with-Python-and-Nginx-on-Ubuntu-1.jpg\",\"datePublished\":\"2026-10-14T06:50:12+00:00\",\"dateModified\":\"2026-10-14T06:55:42+00:00\",\"description\":\"This guide also covers the problems that only show up with machine learning models, such as workers that each load their own copy of the model, timeouts on slow inference, and streamed replies that arrive all at once.\",\"breadcrumb\":{\"@id\":\"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/#primaryimage\",\"url\":\"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/How-to-Deploy-an-AI-API-with-Python-and-Nginx-on-Ubuntu-1.jpg\",\"contentUrl\":\"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/How-to-Deploy-an-AI-API-with-Python-and-Nginx-on-Ubuntu-1.jpg\",\"width\":1020,\"height\":600,\"caption\":\"Diagram illustrating AI API deployment with Python, Nginx, and Ubuntu.\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/greenwebpage.com\/community\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Deploy an AI API with Python and Nginx on Ubuntu\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/greenwebpage.com\/community\/#website\",\"url\":\"https:\/\/greenwebpage.com\/community\/\",\"name\":\"Greenwebpage Community\",\"description\":\"Get online in three steps with our wide range of web hosting solutions. Choose from professional business to enterprise options designed to meet your needs.\",\"publisher\":{\"@id\":\"https:\/\/greenwebpage.com\/community\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/greenwebpage.com\/community\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/greenwebpage.com\/community\/#organization\",\"name\":\"Greenwebpage Community\",\"url\":\"https:\/\/greenwebpage.com\/community\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/greenwebpage.com\/community\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2023\/10\/cropped-logomic.png\",\"contentUrl\":\"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2023\/10\/cropped-logomic.png\",\"width\":512,\"height\":512,\"caption\":\"Greenwebpage Community\"},\"image\":{\"@id\":\"https:\/\/greenwebpage.com\/community\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/greenwebpage.com\/community\/#\/schema\/person\/467c100c1d017bc081473ee0440680c8\",\"name\":\"Karim Buzdar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/greenwebpage.com\/community\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0628fcbcddd9bc5486245d2cf4a904dbcdeac9ad6c3098f49237094e9d513d0c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0628fcbcddd9bc5486245d2cf4a904dbcdeac9ad6c3098f49237094e9d513d0c?s=96&d=mm&r=g\",\"caption\":\"Karim Buzdar\"},\"sameAs\":[\"https:\/\/greenwebpage.com\"],\"url\":\"https:\/\/greenwebpage.com\/community\/author\/karim\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Deploy an AI API with Python and Nginx on Ubuntu - Greenwebpage Community","description":"This guide also covers the problems that only show up with machine learning models, such as workers that each load their own copy of the model, timeouts on slow inference, and streamed replies that arrive all at once.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/","og_locale":"en_US","og_type":"article","og_title":"How to Deploy an AI API with Python and Nginx on Ubuntu - Greenwebpage Community","og_description":"This guide also covers the problems that only show up with machine learning models, such as workers that each load their own copy of the model, timeouts on slow inference, and streamed replies that arrive all at once.","og_url":"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/","og_site_name":"Greenwebpage Community","article_published_time":"2026-10-14T06:50:12+00:00","article_modified_time":"2026-10-14T06:55:42+00:00","og_image":[{"width":1020,"height":600,"url":"http:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/How-to-Deploy-an-AI-API-with-Python-and-Nginx-on-Ubuntu-1.jpg","type":"image\/jpeg"}],"author":"Karim Buzdar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Karim Buzdar","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/#article","isPartOf":{"@id":"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/"},"author":{"name":"Karim Buzdar","@id":"https:\/\/greenwebpage.com\/community\/#\/schema\/person\/467c100c1d017bc081473ee0440680c8"},"headline":"How to Deploy an AI API with Python and Nginx on Ubuntu","datePublished":"2026-10-14T06:50:12+00:00","dateModified":"2026-10-14T06:55:42+00:00","mainEntityOfPage":{"@id":"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/"},"wordCount":2035,"commentCount":0,"publisher":{"@id":"https:\/\/greenwebpage.com\/community\/#organization"},"image":{"@id":"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/#primaryimage"},"thumbnailUrl":"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/How-to-Deploy-an-AI-API-with-Python-and-Nginx-on-Ubuntu-1.jpg","keywords":["AI","API","Python","Ubuntu 26.04"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/","url":"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/","name":"How to Deploy an AI API with Python and Nginx on Ubuntu - Greenwebpage Community","isPartOf":{"@id":"https:\/\/greenwebpage.com\/community\/#website"},"primaryImageOfPage":{"@id":"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/#primaryimage"},"image":{"@id":"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/#primaryimage"},"thumbnailUrl":"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/How-to-Deploy-an-AI-API-with-Python-and-Nginx-on-Ubuntu-1.jpg","datePublished":"2026-10-14T06:50:12+00:00","dateModified":"2026-10-14T06:55:42+00:00","description":"This guide also covers the problems that only show up with machine learning models, such as workers that each load their own copy of the model, timeouts on slow inference, and streamed replies that arrive all at once.","breadcrumb":{"@id":"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/#primaryimage","url":"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/How-to-Deploy-an-AI-API-with-Python-and-Nginx-on-Ubuntu-1.jpg","contentUrl":"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2026\/10\/How-to-Deploy-an-AI-API-with-Python-and-Nginx-on-Ubuntu-1.jpg","width":1020,"height":600,"caption":"Diagram illustrating AI API deployment with Python, Nginx, and Ubuntu."},{"@type":"BreadcrumbList","@id":"https:\/\/greenwebpage.com\/community\/how-to-deploy-an-ai-api-with-python-and-nginx-on-ubuntu\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/greenwebpage.com\/community\/"},{"@type":"ListItem","position":2,"name":"How to Deploy an AI API with Python and Nginx on Ubuntu"}]},{"@type":"WebSite","@id":"https:\/\/greenwebpage.com\/community\/#website","url":"https:\/\/greenwebpage.com\/community\/","name":"Greenwebpage Community","description":"Get online in three steps with our wide range of web hosting solutions. Choose from professional business to enterprise options designed to meet your needs.","publisher":{"@id":"https:\/\/greenwebpage.com\/community\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/greenwebpage.com\/community\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/greenwebpage.com\/community\/#organization","name":"Greenwebpage Community","url":"https:\/\/greenwebpage.com\/community\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/greenwebpage.com\/community\/#\/schema\/logo\/image\/","url":"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2023\/10\/cropped-logomic.png","contentUrl":"https:\/\/greenwebpage.com\/community\/wp-content\/uploads\/2023\/10\/cropped-logomic.png","width":512,"height":512,"caption":"Greenwebpage Community"},"image":{"@id":"https:\/\/greenwebpage.com\/community\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/greenwebpage.com\/community\/#\/schema\/person\/467c100c1d017bc081473ee0440680c8","name":"Karim Buzdar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/greenwebpage.com\/community\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0628fcbcddd9bc5486245d2cf4a904dbcdeac9ad6c3098f49237094e9d513d0c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0628fcbcddd9bc5486245d2cf4a904dbcdeac9ad6c3098f49237094e9d513d0c?s=96&d=mm&r=g","caption":"Karim Buzdar"},"sameAs":["https:\/\/greenwebpage.com"],"url":"https:\/\/greenwebpage.com\/community\/author\/karim\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/greenwebpage.com\/community\/wp-json\/wp\/v2\/posts\/18378","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/greenwebpage.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/greenwebpage.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/greenwebpage.com\/community\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/greenwebpage.com\/community\/wp-json\/wp\/v2\/comments?post=18378"}],"version-history":[{"count":6,"href":"https:\/\/greenwebpage.com\/community\/wp-json\/wp\/v2\/posts\/18378\/revisions"}],"predecessor-version":[{"id":18397,"href":"https:\/\/greenwebpage.com\/community\/wp-json\/wp\/v2\/posts\/18378\/revisions\/18397"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/greenwebpage.com\/community\/wp-json\/wp\/v2\/media\/18399"}],"wp:attachment":[{"href":"https:\/\/greenwebpage.com\/community\/wp-json\/wp\/v2\/media?parent=18378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/greenwebpage.com\/community\/wp-json\/wp\/v2\/categories?post=18378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/greenwebpage.com\/community\/wp-json\/wp\/v2\/tags?post=18378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}