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