1
00:00:00,395 --> 00:00:02,418
- [Instructor] Hi and
welcome back to the course.

2
00:00:02,418 --> 00:00:06,585
In this video we're going to
set up UWSGI in our server.

3
00:00:07,806 --> 00:00:11,269
What we've done up till now
is download our application

4
00:00:11,269 --> 00:00:13,480
from GitHub, our rest api.

5
00:00:13,480 --> 00:00:16,958
We've installed python
and some development

6
00:00:16,958 --> 00:00:18,755
libraries that we've needed.

7
00:00:18,755 --> 00:00:21,669
Then we've also installed
virtual environment,

8
00:00:21,669 --> 00:00:23,202
and we've created a virtual end,

9
00:00:23,202 --> 00:00:26,035
and we've installed the
requirements.txt file

10
00:00:26,035 --> 00:00:28,389
in our application folder.

11
00:00:28,389 --> 00:00:31,830
So just to recap, what
we did was we created

12
00:00:31,830 --> 00:00:35,997
a slash/var/www/html/items-rest folder,

13
00:00:37,869 --> 00:00:41,692
and in this folder, we
downloaded from Git,

14
00:00:41,692 --> 00:00:45,377
our respsirtory, and we've
created the log folder,

15
00:00:45,377 --> 00:00:47,745
we created the virtual environment folder,

16
00:00:47,745 --> 00:00:49,951
and we setup a new virtual environment

17
00:00:49,951 --> 00:00:52,463
using Python 3.5, and then we installed

18
00:00:52,463 --> 00:00:55,376
the requirements.txt file, which includes

19
00:00:55,376 --> 00:00:57,299
everything that we need.

20
00:00:57,299 --> 00:01:00,991
This means that the stuff we've got here

21
00:01:00,991 --> 00:01:03,502
is nearly ready to be used.

22
00:01:03,502 --> 00:01:07,156
The only last thing we
have to change is a UWSGI

23
00:01:07,156 --> 00:01:08,898
and .ini file.

24
00:01:08,898 --> 00:01:11,358
And we're going to do that in a moment.

25
00:01:11,358 --> 00:01:14,804
First, we're going to create what's called

26
00:01:14,804 --> 00:01:17,115
an open to service.

27
00:01:17,115 --> 00:01:21,121
And a service is something
that you can tell Ubuntu

28
00:01:21,121 --> 00:01:24,907
to run when the computer
starts, when a server starts,

29
00:01:24,907 --> 00:01:27,772
and you can tell it to
restart when it crashes

30
00:01:27,772 --> 00:01:28,721
and things like that.

31
00:01:28,721 --> 00:01:33,536
So essentially, a service is
a descriptor of a programme,

32
00:01:33,536 --> 00:01:36,647
and you can, essentially, set the service

33
00:01:36,647 --> 00:01:39,076
to run and restart, and things like that.

34
00:01:39,076 --> 00:01:41,907
In addition, a service
also lets you do things

35
00:01:41,907 --> 00:01:46,074
like set environment variables
before the service runs,

36
00:01:47,302 --> 00:01:49,646
which is gonna be very useful.

37
00:01:49,646 --> 00:01:52,315
So let's get started
creating this service,

38
00:01:52,315 --> 00:01:54,492
and I will explain as we go along

39
00:01:54,492 --> 00:01:57,026
exactly how things work
and when we're finished

40
00:01:57,026 --> 00:01:59,626
in developing this service,
or creating this service.

41
00:01:59,626 --> 00:02:04,365
And you'll understand a bit
more why it's interesting.

42
00:02:04,365 --> 00:02:07,615
So what we're going to do is a sudo vi,

43
00:02:08,499 --> 00:02:10,943
and remember, this is going
to open up a text editor,

44
00:02:10,943 --> 00:02:13,695
which is called Vi, and
we're gonna do this command

45
00:02:13,695 --> 00:02:15,112
as the root user.

46
00:02:17,313 --> 00:02:21,480
And the file we want
to edit is /etc/systemd

47
00:02:22,388 --> 00:02:26,017
for system demon, which is
essentially just a programme

48
00:02:26,017 --> 00:02:29,755
that runs and looks at
things inside this folder,

49
00:02:29,755 --> 00:02:33,922
/system/UWSGI_items_rest.service.

50
00:02:36,423 --> 00:02:39,052
That's important, the .service at the end.

51
00:02:39,052 --> 00:02:43,282
All we're doing is creating
a new text file, essentially.

52
00:02:43,282 --> 00:02:47,449
Inside this particular
folder /etc/systemd/system,

53
00:02:48,533 --> 00:02:52,250
and Ubuntu, when we
create a file which ends

54
00:02:52,250 --> 00:02:55,991
in .service and is inside this folder,

55
00:02:55,991 --> 00:02:59,958
it starts knowing that
this is a service and that

56
00:02:59,958 --> 00:03:03,958
it can run as a service
using the system controller,

