1
1

00:00:00,280  -->  00:00:02,050
<v Tutor>Hi guys and welcome back.</v>
2

2

00:00:02,050  -->  00:00:05,440
In this video we're going to be configuring uWSGI
3

3

00:00:05,440  -->  00:00:07,150
so that it can run our application
4

4

00:00:07,150  -->  00:00:09,250
when engine x talks to it.
5

5

00:00:09,250  -->  00:00:11,930
Remember that we can run our app directly
6

6

00:00:11,930  -->  00:00:14,680
using Flask and Python.
7

7

00:00:14,680  -->  00:00:17,450
However, that doesn't perform as well
8

8

00:00:17,450  -->  00:00:19,810
and it's not as safe as using another server
9

9

00:00:19,810  -->  00:00:21,620
that is placed in front of it.
10

10

00:00:21,620  -->  00:00:25,740
By using engine x to talk to uWSGI
11

11

00:00:25,740  -->  00:00:28,740
and uWSGI to talk to our app
12

12

00:00:28,740  -->  00:00:31,030
we're going into create higher performance
13

13

00:00:31,030  -->  00:00:32,253
and more security.
14

14

00:00:33,830  -->  00:00:38,350
Let's begin by modifying our uWSGI.ini file.
15

15

00:00:38,350  -->  00:00:40,220
Notice that we've already got some stuff here
16

16

00:00:40,220  -->  00:00:41,700
but this is for Heroku,
17

17

00:00:41,700  -->  00:00:44,260
so we need to make a couple of changes.
18

18

00:00:44,260  -->  00:00:47,310
I'm going to delete all of these lines
19

19

00:00:47,310  -->  00:00:51,543
by just pressing D D twice for each line.
20

20

00:00:52,600  -->  00:00:55,450
Don't delete the uWSGI line there
21

21

00:00:55,450  -->  00:00:57,710
because that tells the running programme
22

22

00:00:57,710  -->  00:00:59,883
that these files is about uWSGI.
23

23

00:01:01,150  -->  00:01:03,190
We're going to need a bunch of things.
24

24

00:01:03,190  -->  00:01:05,260
First of all, the base.
25

25

00:01:05,260  -->  00:01:09,160
This is where the home folder of our application is,
26

26

00:01:09,160  -->  00:01:14,160
and that's in var/www/html/pricing-service.
27

27

00:01:14,230  -->  00:01:18,360
Then we do tell it, which file contains our app.
28

28

00:01:18,360  -->  00:01:22,000
So that's app.py and the .py is inferred
29

29

00:01:22,000  -->  00:01:23,490
so you don't have to give it.
30

30

00:01:23,490  -->  00:01:27,060
Then which module, we should import
31

31

00:01:28,230  -->  00:01:30,550
in order to include our app.
32

32

00:01:30,550  -->  00:01:32,780
And that is also the app module.
33

33

00:01:32,780  -->  00:01:34,990
And here we're using the %[]
34

34

00:01:34,990  -->  00:01:37,300
just to refer to this variable here.
35

35

00:01:37,300  -->  00:01:38,770
You could just do app,
36

36

00:01:38,770  -->  00:01:41,580
but if we want to change the app file later on,
37

37

00:01:41,580  -->  00:01:42,960
we just have to change it in one place
38

38

00:01:42,960  -->  00:01:44,370
as opposed to in two places.
39

39

00:01:44,370  -->  00:01:46,660
So this is why we're doing that.
40

40

00:01:46,660  -->  00:01:50,210
Next up, we need to tell uWSGI,
41

41

00:01:50,210  -->  00:01:53,640
where our virtual environment is located.
42

42

00:01:53,640  -->  00:01:56,310
So for that, we need to first find it,
43

43

00:01:56,310  -->  00:02:00,950
I'm going to press escape and :wq to save and quit.
44

44

00:02:00,950  -->  00:02:03,357
And then I'm going to run pipenv --venv.
45

45

00:02:04,380  -->  00:02:08,270
And this tells me where the virtual environment is located.
46

46

00:02:08,270  -->  00:02:09,350
Just copy all of that,
47

47

00:02:09,350  -->  00:02:11,680
making sure not to miss any characters,
48

48

00:02:11,680  -->  00:02:14,840
and then go back to uWSGI.
49

49

00:02:14,840  -->  00:02:17,030
Notice I'm pressing the up arrow in my keyboard
50

