#100DaysOfWeb in Python Transcripts
Chapter: Days 89-92: Deploying Python web apps to Linux
Lecture: Serving through nginx

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We're in the home stretch now. All we have to do is enable Nginx. So the first thing we do is just run this enable command over here on our server.
0:10 It looks like that's good and maybe go ahead and start it. And it's going to start and serve up some things.
0:15 So we go to this IP address here, like that. It'll say, "welcome to Nginx." That's great, this is this default file.
0:22 But let's go ahead and get rid of that and have it serve just our files. So first of all we can remove the default site by deleting that file.
0:33 So we'll do that. Now there's no more default site. Instead we're going to replace it with a different config file called billtracker.nginx.
0:39 This file format's pretty simple. This is how we can configure Nginx and we're going to tell it to listen on this fake domain name that we don't know.
0:47 But many different server's websites running on the same server under the same Nginx and they all trigger off of that name there.
0:55 But since we deleted the default and we only have one it didn't really matter but that's important in practice. And what we're going to do is we're
1:01 going to set up a few things. We're going to say the location for static files are going to be served directly out of Nginx. And it's going to do gzip
1:08 on them and things like that. It's going to have them be cached for a year and so on. Now, along here we're going to just say
1:15 this /static is really this folder. This static folder that had previously been served by Pyramid but now is actually being served
1:24 It sort of skipped, it never even goes to Pyramid or Web App, or uWSGI or anything like that. Then it says, otherwise, / and on.
1:31 We're going to go and just do what's called a proxy pass. Just pass over the request internally to uWSGI Get the response and then send it back.
1:40 So all we have to do to enable this file is to copy it in the right place which is /etc/nginx/sites-enabled/default Then just give it the name.
1:48 You want to copy it there and we just Nginx restart. Big moment of truth. Deep breath. What did we get? Refresh. Bam, how about that?
1:59 Now of course we need to register the domain or put it into our host file or something like that. We're not going to do that in this class.
2:05 That's sort of beyond the scope. You all can figure that out as really just a matter of your name provider's setup, okay? But here we have it.
2:12 It's running, look. Here's our BillTrakerPro. We can click over here and we can go make a payment. Put 45 bucks towards this.
2:19 It should be 300 left. Boom, it is. Our site is working. Let's go ahead and pay off this clothing. Here, where is it in this list?
2:25 Let's pay off the first one. So we add clothing. We still have 300 left. Let's go pay that final 300. It's paid off. Now you see?
2:34 It's gone from our list and is now moved down somewhere to there. It's like our site is working wonderfully on our DigitalOcean Server.
2:43 Running through Nginx. Things like that. If we go over here and it's back to element we can look at the actual request.
2:48 A couple interesting things here. First let's just go to HTTP. Notice the response time. 42 milliseconds. 42 milliseconds and if we ping
2:58 the server. You notice it's 30 milliseconds round trip time to ping it and it's only 41 milliseconds total in response time.
3:06 So that means we're only spending about ten milliseconds per request in our server. That is just awesome. So we do a whole bunch.
3:12 Now if you go over here and pick one of these, you might see sometimes ... It's all up and running. In the very beginning as the worker processes
3:20 process their first request this could be a little bit longer. Once it kind of gets warmed up and it's nice and zippy.
3:26 Another thing to note if we click on here ... I could pull this up. There we go. We look at the response headers. Look at this.
3:33 The server is Nginx 1.14.0. Remain on Ubuntu. It looks like it's working just like I said, right? Our web browser talks to Nginx
3:43 and it think that's it. We're done. I talked to the server. It said it's Nginx. Here's the CSS and HTML and all that.
3:49 In reality, it's passing that back to uWSGI. uWSGI's finding a worker process. The worker process is running our Python 3 App. It does the work.
3:57 It makes it's way outside of that ... Inside, out into that onion comes back and goes back to Nginx to here. Now our web app is up and running
4:06 in the Cloud on an Ubuntu Linux machine. We used but like I said, any of the three choices that I laid out for you are pretty similar
4:13 and really awesome.

Talk Python's Mastodon Michael Kennedy's Mastodon