Python for the .NET developer Transcripts
Chapter: Deploying Python web apps
Lecture: Concept: uWSGI
0:00 Let's review our uWSGI configuration
0:02 and how we got uWSGI to run
0:04 under systemd as a systemdaemon.
0:07 So what we did is we created this thing
0:08 called a systemd unit file
0:10 and it has this certain format that you can see
0:13 various things you can set here and so on.
0:15 I've highlighted the really important parts.
0:17 So we have ExecStart.
0:18 This is the command that runs this daemon.
0:21 So what we're going to do is we're going to run
0:23 uWSGI out of our virtual environment
0:25 and we're going to tell it
0:26 your home is the virtual environment you come from, so.
0:29 Also, go back and just /apps/venv.
0:32 Run in master mode with four sub processes
0:34 four worker processes, that it's going to coordinate
0:36 and each of those get two threads.
0:39 It's going to listen on HTTP on port 5000
0:41 as opposed to, say, Linux sockets.
0:44 Going to manage the script name.
0:45 We're going to add to the Python path.
0:47 We actually did that explicitly in our code.
0:50 Remember that that imports this in OS
0:52 and we did that little line at the top.
0:54 We could've technically avoided that
0:55 if we only ran it this way, rather than
0:58 trying to run it directly in Python.
0:59 I kind of like it.
1:00 That it's super easy to run just in Python all over
1:02 but this also fulfills that role
1:04 so technically, we could avoid it
1:06 but it seems like not a bad idea to throw in, so I did.
1:09 Then we have to say, where is the Flask app
1:12 that we're going to call run-on, located?
1:14 We created a wsgi.py.
1:17 File an imported app into it
1:18 so this mount here is saying, go to that file
1:22 WSGI.py, the module
1:23 and then, look for a thing called app and run that.
1:26 Then finally, we just said, hey
1:27 login this directory, right?
1:29 And that's the log output.
1:30 You want to make sure that this process
1:31 has permissions to write to that log.
1:34 One quick note, this whole ExecStart is just one line.
1:37 It just happens that it won't fit on one line.
1:39 It's really, really long.
1:40 There's no scroll bar.
1:42 Guess I could put one in PowerPoint
1:43 but it doesn't seem like a good idea
1:45 so just be aware that it's just one huge
1:46 and long line with wrapping.
1:48 And we also put the runtime directory
1:50 for where our web app is located
1:52 same as the Python path addition there. That's it.
1:56 And the way you work with these, basically, is you get one, you copy it over, you tweak it
2:00 make minor tweaks to it, so it'll run
2:02 and then, you're good.
2:03 Now, also remember, when you're doing this
2:05 infrastructure stuff, little, tiny baby steps.
2:08 Don't do it all and then, try to test it.
2:09 So for example, I didn't just try to run this.
2:12 I copied the ExecStart without the log parts
2:14 so I could see what was happening.
2:16 Then, I tried to run that to see if that was going to work.
2:19 If something goes wrong
2:20 it's much easier to see what's happening
2:22 by trying to run that directly
2:24 rather than, well, we're running systemd
2:26 then we're enabling the service, and it's starting.
2:28 Then, it's logging over there, but maybe it won't log there
2:31 if it doesn't have permissions to log there.
2:32 And there's just like a bunch of other layers
2:34 you want to verify, so baby steps.
2:35 Try each thing along the way.
2:38 Finally, when we were convinced it was going to work
2:40 we copied this file over to etcetera, systemd
2:43 system guitar a.service.
2:46 Them we issue the commands the systemd to say
2:48 enable the service that created this SymLink
2:50 so that it'll autostart when we reboot the system.
2:53 That was it. We had our Python code running the server.