#100DaysOfWeb in Python Transcripts
Chapter: Days 89-92: Deploying Python web apps to Linux
Lecture: Setting up uWSGI

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We have our server all set up, and our source code there and it's pretty much ready to run. What is left is actually getting our web app
0:09 to run on the server. So, we've already cloned it into our app_repo the next thing to do is to actually go
0:15 and do what is required to get the requirements on the server, and register the Pyramid web app as a package, because that's how it works.
0:23 So, let's go do that. Come over here, and we're in this directory with our setup.py, so we've got to do a couple of things.
0:30 Install our requirements.txt notice we're in our virtual environment because that's how we set up our login to be.
0:36 So here we're installing our requirements then we'll run setup then it should be pretty much ready to go. Okay, great. We'll see if we can pserve it.
0:46 That looks more or less like we can. There it is, it's running on our server. Now, we don't want to use pserve as our backing server
0:53 we want to use uWSGI. It's a much better server. So this is not how it's going to run but this verifies that it's generally in place.
1:00 So we could even use HTTP, HTTPie, on it. We open up another one of these, and go here and just HTTP against that
1:07 you can see this looks like our webapp. Excellent. So, things are looking good. Get out of here. So, we need to configure, first thing, is uWSGI.
1:17 You can see, there's actually some plugins for Nginx and these unit files so let's go ahead and install those real quick.
1:25 Sweet. Now, after restarting PyCharm you can see we have little icons we have some code highlighting, things like that.
1:31 So it's a little bit better. So, for these units, these are things that run system daemons on Linux. And the idea is, we give it a little description
1:38 let's just say the bill tracker server the target, when it's going to be able to start has to wait on sysog but after that it can start
1:44 and we give it details about the service. So, we want to run our uWSGI instance out of this virtual directory
1:51 we're going to use the INI file over here, actually we want to change this to that, okay that's looking good
2:00 and we want to set our working directory here. Notice, one weird thing about these runtime directories
2:04 if it doesn't have the trailing slash, it doesn't work. I don't know why, should be able to put that on for us
2:09 but whatever, just make sure the trailing slash is there. Now, this, in order to test this we want to see if this command itself will run.
2:16 And it turns out it will not. So here we are, over in our server. If we try to run this command it should find a uWSGI, we've installed that.
2:26 It should find the INI file. But it won't start. Notice it's exiting here and we're back at our prompt.
2:33 The problem is, there's where's it say what the error is I don't actually see where the error is in here
2:38 but it's not working, because it should block the process and just keep running, block the terminal. And the reason is, over here in our production.ini
2:47 we need to put information about running in uWSGI. So, let me grab that real quick and stick it over here. So when we run our code normally
2:56 it's going to run this bit and it's going to use this server part. But we're not going to use that server. Instead we're going to use the uWSGI server.
3:04 So we need different config settings in here. I'll just paste them in because they're always the same and you just edit the details.
3:10 So, over here, we have a uWSGI section and let's just tell PyCharm that this is spelled okay this is spelled okay. So this is going to be read by uWSGI
3:18 to configure it. It says, well, we're going to run on localhost port 9055. Now you might say, hey Michael, the web is on port 80.
3:26 Yes, and that's what Nginx is going to listen on. Internally, though, we're going to listen on this port and pass through.
3:32 Running this master mode, use five processes enable threads, all those things are good. We're also setting up our logging to go to this file
3:40 and I think this would be pretty good. So now, put in, checkin these changes run on uWSGI going to push those back to Github
3:47 which, when that's done, we can come over here and do a git pull and we get all those changes. Beautiful. Now, if we just try to run that command again
3:54 it should work. Might not, but it should. Let's try. Ooh! Look at that. No more warnings. And let's go over here and command
4:03 and we can just look at the log file here and you can see, now, it's running it has all these workers it's running in a multiple interpreter mode
4:11 here's our webapp, startup all that kind of stuff. So that's pretty awesome. And if we try our HTTP localhost that new port, 9055, boom!
4:21 Our site's running! Perfect, it's running on a uWSGI. But uWSGI itself is not set up to run as a system process which is what we're going to need to do
4:30 because we don't want to have to come in here and type this command we want it just to start with the web server. So the final thing to do will be
4:35 to register this unit file as a system service which is what we're doing over here. So now we've tested our command
4:42 we're going to copy this file over to systemd, and it should run. We run this command, that looks like it works
4:48 and then the next thing to do is just to enable the bill tractor, beautiful. And then we'll try to start it.
4:55 That's good, you don't see an output, that's good. And then you can ask what it's up to. You see all the processes, they're all still running
5:01 everything's still working great. And we can also verify that in glances. So now you see, over in glances
5:06 all of our web processes are running, it's all good and we could do one more request, so that's good. The final test will be
5:13 if we reboot the server, will it come back? Well, that's easy. We're not in production yet we're just setting up the server. So let's restart it.
5:20 And see, if we log back in, if we have our server running. If we have our web server running. Not quite up yet. A few more times, there we go.
5:28 Now, if we just hit localhost again. Perfect. So, every time our server turns on our web service under uWSGI is up and running in all those processes.
5:38 We have the main coordinating process and the five, as we specified five in our production.ini five worker processes. All right, perfect.
5:47 Last thing to do is get Nginx working and we'll be all set.


Talk Python's Mastodon Michael Kennedy's Mastodon