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