Full Web Apps with FastAPI Transcripts
Chapter: Deploying FastAPI on Linux with gunicorn and nginx
Lecture: Gunicorn as Systemd unit

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