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