57
00:03:03,958 --> 00:03:06,373
and we're gonna look at how they compare.

58
00:03:06,373 --> 00:03:09,076
So now here it asks for my
password, which I'm going

59
00:03:09,076 --> 00:03:12,388
to put in, and then as you
can see, we create a ini file.

60
00:03:12,388 --> 00:03:16,978
Down here at the bottom you
can see that Vi test editor

61
00:03:16,978 --> 00:03:19,307
is telling us this is a new file.

62
00:03:19,307 --> 00:03:21,403
So now that I have a few parts

63
00:03:21,403 --> 00:03:23,947
to creating an Ubuntu
service, but it's really

64
00:03:23,947 --> 00:03:26,520
a quite self-explanatory as we go along.

65
00:03:26,520 --> 00:03:29,648
And the first part is a
unit, and we have to type,

66
00:03:29,648 --> 00:03:32,092
of course, the I key first.

67
00:03:32,092 --> 00:03:35,805
We're going to insert mode,
and then inside square

68
00:03:35,805 --> 00:03:38,479
brackets, we're gonna type
unit with a capital U.

69
00:03:38,479 --> 00:03:43,082
What this is a section,
and a section of a service

70
00:03:43,082 --> 00:03:46,940
allows us to define some
properties of the service.

71
00:03:46,940 --> 00:03:49,421
And one of the properties
we're going to define

72
00:03:49,421 --> 00:03:52,923
within this unit section
is the description

73
00:03:52,923 --> 00:03:55,088
of the service, and the
way we define a description

74
00:03:55,088 --> 00:03:59,254
is we see description
with a capital D, equals,

75
00:03:59,254 --> 00:04:03,421
and then we can type in a
description of our service.

76
00:04:04,489 --> 00:04:06,620
This is going to be useful, for example,

77
00:04:06,620 --> 00:04:08,211
when we run the service.

78
00:04:08,211 --> 00:04:10,653
It's going to say we're
running this service

79
00:04:10,653 --> 00:04:13,725
that we are describing, and
that's going to be useful

80
00:04:13,725 --> 00:04:17,257
if an error happens, we
can then go back and look

81
00:04:17,257 --> 00:04:18,485
for this description.

82
00:04:18,485 --> 00:04:22,152
It will appear in all the
logs and things like that.

83
00:04:22,152 --> 00:04:25,751
So something that describes
the service is useful.

84
00:04:25,751 --> 00:04:28,918
I'm going to call it UWSGI items rest.

85
00:04:30,865 --> 00:04:32,274
I think it's fairly explanatory.

86
00:04:32,274 --> 00:04:34,283
We've got an items rest api.

87
00:04:34,283 --> 00:04:37,584
This is a UWSGI service that
we're going to be running,

88
00:04:37,584 --> 00:04:40,554
so whenever we see this
in the logs later on,

89
00:04:40,554 --> 00:04:43,354
we're gonna know that
this is what this means.

90
00:04:43,354 --> 00:04:44,854
This service here.

91
00:04:46,071 --> 00:04:48,514
The next section is service.

92
00:04:48,514 --> 00:04:51,702
Once again, inside square
brackets with a capital S,

93
00:04:51,702 --> 00:04:55,932
we'll type service, and now
we've defined the unit section.

94
00:04:55,932 --> 00:04:59,043
Whenever the system
controller runs this service,

95
00:04:59,043 --> 00:05:01,547
it's gonna look at the unit
section, find the description,

96
00:05:01,547 --> 00:05:02,823
and use that description.

97
00:05:02,823 --> 00:05:04,920
Then it's gonna look
at the service section

98
00:05:04,920 --> 00:05:06,698
and it's gonna look at all the properties

99
00:05:06,698 --> 00:05:07,982
we're gonna define here.

100
00:05:07,982 --> 00:05:11,431
It's going to run the service
using these properties.

101
00:05:11,431 --> 00:05:13,896
The first property that we're going

102
00:05:13,896 --> 00:05:16,609
to define is environment.

103
00:05:16,609 --> 00:05:19,565
And remember, you put a
capital E, and what this does

104
00:05:19,565 --> 00:05:22,248
is it allows us to define
an environment variable

105
00:05:22,248 --> 00:05:25,622
right before the service starts.

106
00:05:25,622 --> 00:05:29,333
Remember, this service
is going to run UWSGI,

107
00:05:29,333 --> 00:05:32,666
and UWSGI is going to run our Flask app.

108
00:05:33,955 --> 00:05:38,122
So what environment variable
do we need in our Flask app?

109
00:05:50,245 --> 00:05:51,511
I hope you got that.

110
00:05:51,511 --> 00:05:55,372
The environment variable
we need is DATABASE_URL,

111
00:05:55,372 --> 00:05:57,965
and that's because, if you
remember from the last section

112
00:05:57,965 --> 00:06:02,654
on hero deployments, we
changed our app, our api,

113
00:06:02,654 --> 00:06:05,635
to use a DATABASE_URL
environment variable,

