In this tutorial, I will guide you through setting up a FastAPI server on a remote machine, configuring Nginx as a reverse proxy, and enabling HTTPS using Certbot.
Step 1: Setting Up a Non-sudo User on the Remote Machine
Create a Non-sudo User:
sudo adduser karthik
Switch to the New User:
su karthik
Create SSH Directory and Authorized Keys:
cd ~/ mkdir .ssh touch .ssh/authorized_keys
Append your local machine's SSH public key to
~/.ssh/authorized_keys
on the remote machine.
Step 2: Installing Nginx
Install Nginx:
sudo apt-get update sudo apt-get install nginx
Verify Nginx Installation: Open a web browser and enter your remote machine's IP address. You should see the Nginx welcome page.
Step 3: Configuring Nginx as a Reverse Proxy
Edit Nginx Configuration:
sudo nano /etc/nginx/sites-available/fastapi
Add the following configuration (use a simple webserver for testing this):
server { listen 80; server_name your_domain; location / { proxy_pass http://127.0.0.1:8000; # FastAPI server address proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
Enable the Site:
sudo ln -s /etc/nginx/sites-available/fastapi /etc/nginx/sites-enabled/
Test Nginx Configuration:
sudo nginx -t
Restart Nginx:
sudo systemctl restart nginx
Step 4: Enabling HTTPS with Certbot
Note : You need to create a A type DNS Record from your domain registar pointing to your static public ip address before this step, otherwise certbot will fail to generate certificates.
Install Certbot for Nginx:
sudo apt-get install python3-certbot-nginx
Obtain SSL Certificate:
sudo certbot certonly --nginx -d your_domain_or_subdomain
Update Nginx Configuration for HTTPS: Update
/etc/nginx/sites-available/fastapi
with HTTPS settings:server { listen 80; listen [::]:80; server_name your_domain; # Redirect all HTTP traffic to HTTPS if ($host = $server_name) { return 301 https://$host$request_uri; } return 404; } server { listen 443 ssl; listen [::]:443 ssl; server_name your_domain; ssl_certificate /etc/letsencrypt/live/your_domain/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/your_domain/privkey.pem; include /etc/letsencrypt/options-ssl-nginx.conf; ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; keepalive_timeout 5; client_max_body_size 1G; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
Test Nginx Configuration and Restart:
sudo nginx -t sudo systemctl restart nginx
Step 5: Deploying FastAPI Server
Install Micromamba and Create Virtual Environment:
"${SHELL}" <(curl -L micro.mamba.pm/install.sh) micromamba create -n myenv python=3.8 micromamba activate myenv
Install Dependencies and Start FastAPI Server:
pip install fastapi uvicorn gunicorn mkdir ~/Projects/myproject cd ~/Projects/myproject touch main.py
Example
main.py
:from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"}
Create a Systemd Service for the FastAPI Server: Create
/etc/systemd/system/fastapi.service
:[Unit] Description=FastAPI Service After=network.target [Service] User=karthik Group=karthik WorkingDirectory=/home/karthik/Projects/myproject ExecStart=/home/karthik/.local/bin/micromamba run -n myenv gunicorn -w 1 -k uvicorn.workers.UvicornWorker main:app -b 127.0.0.1:8000 Restart=always [Install] WantedBy=multi-user.target
Enable and Start the Service:
sudo systemctl daemon-reload sudo systemctl enable fastapi.service sudo systemctl start fastapi.service
Conclusion
You have successfully set up a FastAPI server on a remote machine, configured Nginx as a reverse proxy, and secured it with HTTPS using Certbot. Your FastAPI application is now accessible via your domain over a secure connection.
Feel free to customize your FastAPI application and Nginx configuration further based on your project requirements.
This tutorial provides a comprehensive guide from setting up basic server access to deploying a FastAPI application with Nginx. Happy coding!
Comments
Post a Comment