Building data-driven web apps with Flask and SQLAlchemy Transcripts
Chapter: Deployment
Lecture: Running under uWSGI

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We saw that we have our code on the server
0:02 and that it runs, if you just run it straight with Python
0:06 in the development server basically.
0:08 Now the next step is going to involve uWSGI.
0:11 Remember, uWSGI is the layer
0:13 that actually in production, properly runs our code
0:17 our Python code. So what we're going to need to do
0:19 is we're going to work on this file
0:22 called a service file or a unit file.
0:24 So if we go over here and look at it
0:26 what it does is it basically has a title.
0:28 This is uWSGI PyPI.
0:31 It's going to run the uWSGI command
0:34 with the virtual directory in this
0:36 run in four processes on port 500 and so on.
0:39 It's going to run in that folder
0:42 and it'll restart if there's an error, things like that.
0:45 So what we want to do is copy this file over and run it.
0:48 the best thing to do however though
0:49 is not try that right away
0:52 because if it doesn't work
0:53 you're hunting around for log files
0:55 and other weird stuff that you're trying
0:57 to figure out what's going on.
0:58 So the best thing to do is just copy this right here.
1:02 This is the command we're going to run.
1:03 It's going to run with that in the path.
1:06 And it's going to import somebody called WSGI
1:09 which we need to create that really quick.
1:11 So over here at the top
1:15 we're going to create a file
1:16 and it's going to say from pypi_org.app import app.
1:21 We're also going to need main.
1:23 And here we'll just do our little main trick will run main.
1:26 But only if we're happening to run it as the app.
1:30 We're not going to run it that way.
1:31 What we really want to do is just export this.
1:34 So let's commit this really quick.
1:41 All right, perfect. Now over here we do an
1:45 we'll see you that it's not here yet
1:47 but it would do a get.
1:49 Should come down and here's our WSGI.
1:54 So we're going to go to there
1:55 and import the app instance from there.
1:58 And we're going to just mount that to / okay.
2:00 So what we want to do
2:01 is just run this command over there
2:03 and make sure that it works while we're in this directory.
2:07 So we're in the right directory.
2:10 Now we're just going to try the command
2:12 and either is going to work or we're going to get an error.
2:14 Let's see. Oh, pretty good. It looks like it's working.
2:19 Here we can see our app. We put some little printouts.
2:21 It's configured in the flask app
2:23 and registry and blueprints and so on.
2:24 So this is all good.
2:26 How do we see if it worked?
2:27 Well, we have to go do a request against it
2:29 and we can't do it here 'cause if we exit out
2:31 it's going to stop it.
2:32 So let's create another terminal.
2:34 Go in there.
2:35 All right, let's use our little HTTP utility
2:37 that we created.
2:39 I'm going to go against localhost:5000.
2:43 Remember our command that we issued over here
2:47 we're listening on port 5000 not 5006
2:50 like we were in our devops.
2:51 So let's see what we get.
2:53 Oh it would be super If I could spell correctly.
2:55 Here we looked like we lost in H on the way.
2:58 Makes a request what do we get?
2:59 Yes, all right.
3:00 You can see over here, the request came in.
3:02 I went pretty quick for the first time
3:05 and the response code is 200 right there.
3:09 That's good.
3:10 And of course, we can see our content.
3:11 Here's our little fake site.
3:13 So it looks like running uWSGI
3:16 outside of the system daemon
3:18 just directly with that configure we pass along is working.
3:21 That's a super good sign
3:22 and definitely something you want to do
3:24 before you try to actually run it as a system daemon.