50

00:02:17,030  -->  00:02:19,663
to cycle through old commands just for speed.
51

51

00:02:20,720  -->  00:02:22,310
Go back in here and press the I key
52

52

00:02:22,310  -->  00:02:24,020
to go into insert mode again,
53

53

00:02:24,020  -->  00:02:27,083
and type home equal your path.
54

54

00:02:28,550  -->  00:02:31,220
Then we need to also set the Python path,
55

55

00:02:31,220  -->  00:02:33,903
and this is going to be our base folder.
56

56

00:02:34,950  -->  00:02:36,580
When we type home,
57

57

00:02:36,580  -->  00:02:40,230
this sets where Python is located in our system,
58

58

00:02:40,230  -->  00:02:42,140
as well as where any libraries
59

59

00:02:42,140  -->  00:02:46,180
we may want to import should be looked for, at first.
60

60

00:02:46,180  -->  00:02:48,070
The Python path defines where
61

61

00:02:48,070  -->  00:02:50,090
the top level of our application is.
62

62

00:02:50,090  -->  00:02:52,270
So that when we import any local files,
63

63

00:02:52,270  -->  00:02:53,573
we will look there first.
64

64

00:02:55,630  -->  00:02:56,900
We need to define a socket,
65

65

00:02:56,900  -->  00:02:59,340
and this is just a file that contains some information
66

66

00:02:59,340  -->  00:03:02,220
so that engine x can talk to uWSGI.
67

67

00:03:02,220  -->  00:03:03,660
And we just need to define
68

68

00:03:03,660  -->  00:03:07,400
where this file will be created by uWSGI
69

69

00:03:07,400  -->  00:03:09,800
so that engine x can then find it.
70

70

00:03:09,800  -->  00:03:14,753
And I'm going to create it in the base folder/socket.sock.
71

71

00:03:15,650  -->  00:03:17,560
These are normally called .sock files
72

72

00:03:17,560  -->  00:03:18,760
I would keep it as that.
73

73

00:03:19,930  -->  00:03:22,460
We need to make sure that this socket can be read
74

74

00:03:22,460  -->  00:03:25,710
and written to by anybody, including the engine x users.
75

75

00:03:25,710  -->  00:03:29,410
So I'm going to do chmod-socket equal 777
76

76

00:03:29,410  -->  00:03:31,170
And this allows everybody
77

77

00:03:31,170  -->  00:03:34,490
to read and write that socket file.
78

78

00:03:34,490  -->  00:03:36,650
If you don't fully understand what these commands are doing,
79

79

00:03:36,650  -->  00:03:37,580
don't worry too much,
80

80

00:03:37,580  -->  00:03:41,400
That is a bit of research that goes into creating these,
81

81

00:03:41,400  -->  00:03:46,043
and you don't really have to know all that much about it.
82

82

00:03:47,610  -->  00:03:51,180
Then we're gonna create a certain number of processes,
83

83

00:03:51,180  -->  00:03:54,970
which I'm going to stick to eight and threads, eight.
84

84

00:03:54,970  -->  00:03:58,330
eight threads and eight processes mean a total of 64
85

85

00:03:58,330  -->  00:04:01,520
independent instances running of our application,
86

86

00:04:01,520  -->  00:04:05,520
which means that we can serve more users at once.
87

87

00:04:05,520  -->  00:04:08,760
However, the more processes and threads you have,
88

88

00:04:08,760  -->  00:04:13,660
the less resources each one has, by your application.
89

89

00:04:13,660  -->  00:04:16,563
That means that they go slower, the more you have.
90

90

00:04:17,610  -->  00:04:20,770
This is a trial and error number.
91

91

00:04:20,770  -->  00:04:23,670
So you have to find out what number of processes and threads
92

92

00:04:23,670  -->  00:04:25,300
is good for your server,
93

93

00:04:25,300  -->  00:04:28,450
and just play around with it until you find a good number.
94

94

00:04:28,450  -->  00:04:30,800
By checking the performance of your application,
95

95

00:04:30,800  -->  00:04:33,810
how many users can interact with it at once and so on.
96

96

00:04:33,810  -->  00:04:35,940
There is a whole field of software testing
97

97

00:04:35,940  -->  00:04:39,200
called load testing, that we're not going to get into here.
98

98