114
00:06:05,635 --> 00:06:07,445
and if you haven't done the last section,

115
00:06:07,445 --> 00:06:08,950
and I'll tell you now.

116
00:06:08,950 --> 00:06:11,209
Instead of sticking with
SQL Lite, we're going

117
00:06:11,209 --> 00:06:14,768
to use Posher SQL, which you know,

118
00:06:14,768 --> 00:06:18,549
and instead of hard coding
the URL in the code,

119
00:06:18,549 --> 00:06:21,100
we're going to be defining the URL

120
00:06:21,100 --> 00:06:23,061
inside this environment variable,

121
00:06:23,061 --> 00:06:26,186
but this is a variable
that this server remembers,

122
00:06:26,186 --> 00:06:28,766
and then we're gonna tell our Python code

123
00:06:28,766 --> 00:06:31,232
to read this environment to find out

124
00:06:31,232 --> 00:06:35,146
what the DATABASE_URL is,
and then it's gonna access

125
00:06:35,146 --> 00:06:37,128
the database on that URL.

126
00:06:37,128 --> 00:06:40,038
And that's better than
putting in the code,

127
00:06:40,038 --> 00:06:42,350
because if we leave it
in the code and people

128
00:06:42,350 --> 00:06:44,636
are gonna be able to look at the database

129
00:06:44,636 --> 00:06:48,999
username and password
that we have defined.

130
00:06:48,999 --> 00:06:50,332
Not ideal, okay.

131
00:06:52,109 --> 00:06:55,707
So we're gonna say DATABASE_URL, equals,

132
00:06:55,707 --> 00:06:59,108
and here we go with the
database URL for postgres,

133
00:06:59,108 --> 00:07:02,301
which is postgres://,

134
00:07:02,301 --> 00:07:03,772
this is necessary.

135
00:07:03,772 --> 00:07:04,787
You cannot skip this bit.

136
00:07:04,787 --> 00:07:09,214
And then we're going to
type in our postgres user

137
00:07:09,214 --> 00:07:13,135
that we created using a
create user earlier on

138
00:07:13,135 --> 00:07:16,762
in this section, which
in my case, is Jose.

139
00:07:16,762 --> 00:07:19,264
Then we're gonna put the
colon, and then we're going

140
00:07:19,264 --> 00:07:21,720
to put the password,
which in my case is 1234.

141
00:07:21,720 --> 00:07:25,145
And you may have chosen
a more secure password.

142
00:07:25,145 --> 00:07:28,645
Okay, so that's the user, colon, password,

143
00:07:30,019 --> 00:07:32,349
and then we're going to
type in the at symbol,

144
00:07:32,349 --> 00:07:35,593
and where we're gonna put localhost.

145
00:07:35,593 --> 00:07:39,739
Localhost is a server that
this programme is running on.

146
00:07:39,739 --> 00:07:42,253
So what this is going to do is it's going

147
00:07:42,253 --> 00:07:45,253
to try to access the postgres server

148
00:07:46,270 --> 00:07:48,960
running on the same server as the app,

149
00:07:48,960 --> 00:07:52,793
using the username Jose
and the password 1234.

150
00:07:53,716 --> 00:07:56,061
This does means that we
can access the postgres

151
00:07:56,061 --> 00:07:59,010
server on a different
server if we have it setup,

152
00:07:59,010 --> 00:08:00,194
which we don't.

153
00:08:00,194 --> 00:08:01,847
But potentially, if you wanted to separate

154
00:08:01,847 --> 00:08:05,191
your postgres and your flash
servers, you could do that

155
00:08:05,191 --> 00:08:06,231
and that would be fine.

156
00:08:06,231 --> 00:08:09,358
You will have to set
the postgres to enable

157
00:08:09,358 --> 00:08:11,005
you to do that.

158
00:08:11,005 --> 00:08:13,896
Finally, after the local
host, we're gonna put

159
00:08:13,896 --> 00:08:16,826
a colon again and 5432, which is the port

160
00:08:16,826 --> 00:08:19,056
that postgres was running on, as we saw

161
00:08:19,056 --> 00:08:20,995
earlier in this section.

162
00:08:20,995 --> 00:08:22,749
Okay, so looks a bit confusing.

163
00:08:22,749 --> 00:08:26,832
Username, colon, password,
@ server, colon, port.

164
00:08:27,872 --> 00:08:31,420
Okay, so the server is local hosting now.

165
00:08:31,420 --> 00:08:33,956
Finally, at the very end, we're gonna put

166
00:08:33,956 --> 00:08:36,107
a forward slash and we're
going to put Jose again.

167
00:08:36,107 --> 00:08:38,686
And now this is not the user,

168
00:08:38,686 --> 00:08:41,844
this is the database that we're accessing.

169
00:08:41,844 --> 00:08:44,893
So in my case, I've created
a database called Jose

170
00:08:44,893 --> 00:08:47,595
to match my user, but
potentially you could've created

