Building data-driven web apps with Flask and SQLAlchemy Transcripts
Lecture: Deployment overview and topology
0:00 Now that we've built our web app
0:01 it's time to share it with the world right?
0:04 It's great to have a little app that we built
0:06 but it's basically useless
0:07 if we don't put it on the internet.
0:09 It is a web app, after all.
0:11 At least put it on an internet
0:12 for your internal company, right?
0:14 So that's what this chapter's all about.
0:16 We're going to see how to deploy our web application
0:20 onto a standard Linux server in the cloud.
0:23 I want to be clear that this is not the only option.
0:26 There's certainly other ways to put our web app out there.
0:29 We could go use something like Heroku
0:31 and just configure Heroku
0:33 to grab our stuff out of our GitHub repository
0:36 and launch it into their system.
0:38 Those to me seem easier.
0:40 They're less flexible and often more expensive
0:42 but they're easier.
0:44 So, what I want to do is show you how to deploy
0:46 to a standard Linux server
0:48 run it in some cloud VM somewhere
0:50 and you can adapt that from Digital Ocean, Linode
0:53 AWS, Azure, wherever you want to run it.
0:56 So we're going to do that in this chapter.
0:59 And that brings us to our overall architecture and topology.
1:03 One of the things we're not going to focus on here
1:06 is setting up and configuring a database server.
1:09 I consider that a little bit outside the scope
1:11 of this course and you can pick the database server
1:13 that you want and then configure it.
1:15 So you'll have to fold that in here.
1:17 Right now, we're choosing SQLite
1:19 which means as long as that file comes along
1:21 we have our database.
1:22 So with that caveat, here's how it's going to work.
1:25 We're going to go get a box in the cloud
1:27 which is going to be an Ubuntu Server, probably 18.04
1:32 that's what that little icon in the bottom left means.
1:34 On here, we're going to install Nginx
1:37 Nginx is the thing that people will actually talk to.
1:41 This listens on Port 80 and on Port 443
1:45 for regular HTTP
1:47 and on 443 for HTTPs encrypted traffic.
1:50 A request is going to come in here
1:52 but this does not run our Python code.
1:55 It serves up static files and it delegates
1:57 to the thing that actually runs our Python code.
2:00 That thing is called micro WSGI, uWSGI
2:04 muWSGI, I guess it should be.
2:06 Now uWSGI, when we run it
2:08 will handle our Python requests.
2:11 However, we don't want to just run one of 'em.
2:14 Remember, you may or may not be aware
2:15 that Python has this thing called the GIL
2:17 or Global Interpreter Lock
2:19 which inhibits parallelism within a single process.
2:23 And a lot of the ways people get parallelism in Python
2:26 is to actually create multiple processes.
2:29 This also has great benefits for failover
2:31 something goes wrong with some process
2:33 running one of our requests
2:35 we can kill it and have other processes deal with it.
2:37 It's not the only way to get parallelism
2:39 but it's one really nice way.
2:40 So we're going to do that and have uWSGI
2:42 spin off a whole bunch of itself.
2:46 This will actually run our Python code.
2:48 It's going to host the Python Runtime
2:51 and it's going to launch and initiate a bunch of copies
2:54 of our website running in parallel.
2:56 Here we have it configured to run six worker processes.
3:00 So here's what's going to happen.
3:02 Request is going to come in, hopefully over HTTPS
3:05 right, you want to set up some sort of encrypted layer here
3:07 right, that's the way the web's going these days.
3:11 And once we're inside the server
3:13 we no longer need encryption
3:14 so we'll just do a regular HTTP request
3:16 over to uWSGI itself.
3:18 Micro SGI will decide which of its worker processes
3:21 is ready to handle it. This time that one is.
3:23 Maybe next time, this one's free, or maybe that one.
3:26 It'll find one that's not busy, or less busy
3:29 and then pass that request off
3:31 and then return the response back through Nginx
3:34 back out over HTTPS to the client.
3:37 So this is what we're going to set up
3:39 an Ubuntu Server with these things
3:41 along with Python 3 and our web app ready to roar.