00:04:39,200  -->  00:04:41,940
But for now, I've seen the eight processes and eight threads
99

99

00:04:41,940  -->  00:04:43,880
seems to perform all right.
100

100

00:04:43,880  -->  00:04:47,080
After we're going to define the harakiri variable,
101

101

00:04:47,080  -->  00:04:50,750
and this is how many seconds we will wait
102

102

00:04:50,750  -->  00:04:54,110
before killing one of these threads, if there is an error.
103

103

00:04:54,110  -->  00:04:56,670
For example, it's possible the thread may recover
104

104

00:04:56,670  -->  00:04:58,170
so we're giving it 15 seconds
105

105

00:04:58,170  -->  00:04:59,823
before we kill it and restart it.
106

106

00:05:01,100  -->  00:05:04,160
Killing and restarting a thread can be quite damaging
107

107

00:05:04,160  -->  00:05:08,010
because it can cause shutdowns for your users,
108

108

00:05:08,010  -->  00:05:09,610
connection errors and things like that.
109

109

00:05:09,610  -->  00:05:11,680
So we do want to give it a little bit of time
110

110

00:05:11,680  -->  00:05:13,600
to recover if it can.
111

111

00:05:13,600  -->  00:05:17,793
Then we've got our callable, and that is the app as well.
112

112

00:05:19,780  -->  00:05:22,670
And finally, where we're going to log to,
113

113

00:05:22,670  -->  00:05:27,657
and that's going to be in the base/log/%n.log.
114

114

00:05:28,910  -->  00:05:32,810
This is just going to create a file called uWSGI.log
115

115

00:05:32,810  -->  00:05:35,920
and is going to write our applications output
116

116

00:05:35,920  -->  00:05:39,420
and any other Python output into it.
117

117

00:05:39,420  -->  00:05:41,060
If we do have any application errors,
118

118

00:05:41,060  -->  00:05:45,513
we will be checking uWSGI.log inside the log folder.
119

119

00:05:46,810  -->  00:05:48,400
That's everything we need in this file
120

120

00:05:48,400  -->  00:05:51,390
so call on wq after pressing escape,
121

121

00:05:51,390  -->  00:05:55,690
and we are good to go with our uWSGI.ini.
122

122

00:05:55,690  -->  00:05:58,070
The next thing we have to do is create a system daemon
123

123

00:05:58,070  -->  00:06:02,490
that will take care of starting up uWSGI for us.
124

124

00:06:02,490  -->  00:06:03,510
So we're going to do
125

125

00:06:03,510  -->  00:06:06,630
sudo vi /etc/systemd/system
126

126

00:06:09,234  -->  00:06:12,734
/uwsgi_pricing_service.service.
127

127

00:06:13,780  -->  00:06:16,850
This is a new file, and we're gonna go into insert mode.
128

128

00:06:16,850  -->  00:06:19,320
And then we're gonna write a bunch of things.
129

129

00:06:19,320  -->  00:06:21,980
First, we're gonna start with a unit section.
130

130

00:06:21,980  -->  00:06:24,940
And this just describes what we're doing here
131

131

00:06:24,940  -->  00:06:26,700
so that when it appears in the logs,
132

132

00:06:26,700  -->  00:06:29,420
we will be able to identify them more easily.
133

133

00:06:29,420  -->  00:06:31,250
So we will put a description
134

134

00:06:31,250  -->  00:06:35,020
and this is gonna be uWSGI Pricing Service.
135

135

00:06:35,020  -->  00:06:37,960
Then we will go into the service configuration itself.
136

136

00:06:37,960  -->  00:06:40,620
So any information that we provide here,
137

137

00:06:40,620  -->  00:06:45,350
will be used to run this service, which runs uWSGI.
138

138

00:06:45,350  -->  00:06:49,070
So we'll say that the user that is running this service
139

139

00:06:49,070  -->  00:06:52,800
is the Jose user and the group is also Jose,
140

140

00:06:52,800  -->  00:06:55,330
then we're going to say that the working directory
141

141

00:06:55,330  -->  00:06:59,500
which is where this thing that we're running is located
142

142

00:06:59,500  -->  00:07:04,010
is in var/www/html/pricing-service.
143

143

00:07:04,010  -->  00:07:07,030
Then we will define an environment variable,
144

144

00:07:07,030  -->  00:07:08,730
which is our MONGODB_URI
145

