Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Deployment
Lecture: Configure the server
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 through our little script here, and configure the server. The first thing I actually want to do is commit these files.
0:08
So let's do a quick commit here and say Commit and push those over. The reason is, we're going to use get to get our files onto the server.
0:20
And if these are not checked in then obviously they won't show up and we won't be able to copy them to the right location.
0:26
So our goal is going to be basically to work our way down this file. Like I said, don't run it all at once 'cause in case something fails
0:33
you might not notice it or whatever. So we've already done this. This was our right away, log in and do our update.
0:39
The next thing, let's go ahead and log in over here. That looks like we are. Let me just log out and log back in real quick. So, notice our shell here.
0:48
We're using Bash. Now Bash is okay but I actually don't like it all that much. I like Oh My Zsh so much better. So I'm going to install Z-Shell
1:01
and then on my Z-Shell, get those. Again, it's not required but it just makes working with the server nicer.
1:13
Here we go. Notice our cool prompt has changed and we get better history of commands we've run so we can more easily rerun them
1:19
and find what we've been up to. Now, with that in place let's just start going through and installing some dependencies.
1:26
So first we're going to need build-essential for installing some packages potentially. We need get to get our files there having zip is always nice.
1:34
Nload is a utility we can use. We're looking at network traffic to see that requests are coming in and out.
1:39
And tree is just a way to look at directory structure. So we can run those. All right, super. It looked like that succeeded. Now we need to have
1:51
there's simple things that are not here. Like if I try to type pip3 it's like well, sorry. pip3 is not a command. pip as well.
2:00
Having the ability to pip and solve something that seems pretty fundamental so let's go and add stuff like that.
2:09
There we go. It looks like pip does something now. Cool. Now we're also going to want to have Nginx. Nginx is a system-wide thing installed by app
2:17
so we're going to do that. Now, this next set of dependencies here that we're going to install this is so that we can able gzip support in uWSGI.
2:29
This let's us compress some of our files and make the response a little bit better so let's go ahead and have that in there.
2:36
All right, so that's pretty much it for the dependencies. The next thing I want to install is something called Fail2ban.
2:41
And when we check the box for using SSH certificates when we set up the server in Digital Ocean that disabled logins with a password
2:51
so this might not technically be necessary but what this will do is it'll watch and if people try to log in over SSH and they fail
2:59
we're going to eventually blacklist them and not let them even attempt to log in. All right, so we could potentially
3:05
accidentally turn that feature back on or they could start trying to send certificates over or something like that.
3:11
So let's set this up to make sure that people who try to pound on the server get turned away. At least for SSH.
3:18
Next, we only want to expose the ports that are necessary so SSH so we can manage our server. 80 for the base request, and 443 for SSH.
3:28
So let's go use this thing called uncomplicated firewall to enable those and then it's off by default so let's turn it on.
3:35
Now it says, warning, you may not ever be able to come back if you forgot that line. We didn't, so we should be okay but let's just double check here.
3:44
If we go and log out, and then log out of both shells just that one time. So if we go back, hey we can get back in
3:53
our Z-Shell is here too, that's good. We can get back in that means the firewall is letting us through. That's all good. Now, we're going to use get
4:01
and we're just going to type in our password in the server. So, in order to make sure that we have to do that as little as possible
4:07
I'm going to set up this command that says basically remember my login credentials for a month under my account if I log in and type them in.
4:16
So that's good. And then the other thing is if you ever try to push back, do a push from your server it won't let you unless you set these two things.
4:23
Let's go ahead and set your email like michael@talkpython.fm And we also have to set our name. Here we go, those two are set, that's good.
4:35
So now, get is up and ready to go. We're also going to want to have a place where we put out web app of course
4:43
so we're going to make this structure here under /apps/. We're going to do a clone there. We're also going to have a log section.
4:51
We didn't actually set up logging in our server so it's not. I'm putting them here for you to add logging to your server and write them there.
5:00
We're not going to make too much use of 'em. But over here, we now in this little section, like so. That's cool. The other thing is we're going to want
5:09
to create a virtual directory. Now, the server is dedicated to singularly dedicated at the moment to running this web app.
5:17
So you could say, maybe we could just update the system Python and system packages by using a virtual environment.
5:24
If something goes wrong, it's easy for us to delete that environment, recreate it rerun the dependencies, pip install -r requirements.txt.
5:33
Things like that and it will just come back to life. So this means it's easier if we do something wrong to recreate what what we need.
5:40
So let's go ahead and create this virtual environment here. Let's activate it. Like so. Notice our prompt changes.
5:51
And if we ask which Python it's now this one so that's good. And we can go ahead and install some tools like so.
6:02
All right, so we want to make sure that we have the latest pip and setuptools because notice we had a incredibly out of date pip 9
6:11
and now we're in pip 19. How ridiculous is that I really just don't get why Python doesn't upgrade this for us
6:18
when we're creating a virtual environments. But it doesn't, so make sure you don't forget that. That way, we're all good.
6:23
Now, we're going to install a couple of utilities that are Python based so they go in our virtual environment not through app one, is httpie.
6:31
This is kind of replacement of recurral, or wget. It let's us test our web app and it's just nicer
6:36
and it happens to be Python based so we put it over here. Glances is a cool GUI light program that let's us understand the apps
6:43
that are running our server. So now we have this, we can run Glances and have a better look at our server. This is cool.
6:50
So here you can see the cp load. Looks high, like Glances was, figuring out what's up. But now it's super low. Our memory usage is really good.
6:58
We're releasing 280 megs. We got 700. We can sort by memory, by person m's, c, cpu by pressing c. And over here we have things like Glances, Fail2Ban.
7:07
Stuff that you would expect. Our web app is not here, of course. We haven't started it. We'll get there in a little bit. We also need to install uWSGI.
7:15
This is a Python thing. When you install it, it actually takes a little awhile. It does a bunch of compilation and stuff in the setup.py.
7:23
So this takes awhile, but then eventually it will be set up inside our virtual environment. And there you have it. The server is ready to go.
7:30
You can type WSGI just to make sure that it's there. Obviously it won't start. It's like, give me no configuration. I don't know what to do.
7:37
But it looks like we can run it and everything we need is now on the server. It's just up to us to get our code there
7:44
and configure micro isky and Nginx and get them running.