Full Web Apps with FastAPI Transcripts
Chapter: Deploying FastAPI on Linux with gunicorn and nginx
Lecture: Gunicorn as Systemd unit
0:00 Now we want our web app to work all the time on this server. And if we try to request it, mmm not running, why? Because I logged out,
0:07 that's silly. We just want it to be part of the server. And so what we're gonna use is something called a unit file over here, to set
0:15 up a systemd server. So this is going to tell the system: run this command when you boot, basically. So it says, go to this directory,
0:24 run this command. And let's, just let me do some line breaks here to make this mean stuff. So it says we're gonna run gunicorn,
0:30 we're gonna bind to that address. We're gonna run four copies of you uvicorn as workers. I talked about having multiple worker processes
0:41 where our Python code and our FastAPI code is actually gonna run. So if you want 10, you put 10 there, 4 is fine though.
0:48 And we're gonna run not WSGI gunicorn stuff, but ASGI uvicorn ones. We're gonna go to the main module and pull out the app
0:58 api instance that's configured. Tell the process it's called that, make sure that that's the working directory cause sometimes this is little wonky.
1:06 Set a log file there for access, set the error log file there, then run as the user apiuser. It's a lot, right? But that has to be all one line
1:16 because it, the way it's issued. So I'm gonna just copy that. Now, you wanna make sure this is going to work before you try to set it
1:22 up as a system daemon. Okay, so just making sure that line is gonna do something is a good idea, and it doesn't. Why? cause there's no gunicorn.
1:31 So we've got to go back over here and we've got to install gunicorn, and gunicorn and uvicorn on the server require these two libraries as well.
1:41 So we're just gonna do those three things. Ah, looks like a pip install wheel would have made that a little bit nicer,
1:50 but I'll change the script for you all, here we go. Anyway, all those installed slightly, slightly quicker. Now with those set up,
1:58 let's go and try to run our command here. Run uvicorn this, what happened? Nothing. And that's good, all the output is going to these log files.
2:06 Okay, so let's just make sure that we can connect over here and now we'll be able to http localhost:8000. Nice. See, for example,
2:17 color coding, you see the header information, all kinds of good stuff, it's better than curl,
2:22 but still working. Good. So let's get that to the side for a minute and we get out of here. It looks like it works.
2:29 One thing that may be helpful sometimes, specially if you're getting errors is if you just try it without the logging.
2:35 That way you see all the messages right here, and if we go back and do this again,
2:42 ah you still don't see that, but at least you see the start up process. If something goes wrong, often, you'll see the error right there.
2:47 Ctrl+C to get out. So it looks like that command is working, that means it's likely going to work if we set it up as a system process
2:55 instead of just typing it in. So what do we gotta do to do that? We just copy that file over here, like this.
3:03 And then we say we want to use the system control to try to start the thing called weather. Right now, if we look, not there. If we try this, so
3:15 this has kicked off the thing as a server in the background, now it's just running. Yes, it works. How cool is that? And you can ask, go up here,
3:22 you can ask "status", what's it doing? Look, there's four worker processes all running over here,
3:29 that's great. Now we also want to say: do that when I reboot, not just this one time. It's the way we do that is, we say enable,
3:37 perfect. And now we could do a test, reboot and just make sure when we get back, things are still hanging together. So give that about 10 seconds,
3:44 you don't have to do this, it would've been fine, but just, you know, peace of mind to make sure that it really is set.
3:51 And it's back. Let's do our http localhost Yes, all right. We re booted the server and now our service is running
3:57 still, so perfect. We've now set up gunicorn and uvicorn to always run our app. But you still can't get to it from the outside.
4:06 remember it's listening on localhost but this is a huge start. Actually, this is the hardest bit of it all.
4:11 If we get this working, we're pretty golden.