145

00:07:09,810  -->  00:07:10,643
and that is going to be
146

146

00:07:10,643  -->  00:07:13,380
monogodb://127.0.0.1:27017
147

147

00:07:15,256  -->  00:07:16,089
/fullstack.
148

148

00:07:17,530  -->  00:07:21,360
Then we will define the command that actually starts uWSGI.
149

149

00:07:21,360  -->  00:07:24,760
So ExecStart, and this command is going to be
150

150

00:07:24,760  -->  00:07:29,760
/home/jose/.local
151

151

00:07:30,733  -->  00:07:31,566
/bin/pipenv.
152

152

00:07:33,093  -->  00:07:37,250
This is the absolute path to our pipenv installation
153

153

00:07:37,250  -->  00:07:39,440
that we have in this system.
154

154

00:07:39,440  -->  00:07:42,320
Then we're going to type run uwsgi.
155

155

00:07:42,320  -->  00:07:43,153
And what this is going to do,
156

156

00:07:43,153  -->  00:07:45,020
it's just going to launch pipenv
157

157

00:07:45,020  -->  00:07:47,930
inside this pricing service folder.
158

158

00:07:47,930  -->  00:07:50,930
And it's gonna call pipenv with these arguments
159

159

00:07:50,930  -->  00:07:54,400
run uwsgi, and that's gonna start up
160

160

00:07:54,400  -->  00:07:58,073
the uWSGI binary inside our virtual environment.
161

161

00:07:59,560  -->  00:08:02,320
Then you want to type --master --emperor
162

162

00:08:03,390  -->  00:08:06,840
and we're gonna define the configuration file for uWSGI
163

163

00:08:06,840  -->  00:08:09,143
which is uwsgi.ini.
164

164

00:08:10,310  -->  00:08:12,880
Then we're gonna say --die-on-term
165

165

00:08:12,880  -->  00:08:14,620
which is when we terminate uWSGI,
166

166

00:08:14,620  -->  00:08:16,120
which should kill the process.
167

167

00:08:17,697  -->  00:08:19,700
<v ->-uid is going to be Jose</v>
168

168

00:08:19,700  -->  00:08:21,970
and --gid is going to be Jose as well.
169

169

00:08:21,970  -->  00:08:26,470
And finally, a --logto wear any Emperor logs
170

170

00:08:26,470  -->  00:08:28,960
are going to be forwarded to.
171

171

00:08:28,960  -->  00:08:31,000
And that's gonna be /var/www
172

172

00:08:31,000  -->  00:08:32,200
Oh sorry, I actually don't need that.
173

173

00:08:32,200  -->  00:08:36,450
That's just going to be log/emperor.log.
174

174

00:08:36,450  -->  00:08:39,743
So what is emperor and what is uWSGI.
175

175

00:08:40,748  -->  00:08:45,748
uWSGI is the main programme that we talked to our Python app.
176

176

00:08:45,960  -->  00:08:48,410
And the emperor is sort of like a manager for it,
177

177

00:08:48,410  -->  00:08:50,750
that will manage the different threads and processes
178

178

00:08:50,750  -->  00:08:54,320
and kill them if they fail and restart them and so on.
179

179

00:08:54,320  -->  00:08:56,600
So we're gonna have two different log files,
180

180

00:08:56,600  -->  00:08:58,390
the uwsgi.log
181

181

00:08:58,390  -->  00:09:02,260
that contains our application, logs and things like that.
182

182

00:09:02,260  -->  00:09:04,000
And we're going to have emperor.log
183

183

00:09:04,000  -->  00:09:07,300
which contains the manager's logs.
184

184

00:09:07,300  -->  00:09:09,150
Things like if there's a failure to start up
185

185

00:09:09,150  -->  00:09:11,600
and things like that could end up in emperor.log
186

186

00:09:12,470  -->  00:09:15,040
Then we're going to type to Restart = always
187

187

00:09:16,080  -->  00:09:18,560
KillSignal is SIGQUIT
188

188

00:09:18,560  -->  00:09:20,250
Type is notify,
189

189

00:09:20,250  -->  00:09:22,930
NotifyAccess is all
190

190

00:09:22,930  -->  00:09:25,270
and then a new section for instal is gonna be
191

191

00:09:25,270  -->  00:09:29,090
WantedBy = muliti-user.target
192

192

