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