Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: Deployment
Lecture: Configuring uWSGI

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So we saw we can run our development version of the code but we don't want to do that. We want to run the production version
0:06 not in that developer pserve thing in Waitress I think it is but actually on uWSGI with the real production scale out stuff that we want.
0:17 So let's look over here really quick and I'll show you one thing I've added this uWSGI section to the production.ini
0:25 And it just configures uWSGI to listen. So it says "Listen to local host this port. We're going to have to coordinate that with nginx.
0:33 We're going to run in master mode with ten subprocesses." That's kind of high, maybe we'll go with five because we only have one gig of RAM.
0:43 Who knows maybe it's okay. It is takes too long more than a minute 60 seconds kill it off and just restart that process
0:52 and then log here. So we should already have those log files there You don't want to go with that tree do you?
1:05 Here we go we have logs, PyPI outputs for log. Haps logs PyPI oh we're not going to use that part. We use that somewhere else.
1:15 Okay so this is a uWSGI log this just needs to be in place. There's nothing magical about it. This controls how many subprocesses you have
1:22 in that fanout thing we talked about enable threads will get us a little more concurrency as well per process so we should be good.
1:30 And this is the one we want to run in order to run this well we have to have uWSGI around. Well since you don't have uWSGI. So let's do this.
1:40 And you can install it with apt here but we're going to just pip install uwsgi into our virtual environment. This can take a moment to install.
1:53 There's I think a lot of compilations steps or something happened in here. Great so our uWSGI is working but it said it gave me no configuration.
2:04 All right so now we pretty much are ready to go. Let's go back to our server setup. So what we're going to need to do
2:10 is to copy this pypi.service into systemd so that basically systemd knows to start uWSGI as the system daemon. And if we look over here real quick
2:23 what we're going to do is we're going to run that virtual environment and this flag -H means you're in a virtual environment
2:29 so use that one and it's here. And then here this ini-paste this is the config file. So before we go on let's just make sure
2:38 by copying this part let's make sure that this is actually working okay. When you set it up as a system daemon
2:45 if it fails you're like "Agh why did it fail" this might get us a little more information. We'll either see that it's working
2:50 or we'll get a little more information. So here we go. I think it's singular app let's see. No it's app. Let's figure out why it sees what I said.
3:00 It's good to have this running. Ah, the problem is we want it like that. Just pypi/deploy. And the fact is that it's doing nothing
3:13 while the logging has captured all the output. But if we open up another terminal and now we do our HTTP not against the development server port
3:25 but we got to look over here to find this port we can see if the server is working here and it is. Perfect so we have out config from uWSGI working
3:37 as long as I remember to put that back. Like that. All right let's move these over Over here. Let's cancel out.
3:54 We don't need that to run anymore. git pull make sure we get the new values. So now we're ready to try this command again.
4:04 Let's see if this is going to work. Great it is working. So what we need to do is copy that file to where it belongs.
4:15 So we're going to run this and that worked. Now we can go and use the system control stuff to start
4:25 assuming everything goes right great that looks good. We can ask for the status and you can see all the processes running in this tree
4:35 and looks like it's running. No errors we can check on Glances real quick. Sort by memory. So a whole bunch of uWSGI worker processes running.
4:44 That's good. Let's get out of there. So it looks like it's running but if I reboot it's not going to start again so what we have to do is enable it.
4:54 And now it tells the system "Let's restart. You know make this active all the time." So any system reboot as soon as it starts up
5:03 it's going to start this whole series of processes and just keep it running. Perfect so uWSGI looks like it's working.
5:11 There it is once again but this time as a system daemon. So we can just call that good. That is done. The last thing to do is hook up Nginx to uWSGI.


Talk Python's Mastodon Michael Kennedy's Mastodon