Python for the .NET developer Transcripts
Chapter: Deploying Python web apps
Lecture: Setting up server foundations

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let me show you the code or maybe the configurations
0:03 that we're going to be using for this chapter.
0:05 So over here we have a chapter 12 deployment.
0:07 And what we're actually going to deploy is this version
0:09 or you could just as easily deploy this one over here.
0:12 They're basically the same as far as deployment goes
0:15 'cause we're not using a real database.
0:17 We don't have to set up one. We're just using SQLite.
0:18 That'd be fine.
0:20 So over here we have these files that are basically
0:23 going to configure our server to run this project.
0:27 We have this Nginx file.
0:28 This is the configuration file for Nginx.
0:31 If you come from the ISS world, you might run
0:34 Internet Information Server Management
0:36 I forget the exact name of it, UI, and configure
0:39 and setup your apps through that.
0:41 But we don't do that in Linux.
0:42 There's a lot of command line stuff
0:44 and a lot of config file stuff.
0:45 So we're going to set up this actually
0:47 relatively simple config file here.
0:49 And this is going to control how our app runs in Nginx.
0:53 And we're going to set it up on gitary.talkpython.com.
0:57 They'll see at the very end why we need an actual
0:59 proper domain, not just some made-up domain.
1:02 And this one sets up uWSGI
1:05 here, you can see right there, uWSGI.
1:08 Now, before we can do anything interesting with those
1:10 we need to do things like, well
1:12 install Nginx and some other stuff.
1:15 So what we're going to do is we're going to go through
1:17 a couple of steps here.
1:18 And I just want to take a moment and comment on
1:20 these steps, workin' with Linux in this way.
1:24 At first, if you're brand new to Linux
1:26 it's going to probably feel a little uncomfortable.
1:28 You're like, well, how do I know to type apt install ZSH
1:32 and do I need to do this? In this case, no.
1:35 But you can. And why would you do it? And so on.
1:39 It takes some getting used to
1:40 it turns out to set up the server I manage
1:42 it is actually incredibly simple
1:44 there's not a whole lot going on.
1:46 But figuring that out for yourself is a lot of work.
1:49 Luckily, you can take somebody's existing setup
1:52 and just adapt it a little bit.
1:54 So here we are, we're going to be setting up Nginx
1:56 we're going to be setting up uWSGI
1:57 you want to be using continuous deployment out of roughly
2:01 use GitHub as deployment
2:02 I guess we'll have to push a button to get it to deploy.
2:05 Nonetheless, something close to continuous deployment
2:07 straight out of GitHub.
2:09 And the steps that we have here
2:10 I'll explain 'em.
2:12 They take a lot of sort of poking around and research
2:14 to learn them from scratch, from a blank canvas
2:16 but once you have the outline laid out here, super simple.
2:19 And in fact, here's something cool
2:20 that's really hard to do on Windows.
2:23 I could take this script and run it
2:25 and then my server would be set up.
2:27 Actually, I have to change just a couple of settings
2:29 'cause I left them generic for you.
2:31 But if I was willing to, like
2:32 personalize this for my server
2:34 I could run this and then the server would be running.
2:37 That's pretty unique.
2:38 That's pretty awesome that I could just
2:40 script the creation of the server completely.
2:42 So, there's a lot of advantages to Linux
2:45 in addition to the fact that its cheaper hosting options
2:47 that we've already talked about.
2:49 The dev op side of things is really interesting.
2:51 A lot of scripts, these are ones we're going to run here.
2:54 So remember, when we first got it, I said
2:56 the first thing you want to do is do an update.
2:57 So apt is the package manager for Linux
3:00 so we updated it and then we did an upgrade.
3:01 So update is check is for updates
3:03 and upgrade is apply the updates.
3:06 So that's already done
3:07 we don't have to worry about that.
3:08 You might have noticed over here on this computer
3:11 that I have a cool shell that lets me do
3:14 all sorts of, like, history
3:16 and it lights up on Git
3:17 and when it's on the Git Repo and so on.
3:19 That is through Oh My ZSH.
3:23 I might even set that up by the server
3:24 this is totally optional
3:25 but it just makes it so much nicer to work with.
3:27 I really dislike Bash
3:29 so we're going to install the prerequisite
3:31 which is Z Shell, and then we're going to install
3:34 Oh My Z Shell like this
3:35 so give that just a sec.
3:38 It asks if you want to do that
3:39 and oh yes, Oh My Z Shell, things are better.
3:41 So we can exit out and then log back in
3:43 'cause we were running Z Shell within Bash
3:46 but now the prompt changed, so that's cool
3:48 so this is not required, but it makes me happy.
3:51 Now we need to install some stuff
3:52 like Build Essentials and Git and whatnot
3:55 so that we can better work with Python packages.
3:59 Of course, I could, like, run the entire script
4:01 but I want to talk you through each step and what it does
4:03 so, you know, you don't have to go
4:05 and research it from scratch
4:06 and it makes more sense this way, I hope.
4:10 So we need to set up python3 pip
4:12 and the dev tools and virtual environment.
4:15 Python on Linux is broken up into way more small pieces.
4:20 So this lets us put together the ones
4:22 that we need for our project.
4:25 We can also install Nginx
4:27 we're going to need that in a minute.
4:32 Divert, so it looked like it worked.
4:34 And then these here are so we can enable
4:37 Gzip support in uWSGI.
4:41 Not sure they're always needed
4:42 but last time I tried to set it up
4:44 it wouldn't work until I installed these
4:45 so, here we go.
4:47 Now that sets up our prerequisites on the app side.
4:52 Before we were only using certificates for logging in
4:55 we're not using passwords.
4:57 But if people keep trying to login
4:58 they can keep interacting with the SSH session
5:01 so we can set up this thing Fail2ban
5:03 which will actually ban them
5:04 if they do too many failed login attempts
5:06 so let's go ahead and do that.
5:13 Another thing we want to do is turn on the firewall.
5:15 So there's a really simple firewall that I'm going to do
5:18 called UncomplicatedFirewall.
5:20 And we want to allow SSH traffic
5:23 we could say this or we could even say SSH, they both work.
5:26 ACUP and ACUPS, so let's put those in here.
5:31 Right, those are rotated and we can actually
5:34 turn on the firewall, it says, warning, your SSH is in here
5:37 and if you did not put allow22, you're gone.
5:41 Luckily we did. We can double check by logging out
5:45 and logging back in.
5:46 Whew, we didn't break the server.
5:48 Do that right away, don't wait until it's all set up
5:50 and doing important stuff.
5:51 The other thing is, we're going to need to login to Git
5:53 so we can actually Git to remember our login
5:56 and not ask us for our credentials, you know
5:58 however many seconds that is.
6:00 It's like a month or something like that.
6:02 So we'll run that.
6:04 Run this, optional, but I find it super helpful.
6:07 If you need to do commit from the server back
6:10 you have to tell Git your username and email
6:12 or it won't work. I'll let my email address.
6:16 A lot of times I use my TalkPython email
6:18 but that's what I registered at GitHub
6:20 so I think it maybe makes more sense to use this Gmail one.
6:24 And that's it, now we're ready to go get our code
6:27 from GitHub because we have our credential cache
6:30 and all that sort of stuff.
6:32 Create the project structure
6:33 and then start setting up our app code.
6:37 Our server is basically ready to be webservered
6:39 it has Nginx, it has the right prerequisites for Python
6:43 and for running uWSGI, and some good, safe settings.
6:47 Yeah, we're in good shape.