171
00:08:47,595 --> 00:08:51,762
a database such as fullstack
or rest.api, or something.

172
00:08:54,015 --> 00:08:56,029
Sorry, that was misspelt.

173
00:08:56,029 --> 00:08:58,032
So this is just the
database you've created,

174
00:08:58,032 --> 00:09:00,714
and you must make sure that your Jose user

175
00:09:00,714 --> 00:09:03,124
has access to that database,
which it most likely

176
00:09:03,124 --> 00:09:04,457
will be default.

177
00:09:05,880 --> 00:09:06,713
Now okay.

178
00:09:07,791 --> 00:09:10,178
That is the environment
variable we're going to define.

179
00:09:10,178 --> 00:09:13,813
So right before running the Flask app,

180
00:09:13,813 --> 00:09:17,998
or the UWSGI app, we're just
gonna run the Flask app.

181
00:09:17,998 --> 00:09:20,926
We're going to define
this environment variable,

182
00:09:20,926 --> 00:09:22,471
and the server is going to know

183
00:09:22,471 --> 00:09:24,617
that this environment variable exists.

184
00:09:24,617 --> 00:09:26,962
Then our Python code is going to access it

185
00:09:26,962 --> 00:09:28,962
to retrieve this string.

186
00:09:35,651 --> 00:09:37,794
Next, we're going to type ExecStart.

187
00:09:37,794 --> 00:09:41,993
Notice how the E is capital
and the S is capital?

188
00:09:41,993 --> 00:09:45,576
And this is what programme
this service runs.

189
00:09:46,684 --> 00:09:50,881
Remember, a service descriptor
is used to tell Ubuntu

190
00:09:50,881 --> 00:09:55,090
that this is a service that can be started

191
00:09:55,090 --> 00:09:57,071
when the computer or the server starts,

192
00:09:57,071 --> 00:09:59,627
it can be restarted if it
crashes, and things like that.

193
00:09:59,627 --> 00:10:01,586
And Ubuntu will take care of that for us,

194
00:10:01,586 --> 00:10:05,255
but we have to tell it
what programme we want

195
00:10:05,255 --> 00:10:06,997
the service to run, and
in this case, we want

196
00:10:06,997 --> 00:10:09,997
to tell it to run the UWSGI programme,

197
00:10:11,448 --> 00:10:14,961
which is then going to run our Flask app.

198
00:10:14,961 --> 00:10:19,426
So the way we tell it is
by using the normal Unix

199
00:10:19,426 --> 00:10:21,289
notation for running a programme.

200
00:10:21,289 --> 00:10:23,208
The first thing we do is we
tell it the programme name.

201
00:10:23,208 --> 00:10:27,375
So
/var/www/html/items-rest/venv/bin/UWSGI.

202
00:10:32,302 --> 00:10:35,843
So when we've installed
UWSGI, that has created

203
00:10:35,843 --> 00:10:39,093
a UWSGI executable file that we can run

204
00:10:40,420 --> 00:10:42,549
inside the virtual environment.

205
00:10:42,549 --> 00:10:46,467
So we have our project folder, the virtual

206
00:10:46,467 --> 00:10:49,668
environment folder, and
then the binaries folder,

207
00:10:49,668 --> 00:10:50,801
the bin folder.

208
00:10:50,801 --> 00:10:52,296
We've got UWSGI.

209
00:10:52,296 --> 00:10:57,192
We can run UWSGI, and that
starts the UWSGI process.

210
00:10:57,192 --> 00:11:00,211
Then we're going to tell it --master,

211
00:11:00,211 --> 00:11:02,083
after a space, remember.

212
00:11:02,083 --> 00:11:03,904
Space there and then
--master, and what this does

213
00:11:03,904 --> 00:11:06,332
is it tells it to use a master process.

214
00:11:06,332 --> 00:11:09,982
Master process is a way
that UWSGI has of keeping

215
00:11:09,982 --> 00:11:12,894
track of other processes in
case we use more than one.

216
00:11:12,894 --> 00:11:17,167
So it can restart them if they
crash and if they get blocked

217
00:11:17,167 --> 00:11:19,098
and things like that.

218
00:11:19,098 --> 00:11:21,634
Then we're going to use --emperor.

219
00:11:21,634 --> 00:11:25,801
Emperor, which is essentially
a controller as well.

220
00:11:28,944 --> 00:11:31,903
And then we're going to tell
it the configuration file

221
00:11:31,903 --> 00:11:36,070
for UWSGI, which is in
/var/www/html/items-rest/UWSGI.ini.

222
00:11:41,360 --> 00:11:44,263
And we created this in
the last section and in

223
00:11:44,263 --> 00:11:46,463
hero deployments as well.

224
00:11:46,463 --> 00:11:49,635
And it's got, essentially,
how to run the Flask app.

225
00:11:49,635 --> 00:11:51,584
That's what this file contains.

226
00:11:51,584 --> 00:11:55,071
It tells UWSGI what Python code it

