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