Modern APIs with FastAPI and Python Transcripts
Chapter: Deploying FastAPI on Linux with gunicorn and nginx
Lecture: Installing and running nginx
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
The last thing we need to do really, is to set up NGINX so you can talk to our web API from
0:06
the outside. So this is super easy, we're gonna install NGINX, go back here and off it goes. It's going to install a bunch of stuff. NGINX is
0:15
a fantastic front end Web server, can do all sorts of cool things with it. Okay, Now we're going to need to, just
0:22
like before, copy a configuration file over because that's how this all works. And in order for this to work,
0:27
what we want to do is we're gonna have a domain name. Imagine Like "weatherapi.talkPython.com" or something,
0:34
but because it's not a real thing with the real domain, just a simple demo, I'm going to just put the IP
0:40
address here as well, let me actually get IP address copied here. You wouldn't normally do this, probably,
0:48
but because we don't have a domain name and I don't want to mess with DNS and wait for it to propagate, we're
0:53
gonna do that. So the thing basically says we're gonna set up a server to listen on port 80 to the domain and maybe IP addresses.
0:59
There's a set of static files that live here. So if somebody asks for "/static", give them files out of that folder.
1:06
Otherwise, anything else go and proxy pass over to Port 80 on local host. That means talk to Gunicorn, which will fan it out to the uvicorn workers,
1:16
and that's about it. There's really not a whole lot else going on, but notice this change. We've got to get this up to the server.
1:25
Let's go commit that and then push that to GitHub and then we'll go here and we'll cd to our apps, app
1:33
repo. We'll git pull. We can see our server change there. And then we're pretty much set. We just gotta copy this file. We might as well copy paste,
1:44
so you don't get it wrong. And then we need to tell it also, we want to remove the default just NGINX's installed file and make it use ours.
1:53
And then we're going to tell it to start when the server starts and then we're going to restart it so it re-reads the config files.
2:02
All those things are good. Now, if we try http, remember that local host 80? Port 8000? What if we just do local host? This will talk to NGINX.
2:10
It's working! So, go over here, I need my IP address back. Let's go on the internet and see if this works. Fingers crossed. Yes! Look at that!
2:21
Our server is up and running, and all we got to do is go to the DNS and put a proper domain
2:26
name there, and we would have our thing fully on the website. Up and running on our Ubuntu server.
2:32
So what does it mean to have a domain name and just go to wherever your DNS is and tell it that this IP address is where that domain name goes
2:40
And make sure that that domain name is right here. You can have multiple domain names, like you can have
2:51
Alright, we could have "reports.talkpython.fm", "weatherapi.talkpython.fm" and so on and so on.
2:57
Whatever ones you wanna point at the server, long as they're in there, the rest will just flow through the system. So, super cool. We got this working.
3:04
Let's just check that our API does its thing. Yeah, we went out to open weather map. We've got it. Put it here.
3:11
Now, of course, it's using this caching. Let's actually do an inspect element. Look at the network. Make a call again, look at that, 45 milliseconds.
3:19
39 milliseconds. That's all the way to the server across the Internet. Down to San Francisco or wherever it was we picked it. Very, very cool.
3:27
So our job here is basically done.