Building data-driven web apps with Pyramid and SQLAlchemy Transcripts
Lecture: Concepts: Deployment
0:00 Let's review some of the concepts we saw during deployment.
0:03 Now, we deployed to Ubuntu and DigitalOcean.
0:07 But you don't have to go to DigitalOcean.
0:09 What you saw would work almost exactly the same
0:11 in places like AWS, or Azure, or Linode, or whatever.
0:15 You do have to be aware on AWS
0:17 there's also a cloud-controlled firewall
0:20 that happens to be blocking things
0:21 so you have to unblock the HTTP ports in a couple places.
0:25 That's AWS. It's kind of super complicated.
0:27 And if you're doing basic things that
0:29 anyway, that's a whole different discussion.
0:31 Let's talk about the concepts.
0:33 We started by having a production.ini.
0:36 And there's a couple settings
0:37 that I didn't point out there that are important.
0:40 I did point out the uWSGI section.
0:43 So here you have the localhost port
0:46 it's going to listen on, this is where Nginx speaks to it
0:49 you don't expose this on the internet directly.
0:51 Master mode means we're going to run
0:53 five processes for parallelism.
0:56 Five actually can handle quite a bit.
0:57 Threads amplifies that further.
0:59 And then the hari kari thing
1:01 is if they don't respond in a certain amount of time
1:03 they'll get killed off.
1:04 And finally, you want logging in your app
1:07 so you can figure out what's happening.
1:08 The production.ini settings I did not show you were
1:11 there's a difference between the development.ini
1:14 and production.ini, where for example
1:16 the templates don't auto-reload.
1:18 You have to restart the process if you change the templates.
1:20 The debug toolbar thankfully is not there in production.
1:24 That would be bad, you don't want it there.
1:26 So things like that, you can check the differences.
1:28 They're not major but there's a few other differences
1:30 that Pyramid itself puts into those two files, plus this.
1:35 Once we have our production.ini ready to go
1:39 we want to create a system daemon, using the systemd
1:42 that's going to run uWSGI anytime the server is on.
1:46 It's going to auto-start it and keep it running.
1:48 So here is the settings we used.
1:51 Here, we just say execstart
1:52 use that virtual environment to uWSGI
1:55 and use our production.ini to do it, so really simple.
1:59 I just noticed I didn't have a forward slash
2:01 in the runtime directory in the end.
2:03 uWSGI claims that the directory is invalid
2:05 if you don't explicitly put that trailing slash
2:08 so make sure you put that there, if you care.
2:11 Alright, so you have basically this file
2:13 in the files I'm giving you as well.
2:15 So this would be uWSGI running as a system daemon
2:18 and you're going to need to copy that from wherever it is
2:21 over to etc/systemd/system.
2:24 Finally we want to set up the outer shell
2:27 the thing that listens on the actual internet
2:29 the thing that serves the static files
2:31 and then also delegates the Python request
2:33 back to uWSGI and that's Nginx.
2:36 So here's how we can configure that.
2:38 You can listen on more than one port.
2:40 You can listen on 80 and do a redirect to 443.
2:43 There's a lot of configuration stuff you can do
2:44 but here's a super basic one.
2:46 Listen on 80 for fake_pypi.com
2:49 as the domain name that we registered.
2:52 You know, registered by typing it into our host file.
2:54 My serve up static files
2:56 point at the static directory and set some expiration date.
3:00 And this one you want to copy over
3:01 to etc/nginx/sites-enabled.
3:06 Now finally we also have in our Nginx files
3:09 telling it to go, if you don't go to /static
3:12 just go to / or anything below that, except static.
3:15 Just go and do a request over to your application
3:18 which forwards over to our configuration in uWSGI.
3:23 Be really careful that that port 8999 is the same one
3:27 as you have back in your production.ini's
3:29 configuration for uWSGI.
3:31 They have to match, of course
3:32 so that they can find each other.
3:33 Or use a system socket.
3:35 That takes a little more setup
3:36 so I'm just using the localhost HTTP.
3:40 And that's it. I gave you the script of all the pieces you need to run.
3:43 These are the details of the
3:44 configuration files you have to set up.
3:46 And hopefully you come away feeling like
3:48 setting up that Linux server wasn't too much work.
3:51 Did talk a lot about it, but if I just put my head down
3:53 and focused, and went after it
3:54 we could probably do it in 10 minutes
3:56 And most of that time is waiting on installs
3:58 you know, apt this, pip that, right?
4:01 Just waiting for those things to happen.
4:02 So not a lot of work once you have the scripts
4:05 and everything put together for you.