227
00:11:55,071 --> 00:11:57,969
is meant to interact with.

228
00:11:57,969 --> 00:12:01,437
Then we're going to say --die-on-term,

229
00:12:01,437 --> 00:12:03,699
and that is essentially when we terminate

230
00:12:03,699 --> 00:12:05,315
the UWSGI process.

231
00:12:05,315 --> 00:12:08,225
That's going to take
the Python code as well.

232
00:12:08,225 --> 00:12:10,269
And so it doesn't keep
running in the background

233
00:12:10,269 --> 00:12:12,186
without doing anything.

234
00:12:13,111 --> 00:12:15,944
Finally, we're going to say --uid,

235
00:12:17,768 --> 00:12:20,705
and then I'm going to type in
Jose, which is my username.

236
00:12:20,705 --> 00:12:22,729
You should type your own username here.

237
00:12:22,729 --> 00:12:26,896
And what this is please run
the UWSGI process as Jose.

238
00:12:28,416 --> 00:12:31,893
Remember, in the past,
we accessed the process

239
00:12:31,893 --> 00:12:34,813
as the postgres user by doing something

240
00:12:34,813 --> 00:12:37,230
like psudeo -you -I postgres.

241
00:12:38,396 --> 00:12:40,089
And this is essentially gonna do the same,

242
00:12:40,089 --> 00:12:42,957
but it's going to run the UWSGI process

243
00:12:42,957 --> 00:12:47,632
as the Jose user, which
is good because Jose user

244
00:12:47,632 --> 00:12:51,034
owns this folder here,
so the user has access

245
00:12:51,034 --> 00:12:53,613
to doing everything inside this folder.

246
00:12:53,613 --> 00:12:56,963
Now similarly, GID for the group ID,

247
00:12:56,963 --> 00:12:58,905
which is also going to be Jose.

248
00:12:58,905 --> 00:13:00,615
So we want to make sure that both the user

249
00:13:00,615 --> 00:13:04,365
and the group match the
owner of this folder.

250
00:13:06,612 --> 00:13:09,571
And finally, we're gonna
tell it the log folder.

251
00:13:09,571 --> 00:13:10,457
So we're gonna say

252
00:13:10,457 --> 00:13:14,624
logto/var/www/html/items-rest/log/emperor.log.

253
00:13:20,204 --> 00:13:21,687
And what this is going to do is it's going

254
00:13:21,687 --> 00:13:24,238
to tell it where to write the log files

255
00:13:24,238 --> 00:13:26,571
for the emperor and process.

256
00:13:31,475 --> 00:13:32,487
That's it.

257
00:13:32,487 --> 00:13:37,064
That's the XX start and
sort of property, and now

258
00:13:37,064 --> 00:13:40,338
whenever we run this service
using the system controller,

259
00:13:40,338 --> 00:13:42,961
it is going to run this command there.

260
00:13:42,961 --> 00:13:45,689
Before running that command,
it's going to make sure

261
00:13:45,689 --> 00:13:48,004
that this environment variable exists.

262
00:13:48,004 --> 00:13:50,784
And so after running
this command, our Python

263
00:13:50,784 --> 00:13:53,622
code will be able to
access the database URL

264
00:13:53,622 --> 00:13:55,705
and environment variable.

265
00:13:56,787 --> 00:13:59,241
Finally, we're going
to say restart=always,

266
00:13:59,241 --> 00:14:01,289
and that's just gonna mean if the process

267
00:14:01,289 --> 00:14:04,503
crashes or stopped, then
the assessment controller

268
00:14:04,503 --> 00:14:07,949
is going to restart the
process so it's not dead

269
00:14:07,949 --> 00:14:10,884
for a long period of time.

270
00:14:10,884 --> 00:14:13,146
Finally, we're going to
give it a kill signal,

271
00:14:13,146 --> 00:14:17,156
which his SIGQUIT, and this
is reasonably complicated

272
00:14:17,156 --> 00:14:20,304
and Unix set of terminology.

273
00:14:20,304 --> 00:14:23,829
There are many different
ways to terminate a process,

274
00:14:23,829 --> 00:14:27,996
but SEGQUIT is one of them,
and that doesn't force quit.

275
00:14:30,680 --> 00:14:34,662
So it tells it to exit, then
it will shut down gracefully

276
00:14:34,662 --> 00:14:38,280
without losing data, things like that.

277
00:14:38,280 --> 00:14:41,030
And it's good to have that there.

278
00:14:43,458 --> 00:14:47,625
We're going to type Type=notify,
and notifyAccess=all.

279
00:14:49,882 --> 00:14:53,050
So these are just the
notification parameters

280
00:14:53,050 --> 00:14:54,467
for this service.

281
00:14:56,882 --> 00:15:00,838
Finally, we're going to have
a last section called instal.

282
00:15:00,838 --> 00:15:03,709
And this is not a very
good name for this section.

283
00:15:03,709 --> 00:15:06,032
And also not a very good
name for the property,

