Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Lecture: The setup script and config files
0:00 Now it's time to start working with our code.
0:01 We're in another chapter in our GitHub repo
0:04 Chapter 15_deploy
0:06 and of course I made a copy of the code
0:08 we have starter and a final
0:09 and these are actually not super different
0:11 but there's going to be a few files that we change.
0:13 Now, in order for us to get things to work in Linux
0:16 what we have to do is create
0:17 a couple of configuration files.
0:19 We need to talk about how to run uWSGI
0:21 which is going to run our Python code
0:23 and how to run Nginx
0:24 which is going to be the frontend server
0:26 that's going to communicate through some back channels
0:28 to uWSGI
0:30 how are we going to set up the server, and so on.
0:32 Watching me type that's not super interesting
0:33 so I'm going to paste in some files
0:35 and then we'll talk through them.
0:36 So we now have a server chapter
0:38 located next to pi, pi_org, and it has three files.
0:42 It has an Nginx file
0:44 and this is going to be our Nginx server configuration.
0:47 We'll talk through that in a minute.
0:48 It also has a unit file here.
0:51 A unit file is like a background daemon or service.
0:55 It's like a Windows service
0:56 if you're coming from the Windows world.
0:57 It just runs when the system starts.
0:59 When Linux boots up
1:00 this is just always going to run this code.
1:02 So, this is going to have the configuration
1:04 to run uWSGI and all the stuff
1:07 we need to run our web app.
1:09 We'll talk more about that, as well.
1:11 The first thing we're going to work with
1:12 is going through the script that we have here.
1:15 So, this script, basically, is all the steps that you need
1:18 to apply to the server in order to get into a nice
1:21 working state, get all the dependencies set up
1:25 firewalls, and some other things like that.
1:28 So, what we're going to do is we're going through this
1:29 step by step. Now, you mostly could run it.
1:31 I think there's a couple of lines
1:33 that you have to be a little bit interactive
1:36 and also, down here
1:37 you have to fill out your details and so on
1:40 but, more or less, you could just run the script.
1:41 We're not going to do that.
1:42 We're going to talk through it piece by piece
1:44 and you probably want to run it piece by piece, as well
1:46 to see if anything fails, but it's all in one script here.
1:49 So, this is going to be real helpful for us
1:51 to get all set up.
1:52 Now, final thing: the way that our code is written so far
1:55 does not really make uWSGI, this one
1:59 does not make uWSGI happy.
2:01 The way that it imports stuff
2:03 it tries to our project
2:05 and it just imports the application out of the module.
2:11 It doesn't run the module.
2:13 So, what does that mean?
2:14 Over here, under app.py
2:17 we have some code that comes along
2:21 and it says, really, down here at the bottom
2:23 what we had before is just this
2:24 and what it was doing, it says, If you run this file
2:27 as we have been the whole time, then run the main.
2:30 Otherwise, do nothing. Just import it.
2:33 Well, uWSGI doesn't work that way.
2:35 What it does is it's going to import this thing
2:39 basically, import this file
2:40 and then grab hold of this and run it with its own settings
2:44 and that means this configure bit isn't getting called here.
2:47 So, in particular, the blueprints and the database
2:49 are not getting set up under uWSGI.
2:52 So, what we can do is we can say
2:55 If you run it like this, actually start the program
2:58 start the server, the dev server.
3:00 Otherwise, just configure it.
3:02 So, this little tweak that I added here is necessary
3:06 for running our code under uWSGI.
3:08 There's other ways, as well, but this is how we're doing it.