Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Deployment
Lecture: Running in nginx
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
The final thing we have to do to get our server up and running is we have to configure Nginx. When you talk to the server
0:08
when you make a request to it in your browser or anyone does what you're actually talking to is Nginx. It does all the static file serving, the SSL
0:16
translation, all that kind of stuff. And then internally it will make a request over to uWSGI, get the result and stream that back to you.
0:24
So as far as the public is concerned Nginx is your web server. They don't know uWSGI is doing the magic behind the scenes.
0:31
So we still have to set that up. Now let's just figure out what happens if we request a page over there.
0:36
If I put in the URL here, we get "Welcome to Nginx". You see, it's already running, so that's cool but it's not running our code.
0:42
So let's make it run our code. That brings us back to our little script here. We've already got Nginx installed. That's cool.
0:48
But what we need to do is remove that default page that says, "Hey, Welcome to Nginx. Good job setting it up". So we are going to remove that
0:56
and then the next thing we need to do is copy our Nginx configuration over to sites enabled. Now let's just quickly scan over this.
1:04
Since we are listening on port 80 we are going to listen for fake_pypi.com I'm going to change that actually in a minute
1:11
but we could just hack that into our host or what you would really do is put your site like training.talkpython.fm by or www.google.com
1:20
or whatever your domain is, you put it here. And this allows you to have many different websites served out of the same Nginx. So I have one server
1:29
that has six or seven different Nginx files and configurations for different domains. And this is how you specify that.
1:35
Don't pass back the version of the server. Let's see, we have our static files and those are not processed by Python anymore.
1:43
They are now exclusively handled by Nginx. So we say when somebody's asked for something /static you'll look over here. All right.
1:50
And those are cached for a year. That's good. And then it says otherwise go to your application and your application, does this local call back
1:59
over to 5000. I like to use http. You can also use Unix sockets which is a little better performance but not a huge amount I don't believe.
2:08
This way, I can actually test if anything is going wrong I can test my code. With the sockets, it's not so much. So I'm going to leave it like this.
2:16
And we're just going to copy this file over to where this needs to be configured which is this line right there. That worked. Good.
2:25
And then we're going to enable Nginx. I believe it's already enabled, but just to be sure. And we're going to restart it
2:31
so it detects the configuration change. All right, super. Let's see what happens when I go over here. Refresh. Moment of truth. Ta-dah!
2:41
How awesome is that? Cool. Our site is online. It has a couple of problems, which we're going to fix. Problem number 1: it's not SSL.
2:49
Connect is not secure. These days you really kind of want to have your connection over https. It helps with search engine optimization
2:56
even if you don't care about being truly secure. The other one is look here how many projects, releases, etc. We have no data.
3:04
So, remember we had that script if you remember that far back? If we go into pypi.org and we go into bin remember here we had this load data?
3:16
So we just say Python load data. This will go and actually import all that data build it up. If we come back here one more time and do a refresh...
3:25
Ta-dah! There's our projects and now these show up down here as well. Perfect, right?