Full Web Apps with FastAPI 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 in nginx so you can talk to our web api from
0:05 the outside. So this is super easy, we're gonna install nginx, I'll go back here
0:11 and off it goes, it's going to stall a bunch of stuff, nginx is
0:14 a fantastic front end web server.
0:16 Do all sorts of cool things with it.
0:17 Okay, now we're gonna need to, just
0:21 like before, copy a configuration file over
0:23 cause 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 this is not a real thing with a real domain,
0:36 it's just a simple demo, I'm gonna just put the IP
0:39 address here as well, let me actually get the IP address, copy it 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,
0:52 we're gonna do that. So the thing basically says I'm gonna set up a server
0:54 to 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 someone asks for /static, give them files out of that folder,
1:05 Otherwise, anything else go and proxy_pass over to port 80 on localhost.
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 gotta 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/repo
1:32 We'll git pull, you can see our server change there, and then we're pretty much set.
1:38 We just gotta copy
1:39 this file, which you 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 we're going to tell it to start when the server starts and then we're
1:59 gonna restart it so it re reads the config files.
2:01 All those things are good, now if we try http,
2:04 remember that localhost:80, port 8000.
2:06 What if we just do local host?
2:07 This will talk to nginx. Oh oh oh,
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 gotta 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 to server.
2:31 So what does it mean to have a domain name? You can 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 all right, we could have reports.talkpython.fm,
2:53 whetherapi.talkpython.fm and so on and so on.
2:56 Whatever ones you wanna point to the server, as long as there in there,
2:59 the rest will just flow through the system.
3:01 So super cool, we got this working. Let's just check that our API
3:05 does its thing. Yeah,
3:06 we went out to OpenWeatherMap,
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 and
3:24 we picked it. Very, very cool,
3:26 so our job here is basically done.