Modern APIs with FastAPI and Python 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. And if we try to request it, it's 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 over 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 going to run four copies of uvicorn as workers. I talked about having multiple worker processes
0:41 where our Python code, 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 a 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, because sometimes this is little wonky.
1:06 Set a log file there for access. Set an error log file there, then run it as the user "apiuser", it's a lot, right? But that has to be all one line
1:16 because of the way it's issued. So I'm gonna just copy that. Now, you want to 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 going to do something is a good idea. And it doesn't. Why? Because there's no Gunicorn.
1:31 So we 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. 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, installed slightly, slightly quicker. Now with those set up,
1:58 let's go and try to run our command here. We're gonna run Gunicorn this. What happened? Nothing.
2:03 And that's good. All the output is going to these log files, okay? So let's just make sure that we can connect over here and now we'll
2:11 be able to http local host 8000. Nice. See, for example, color coding. You can see the header information,
2:21 all kinds of good stuff that's better than curl, but still working. Good. So let's move that to the side for a minute and
2:27 we get out of here. It looks like it works. One thing that may be helpful sometimes, especially if
2:32 you're getting errors is you just try it without the logging. That way you see all the messages right here. And if we go back and do this again,
2:42 you don't see that. But at least you see the start up process. Something goes wrong, often you'll see the error right there.
2:47 We'll see 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 got to 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, because right now if we look, not there. If we try this, so
3:15 this is 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" and 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 have 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 local host. Yes, alright. We rebooted 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 local host. 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.


Talk Python's Mastodon Michael Kennedy's Mastodon