Python for the .NET developer Transcripts
Chapter: Deploying Python web apps
Lecture: Concept: Web server architecture

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Before we start
0:01 configuring and installing our server
0:02 let's just take a step back
0:04 and look at all the moving parts.
0:05 So what we're going to do
0:06 as we said
0:07 is we're going to set up an Ubuntu machine.
0:09 That's the Ubuntu logo in the corner by the way.
0:12 We're going to set this up and manage it ourselves.
0:14 We talked about all of the different options
0:16 and there's many things you can choose.
0:17 For this course
0:18 I'm going to show you this because
0:19 like I said, I think
0:20 gives you the most flexibility.
0:22 So what software's involved and
0:23 how does it fit together?
0:24 Well, there's actually two web servers
0:26 so we're going to install this thing called, Nginx.
0:29 It's a free open-source web server
0:31 and it is really really awesome.
0:34 However, it doesn't run Python Code.
0:37 It just handles web traffics, static files
0:40 SSL, that kind of stuff.
0:42 For what we want to do
0:43 we also need to run our Python code.
0:45 Obviously, that's the logic
0:47 and implementation of our web app.
0:48 So, we're goin to use another server called
0:50 uWSGI. The U is like a MIU, a little greek, Miu
0:54 so uWSGI. Remember Web Service Gateway Interface.
0:58 This thing can host any WSGI Python application.
1:01 So Flask, Django, Pyramid, bunch of others.
1:04 We're going to do a couple of things
1:06 to improve the scalability of our web app.
1:09 One, we could have a bigger server
1:10 with more cores. We could actually have multiple servers
1:13 with load balancers. All sorts of stuff.
1:15 But even on this one server here
1:18 instead of just running one uWSGI
1:20 we're going to run uWSGI
1:21 whose job is to communicate with a bunch of
1:24 little uWSGIs. All these little guys.
1:27 This is actually where our
1:28 Python is going to run.
1:29 We have all of these little worker processes.
1:32 They'll basically be running copies of our app.
1:34 Remember Python's Gill and how that can be a problem?
1:36 And how multi-processing solved it?
1:39 Well, here we go.
1:40 We have a whole bunch of parallels
1:41 have added by doing this
1:43 amongst other things.
1:44 Like, if one gets stuck
1:45 locked up, runs out of memory
1:47 we can use all the others.
1:49 So our requests are going to come into this world
1:51 over ACDPS, it's going to talk to Nginx.
1:54 Nginx is what the world believes
1:56 our web server is. They talk to Nginx.
1:58 They never see or know about uWSGI at all.
2:01 They talk to Nginx. That's that.
2:03 This is going to handle the SSL
2:06 maybe even serve them back the static files.
2:08 It won't go to the Python Code for that.
2:10 If there's a data driven request
2:11 it's going to go into our Python application
2:13 it gets delegated over here
2:15 maybe over HTTP or maybe even
2:16 Linux Sockets. It's going to figure out
2:19 Well, which one of these is free?
2:20 Well, that one's free.
2:21 Let's let that one handle the request.
2:22 Another request comes in, it says
2:24 Oh. That one's free. Another request comes in, it says
2:26 Oh, this one! This is the one that's free!
2:27 and they're going to be running
2:28 potentially in parallel, processing the request.
2:32 So, this is the overall pictures.
2:34 What we have to do to make this work is
2:35 we have to setup Nginx
2:36 with the right configuration
2:38 we have to setup uWSGI.
2:40 We have to get our code on the server
2:42 and setup the server with the right dependencies.
2:45 We're actually going to do that in reverse order.
2:46 We're going to put the dependencies
2:47 then put our code, then uWSGI.
2:49 Make sure that uWSGI working
2:51 on local-host and then
2:53 we're going to set up Nginx
2:54 to talk to the world
2:55 and let Nginx actually close that gap there
2:58 and talk to uWSGI on local loop-back
3:00 or over our socket.
3:01 Before you're done that folks
3:02 this is not that different than IS.
3:04 IS handles all the static responses and so on.
3:08 We have the ASP.NET worker processes that run.
3:12 You can even set them up in this kind of
3:13 Farm Mode that we're talking about here.
3:15 Though we have ISS and ASP.NET worker processes
3:18 Nginx is like the ISS.
3:21 uWSGI is like the ASP.NET worker processes
3:23 that would run your .NET C# code
3:25 but of course, we're running Python code
3:27 in this Python world.