284
00:15:06,032 --> 00:15:08,615
WantedBy=mult-user.target.

285
00:15:09,658 --> 00:15:11,825
And as weird as it sounds,

286
00:15:12,861 --> 00:15:15,223
what this does is it allows us to start

287
00:15:15,223 --> 00:15:18,306
the service when the server boots up,

288
00:15:19,545 --> 00:15:23,588
which is a rather weird
naming convention there,

289
00:15:23,588 --> 00:15:26,303
but never-the-less, that's what that does.

290
00:15:26,303 --> 00:15:29,803
So now we've got our service created here,

291
00:15:31,403 --> 00:15:35,096
and remember every character
counts, and every space

292
00:15:35,096 --> 00:15:38,107
and everything that we have typed here.

293
00:15:38,107 --> 00:15:40,502
So have a really close look at this

294
00:15:40,502 --> 00:15:43,187
and make sure that you've
got it exactly like that.

295
00:15:43,187 --> 00:15:45,908
You should only change
your username and password

296
00:15:45,908 --> 00:15:48,825
and database, and also UID and GID.

297
00:15:52,419 --> 00:15:55,041
The rest should stay the same.

298
00:15:55,041 --> 00:15:58,941
And when you're done, press the escape key

299
00:15:58,941 --> 00:16:01,691
and call on WQ to write and quit.

300
00:16:02,722 --> 00:16:05,305
And that's the service created.

301
00:16:06,597 --> 00:16:08,930
So that is pretty fantastic.

302
00:16:11,990 --> 00:16:14,624
There is one last thing
that we have to do as well

303
00:16:14,624 --> 00:16:17,374
which is, as I mentioned earlier,

304
00:16:18,419 --> 00:16:21,296
change the UWSGI.ini file.

305
00:16:21,296 --> 00:16:23,344
So what we're going to do is we're going

306
00:16:23,344 --> 00:16:24,943
to delete all of that.

307
00:16:24,943 --> 00:16:27,290
And it's here, because this was the Horoku

308
00:16:27,290 --> 00:16:29,285
UWSGI.ini configuration.

309
00:16:29,285 --> 00:16:32,040
And the way to delete things in Vi

310
00:16:32,040 --> 00:16:35,707
is to press the D key,
followed by shift, G.

311
00:16:37,441 --> 00:16:39,508
And that deletes everything.

312
00:16:39,508 --> 00:16:42,144
Then we're going to press the
I key to go into insert mode,

313
00:16:42,144 --> 00:16:44,513
and we're going to
write a few things here,

314
00:16:44,513 --> 00:16:46,543
so please bare with me.

315
00:16:46,543 --> 00:16:48,817
Once again, a section called UWSGI.

316
00:16:48,817 --> 00:16:50,346
That is going to be the only section

317
00:16:50,346 --> 00:16:51,846
in this .ini file.

318
00:16:54,143 --> 00:16:56,664
And we're going to start
by finding the base folder

319
00:16:56,664 --> 00:16:59,247
where the UWSGI programme exists.

320
00:17:02,488 --> 00:17:06,645
And that's just in the
base folder of our app.

321
00:17:06,645 --> 00:17:08,745
Then we're going to tell it which file

322
00:17:08,745 --> 00:17:12,912
to find the app in, which
could be our app.py file,

323
00:17:14,105 --> 00:17:17,605
but it's in fact going to be our run file,

324
00:17:18,597 --> 00:17:20,249
or maybe you didn't
follow the last section,

325
00:17:20,249 --> 00:17:23,323
but in the last section,
the run file was created

326
00:17:23,323 --> 00:17:25,564
to make sure that the
database was initialised

327
00:17:25,564 --> 00:17:29,231
before the app was ran,
so that's important.

328
00:17:30,512 --> 00:17:34,679
And the module that we're
importing is the same one.

329
00:17:36,090 --> 00:17:37,588
App.

330
00:17:37,588 --> 00:17:41,902
And what this does is that
this loads that variable there.

331
00:17:41,902 --> 00:17:44,935
So the module we're
importing is the app module,

332
00:17:44,935 --> 00:17:47,018
which is the run.py file.

333
00:17:49,173 --> 00:17:53,006
And UWSGI is gonna use
this variable later on.

334
00:17:54,600 --> 00:17:57,517
Okay, the home of the UWSGI process

335
00:17:59,526 --> 00:18:04,263
is the base folder /venv, because it lives

336
00:18:04,263 --> 00:18:07,214
inside the virtual environment UWSGI.

337
00:18:07,214 --> 00:18:09,785
And apologies, this
should not be a dollar,

338
00:18:09,785 --> 00:18:11,957
it should be a percent symbol there.

339
00:18:11,957 --> 00:18:16,124
That's how we used previously
defined variables like that.

340
00:18:17,102 --> 00:18:18,380
The Python path.

341
00:18:18,380 --> 00:18:22,151
This is a pretty funny
environment variable as well,