00:09:29,090  -->  00:09:31,550
A bunch of these things you don't have to worry about again,
193

193

00:09:31,550  -->  00:09:33,720
they are things like, who should have access
194

194

00:09:33,720  -->  00:09:35,730
to run this service,
195

195

00:09:35,730  -->  00:09:39,180
when should the service start if the server restarts,
196

196

00:09:39,180  -->  00:09:41,190
and things like that.
197

197

00:09:41,190  -->  00:09:43,470
Now that we've got this we can press escape
198

198

00:09:43,470  -->  00:09:46,710
call on wq for write and quit.
199

199

00:09:46,710  -->  00:09:49,010
And there is just one more thing we have to do
200

200

00:09:49,010  -->  00:09:50,660
in case we haven't done it already,
201

201

00:09:50,660  -->  00:09:52,690
which is that we have to do
202

202

00:09:52,690  -->  00:09:57,430
touch log/emperor.log
203

203

00:09:57,430  -->  00:10:00,250
We just have to create the emperor.log file
204

204

00:10:00,250  -->  00:10:03,630
because if not the root user will create it for us
205

205

00:10:03,630  -->  00:10:07,663
and it won't have permissions for our user to write to it.
206

206

00:10:09,070  -->  00:10:10,560
Now that we've got all this,
207

207

00:10:10,560  -->  00:10:12,450
we have to do a couple of things.
208

208

00:10:12,450  -->  00:10:15,110
The first one is that we're gonna do
209

209

00:10:15,110  -->  00:10:18,810
sudo systemctl daemon-reload
210

210

00:10:18,810  -->  00:10:22,300
And that is just going to reload all the system daemons
211

211

00:10:22,300  -->  00:10:24,160
and system services that we have created.
212

212

00:10:24,160  -->  00:10:26,000
We've just created one for now.
213

213

00:10:26,000  -->  00:10:26,833
Then we're going to do
214

214

00:10:26,833  -->  00:10:30,883
sudo systemctl start uwsgi_pricing_service
215

215

00:10:32,900  -->  00:10:36,400
Hopefully, that's what happens, absolutely nothing.
216

216

00:10:36,400  -->  00:10:38,800
But if you do get an error, ask away in the course Q&amp;A
217

217

00:10:38,800  -->  00:10:40,720
we'll be more than happy to help you out.
218

218

00:10:40,720  -->  00:10:44,050
But the likely thing is that there's been a typo somewhere.
219

219

00:10:44,050  -->  00:10:46,290
So please do double check the two files
220

220

00:10:46,290  -->  00:10:47,760
that we've written so far.
221

221

00:10:47,760  -->  00:10:52,653
The uwsgi.ini and the uwsgi_pricing_service.service.
222

222

00:10:53,760  -->  00:10:54,910
The next thing to check
223

223

00:10:54,910  -->  00:10:59,910
is the log/uwsgi.log
224

224

00:10:59,930  -->  00:11:01,550
So what you should see here
225

225

00:11:01,550  -->  00:11:04,360
is if you go down to the very bottom of the file,
226

226

00:11:04,360  -->  00:11:05,900
you should see these things.
227

227

00:11:05,900  -->  00:11:10,300
Spawned uWSGI worker 1 2 3 4 5 6 7 8, so on.
228

228

00:11:10,300  -->  00:11:11,133
What this says
229

229

00:11:11,133  -->  00:11:13,820
is that you've got your eight processes there.
230

230

00:11:13,820  -->  00:11:17,540
If you see any sort of error, or anything like that,
231

231

00:11:17,540  -->  00:11:19,270
again, ask away in the course Q&amp;A
232

232

00:11:19,270  -->  00:11:21,360
or search in Google stack overflow
233

233

00:11:21,360  -->  00:11:24,383
sometimes has a bunch of nice things around this as well.
234

234

00:11:25,290  -->  00:11:27,890
But hopefully, everything's worked up to now.
235

235

00:11:27,890  -->  00:11:32,210
And we are ready to set up engine x to talk to uWSGI,
236

236

00:11:32,210  -->  00:11:33,760
which will talk to our app.
237

237

00:11:33,760  -->  00:11:35,860
And those three together are what's going
238

238

00:11:35,860  -->  00:11:38,663
to serve our users with their content.
239

239

00:11:39,600  -->  00:11:41,150
I'll see you in the next video.
