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.
0:03 And if we try to request it, it's 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 over 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 going to run four copies of uvicorn as workers.
0:38 I talked about having multiple worker processes
0:40 where our Python code, 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 a 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 because sometimes this is little wonky.
1:05 Set a log file there for access.
1:08 Set an error log file there,
1:09 then run it as the user "apiuser", it's a lot,
1:13 right? But that has to be all one line
1:15 because of the way it's issued.
1:17 So I'm gonna just copy that. Now,
1:18 you want to 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 going to do something is a good
1:26 idea. And it doesn't. Why?
1:29 Because there's no Gunicorn.
1:30 So we got to go back over here and
1:32 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 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, installed slightly,
1:54 slightly quicker. Now with those set up,
1:57 let's go and try to run our command here. We're gonna run Gunicorn
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 local host 8000.
2:15 Nice. See, for example,
2:16 color coding. You can see the header information,
2:20 all kinds of good stuff that's better than curl,
2:21 but still working. Good. So let's move 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, especially if
2:31 you're getting errors is 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 you don't see that. But at least you see the start up process.
2:44 Something goes wrong, often you'll see the error right there.
2:46 We'll see 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 got to 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, because right now if we look, not there. If we try this, so
3:14 this is 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" and 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 have 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 local host.
3:53 Yes, alright. We rebooted 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 local host. 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.