342
00:18:22,151 --> 00:18:24,845
but the Python path essentially defines

343
00:18:24,845 --> 00:18:28,115
where your Python project beings.

344
00:18:28,115 --> 00:18:31,433
So what is the root folder
of your Python project?

345
00:18:31,433 --> 00:18:35,100
And in this case, it's
also the base folder.

346
00:18:36,521 --> 00:18:40,688
Now the socket file is in
the base folder /socket.sock.

347
00:18:43,638 --> 00:18:46,147
Don't know if you guys
remember that we defined

348
00:18:46,147 --> 00:18:50,503
this earlier on in the section
and the socket.sock file

349
00:18:50,503 --> 00:18:53,751
is what's going to allow communication

350
00:18:53,751 --> 00:18:57,549
between the UWSGI process and engine.exe.

351
00:18:57,549 --> 00:19:01,122
And so it's going to be
a file there that allows

352
00:19:01,122 --> 00:19:02,705
that communication.

353
00:19:03,803 --> 00:19:07,220
This socket must have permissions of 777.

354
00:19:08,544 --> 00:19:11,948
So we are going to change the permissions

355
00:19:11,948 --> 00:19:15,768
of the socket to 777, and
I'm not gonna get into

356
00:19:15,768 --> 00:19:19,187
exactly all the Unix
permissions, but just remember

357
00:19:19,187 --> 00:19:23,444
777 means that anybody can
access this socket file.

358
00:19:23,444 --> 00:19:26,313
Any user in the system and
any group in the system,

359
00:19:26,313 --> 00:19:28,047
and access this socket file.

360
00:19:28,047 --> 00:19:29,742
That's what this means.

361
00:19:29,742 --> 00:19:32,099
And that's important to
make sure that engine.exe

362
00:19:32,099 --> 00:19:34,959
can access the socket file.

363
00:19:34,959 --> 00:19:38,065
Okay, then we're going to tell
it the number of processes

364
00:19:38,065 --> 00:19:40,223
that we want, and I'm going to say eight

365
00:19:40,223 --> 00:19:42,637
and although that may
be a bit high depending

366
00:19:42,637 --> 00:19:45,543
on what sort of droplet we're using.

367
00:19:45,543 --> 00:19:48,929
And the processes is a
matter of being nominated,

368
00:19:48,929 --> 00:19:51,263
and if things go well
or they act too slow,

369
00:19:51,263 --> 00:19:53,035
you can try and play around with that.

370
00:19:53,035 --> 00:19:55,077
It's not something that
I can normally change.

371
00:19:55,077 --> 00:19:59,196
And threads, I'm going
to give it eight as well.

372
00:19:59,196 --> 00:20:03,208
So each process is going to
have eight threads, and again,

373
00:20:03,208 --> 00:20:04,554
this is something that you can play with.

374
00:20:04,554 --> 00:20:06,351
It depends on the process
that you're using.

375
00:20:06,351 --> 00:20:10,767
Many of these solution boxes,
this may be a bit too high.

376
00:20:10,767 --> 00:20:12,350
But never-the-less.

377
00:20:13,427 --> 00:20:15,615
Then we're also going
to define the harakiri,

378
00:20:15,615 --> 00:20:18,011
which is going to be 15 seconds,

379
00:20:18,011 --> 00:20:20,475
and the harakiri comes
from the Japanese thing

380
00:20:20,475 --> 00:20:24,012
that the samurais did, which
is essentially kill themselves

381
00:20:24,012 --> 00:20:26,511
if they did something wrong, and here

382
00:20:26,511 --> 00:20:27,775
it means the same thing.

383
00:20:27,775 --> 00:20:31,165
If one of these threads gets blocked

384
00:20:31,165 --> 00:20:32,918
or has an error and it shuts down,

385
00:20:32,918 --> 00:20:35,352
then after 15 seconds,
we're going to destroy

386
00:20:35,352 --> 00:20:37,391
the thread and create another one.

387
00:20:37,391 --> 00:20:40,859
And the emperor, the
UWSGI emperor is going

388
00:20:40,859 --> 00:20:43,109
to be responsible for that.

389
00:20:44,663 --> 00:20:47,688
Finally, we're gonna define a callable,

390
00:20:47,688 --> 00:20:50,203
which is going to be app.

391
00:20:50,203 --> 00:20:53,203
And this app is not the file app.py.

392
00:20:54,040 --> 00:20:58,026
This is the variable app that is imported

393
00:20:58,026 --> 00:20:59,609
in the run.py file.

394
00:21:00,541 --> 00:21:02,734
So what UWSGI is gonna see,

395
00:21:02,734 --> 00:21:05,451
it's going to get this
module and it's going

396
00:21:05,451 --> 00:21:07,531
to find this variable in the module

397
00:21:07,531 --> 00:21:09,972
and it's going to run that Flask app.

398
00:21:09,972 --> 00:21:12,472
And so that's important there.

399
00:21:13,775 --> 00:21:16,080
And also we want to define a place

400
00:21:16,080 --> 00:21:18,247
to log the Python app out.

401
00:21:19,799 --> 00:21:21,730
So this is exactly what you saw

402
00:21:21,730 --> 00:21:24,135
in your terminal when
you ran your Flask app,

403
00:21:24,135 --> 00:21:26,276
such as the request coming in,

404
00:21:26,276 --> 00:21:28,126
and the errors and things like that.

405
00:21:28,126 --> 00:21:29,959
We want to log them
somewhere, and where we want

406
00:21:29,959 --> 00:21:34,126
to log them is
/var/www/html/items-rest/log

407
00:21:37,493 --> 00:21:39,300
as the log folder we created earlier,

408
00:21:39,300 --> 00:21:41,654
and then %n.log.

409
00:21:41,654 --> 00:21:45,237
And %n is basically going to be UWSGI.

410
00:21:46,596 --> 00:21:50,679
It's going to log the
Python output to UWSGI.log.

411
00:21:51,863 --> 00:21:53,196
Okay, that's it.

412
00:21:54,727 --> 00:21:59,125
So press the escape file
and :wq will write and quit,

413
00:21:59,125 --> 00:22:00,916
and remember what we've done here

414
00:22:00,916 --> 00:22:04,666
is told the UWSGI how
to run the Python code.

415
00:22:06,784 --> 00:22:07,617
Okay.

416
00:22:08,829 --> 00:22:12,267
And finally, we can startup our Flask app

417
00:22:12,267 --> 00:22:15,502
by doing sudo system controller,

418
00:22:15,502 --> 00:22:18,502
system ctl, start, UWSGI_items_rest.

419
00:22:21,206 --> 00:22:23,542
This here is the name of the service

420
00:22:23,542 --> 00:22:26,328
that we defined earlier, and that is going

421
00:22:26,328 --> 00:22:30,760
to go into the service,
run the XX start command,

422
00:22:30,760 --> 00:22:33,298
which is going to run
UWSGI, which is going

423
00:22:33,298 --> 00:22:35,756
to look at the UWSGI.ini file,

424
00:22:35,756 --> 00:22:37,743
which is going to run
the Flask app and create

425
00:22:37,743 --> 00:22:40,050
the socket file, which
is going to communicate

426
00:22:40,050 --> 00:22:42,588
with engine.exe to route the requests

427
00:22:42,588 --> 00:22:44,251
to our Flask app.

428
00:22:44,251 --> 00:22:47,508
And so hopefully, all that makes sense.

429
00:22:47,508 --> 00:22:50,388
And once again, we need
the password in here.

430
00:22:50,388 --> 00:22:51,802
And that's it running.

431
00:22:51,802 --> 00:22:54,536
Now the first thing to
do, once we do this,

432
00:22:54,536 --> 00:22:57,619
is to go into the log folder and look

433
00:22:58,729 --> 00:23:00,535
at the UWSGI log.

434
00:23:00,535 --> 00:23:02,479
Then we're going to go
down to the very bottom

435
00:23:02,479 --> 00:23:06,418
and see if things are working
fine, or if there's a problem.

436
00:23:06,418 --> 00:23:09,584
As you can see, we've
spawned the UWSGI processes,

437
00:23:09,584 --> 00:23:12,654
but there's nothing else left
down here, so we may have

438
00:23:12,654 --> 00:23:14,599
accessed this a bit earlier.

439
00:23:14,599 --> 00:23:16,808
I'm going to type :q to exit,

440
00:23:16,808 --> 00:23:19,113
and then access the log file again,

441
00:23:19,113 --> 00:23:20,717
and we're going to go
down to the very bottom

442
00:23:20,717 --> 00:23:23,403
again and see if there's
anything new there.

443
00:23:23,403 --> 00:23:26,653
Nothing new, so we're going to then run

444
00:23:29,687 --> 00:23:31,821
the app and see what happens.

445
00:23:31,821 --> 00:23:33,870
So see you in the very next video

446
00:23:33,870 --> 00:23:35,983
and then we're going to
look at the logs again

447
00:23:35,983 --> 00:23:39,034
after running a request and see if more

448
00:23:39,034 --> 00:23:41,222
things will be there or not.

449
00:23:41,222 --> 00:23:43,862
If things don't appear,
it means that somewhere

450
00:23:43,862 --> 00:23:47,437
our connection between engine.ext, UWSGI,

451
00:23:47,437 --> 00:23:49,801
and our Flask app has gone wrong

452
00:23:49,801 --> 00:23:52,833
and the requests are not
reaching the Flask app.

453
00:23:52,833 --> 00:23:56,221
But if things do appear, then
hopefully it's not an error,

454
00:23:56,221 --> 00:23:58,584
and then everything should be working.

455
00:23:58,584 --> 00:24:00,709
So without further ado, let's go over

456
00:24:00,709 --> 00:24:04,023
to the next video and we're
going to look at that.

