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

2
00:00:02,273 --> 00:00:04,897
In this video we're going
to be installing Nginx

3
00:00:04,897 --> 00:00:06,564
and running our app.

4
00:00:07,880 --> 00:00:11,864
Nginx is what's called a reverse proxy.

5
00:00:11,864 --> 00:00:14,320
What that means is that Nginx is going to

6
00:00:14,320 --> 00:00:18,487
act as a gateway between our
application and external users.

7
00:00:20,440 --> 00:00:23,113
It's going to accept incoming requests,

8
00:00:23,113 --> 00:00:23,984
and then it's going to decide

9
00:00:23,984 --> 00:00:26,224
what to do with those requests.

10
00:00:26,224 --> 00:00:28,288
We are going to configure Nginx

11
00:00:28,288 --> 00:00:32,216
so those requests go
straight to our application.

12
00:00:32,216 --> 00:00:34,688
Now, you may wonder,
well, why do we need that?

13
00:00:34,688 --> 00:00:37,302
And I've got to say
that's a good question.

14
00:00:37,302 --> 00:00:41,469
Nginx will also do things
like communicate with uwsgi

15
00:00:42,518 --> 00:00:46,654
to enable multi-threaded
operation of our Flask app.

16
00:00:46,654 --> 00:00:48,510
It can also do things
like allow you to run

17
00:00:48,510 --> 00:00:52,294
multiple Flask apps
simultaneously in your server

18
00:00:52,294 --> 00:00:56,838
while redirecting incoming
requests to different apps

19
00:00:56,838 --> 00:00:59,262
depending on some parameters.

20
00:00:59,262 --> 00:01:01,318
So it's overall a nice piece of software

21
00:01:01,318 --> 00:01:04,086
to have in your server,
and it's very useful,

22
00:01:04,086 --> 00:01:06,013
and it's also really fast,

23
00:01:06,013 --> 00:01:10,421
so it's not going to slow
down your app at all.

24
00:01:10,421 --> 00:01:12,926
The first thing to do is
to log in to your server,

25
00:01:12,926 --> 00:01:16,062
and then sudo apt-get update.

26
00:01:16,062 --> 00:01:19,406
Now, apt-get is going to communicate

27
00:01:19,406 --> 00:01:22,557
with the Ubuntu package manager

28
00:01:22,557 --> 00:01:25,174
and it's going to change some stuff.

29
00:01:25,174 --> 00:01:27,934
It's going to essentially
change the version number,

30
00:01:27,934 --> 00:01:28,910
some things like that.

31
00:01:28,910 --> 00:01:32,662
So you have to be root to
be able to run this command.

32
00:01:32,662 --> 00:01:34,621
And sudo does that.

33
00:01:34,621 --> 00:01:38,534
It says, run the
following command as root.

34
00:01:38,534 --> 00:01:40,294
So we're going to press enter,

35
00:01:40,294 --> 00:01:43,750
and then we're going to
run the command as sudo,

36
00:01:43,750 --> 00:01:45,342
and you may have to put in your password,

37
00:01:45,342 --> 00:01:48,286
so just do that if that's required.

38
00:01:48,286 --> 00:01:51,286
And then sudo apt-get instal nginx.

39
00:01:52,238 --> 00:01:55,154
And that's going to instal Nginx there.

40
00:01:55,154 --> 00:01:57,030
Once again, I'm going to press enter

41
00:01:57,030 --> 00:02:01,197
when it asks me if I want
to continue to continue.

42
00:02:17,734 --> 00:02:20,501
Now that Nginx is installed,

43
00:02:20,501 --> 00:02:24,758
you can see that we've
set up a bunch of things,

44
00:02:24,758 --> 00:02:26,716
and there's one thing that we must do,

45
00:02:26,716 --> 00:02:30,484
which is let our firewall have access,

46
00:02:30,484 --> 00:02:33,260
well, let Nginx have access
through the firewall,

47
00:02:33,260 --> 00:02:36,021
so the incoming requests
don't get blocked.

48
00:02:36,021 --> 00:02:37,180
So what we're going to do is say

49
00:02:37,180 --> 00:02:41,180
sudo ufw status, and ufs
is the Ubuntu firewall.

50
00:02:43,316 --> 00:02:45,164
And as you can see, it
says status: inactive,

51
00:02:45,164 --> 00:02:48,972
so we're going to enable the firewall

52
00:02:48,972 --> 00:02:51,222
to improve the connections,

53
00:02:52,580 --> 00:02:54,844
and it says that it may disrupt
existing ssh connections,

54
00:02:54,844 --> 00:02:56,916
but it doesn't.

55
00:02:56,916 --> 00:02:59,044
And finally we have to give Nginx access,

56
00:02:59,044 --> 00:03:03,628
so we're going to do sudo
ufw allow 'Nginx HTTP'

57
00:03:03,628 --> 00:03:07,236
and make sure to put that
inside quotation marks.

58
00:03:07,236 --> 00:03:08,260
And what that's going to do

59
00:03:08,260 --> 00:03:11,843
is it's going to put
Nginx in the firewall.

60
00:03:12,696 --> 00:03:17,089
And that's the rule added, and
now if we do sudo ufw status,

61
00:03:17,089 --> 00:03:20,339
you can see that Nginx HTTP is allowed.

62
00:03:25,031 --> 00:03:28,003
Now, we're going to make
sure that Nginx is running,

63
00:03:28,003 --> 00:03:29,051
and it should be running,

64
00:03:29,051 --> 00:03:30,291
and the way we check that in Ubuntu

65
00:03:30,291 --> 00:03:32,043
is with the system controller.

66
00:03:32,043 --> 00:03:34,210
So systemctl status nginx.

67
00:03:37,907 --> 00:03:41,507
And we're going to be using
systemctl quite a bit,

68
00:03:41,507 --> 00:03:44,451
so it's good to get to know it.

69
00:03:44,451 --> 00:03:48,347
And as you can see here, it's
active, and it's running.

70
00:03:48,347 --> 00:03:50,579
So that's all good.

71
00:03:50,579 --> 00:03:52,938
There are a bunch of other
things we can do with systemctl.

72
00:03:52,938 --> 00:03:55,955
We can potentially stop Nginx.

73
00:03:55,955 --> 00:03:59,595
We can also start Nginx
if it's not running.

74
00:03:59,595 --> 00:04:01,678
And we can restart Nginx.

75
00:04:03,084 --> 00:04:04,923
So those are a few of the things we can do

76
00:04:04,923 --> 00:04:07,173
with the system controller.

77
00:04:08,452 --> 00:04:09,551
Okay, now what we're going to do,

78
00:04:09,551 --> 00:04:12,666
is we're going to go
over to the Nginx config

79
00:04:12,666 --> 00:04:17,611
and we're going to add our
rest api to the Nginx config.

80
00:04:17,611 --> 00:04:19,805
We've not yet put the
rest api in the server,

81
00:04:19,805 --> 00:04:21,668
but we're going to do
that in just a moment,

82
00:04:21,668 --> 00:04:24,192
but we're going to add some
configuration parameters

83
00:04:24,192 --> 00:04:28,185
to Nginx to make sure that
it can access our api.

84
00:04:28,185 --> 00:04:29,165
So we're going to do

85
00:04:29,165 --> 00:04:33,332
sudo vi
/etc/nginx/sites-available/items-rest.conf.

86
00:04:41,338 --> 00:04:44,771
And this is a brand new
file which we are creating,

87
00:04:44,771 --> 00:04:45,961
and what we're going to
do is we're going to press

88
00:04:45,961 --> 00:04:48,898
the letter I to go into Insert mode,

89
00:04:48,898 --> 00:04:52,153
and then we're going to start
writing our Nginx config.

90
00:04:52,153 --> 00:04:53,560
Now this is going to be a bit long,

91
00:04:53,560 --> 00:04:56,818
but please bear with me while
I explain exactly what it is.

92
00:04:56,818 --> 00:05:01,273
The first part of any
Nginx config is the server.

93
00:05:01,273 --> 00:05:04,269
What that says is this block here

94
00:05:04,269 --> 00:05:07,887
inside these curly braces is a server.

95
00:05:07,887 --> 00:05:11,478
Essentially a Flask app
or something like that.

96
00:05:11,478 --> 00:05:13,974
And this server is going to be able to

97
00:05:13,974 --> 00:05:16,585
listen to incoming requests and then

98
00:05:16,585 --> 00:05:19,811
dispatch them where appropriate.

99
00:05:19,811 --> 00:05:20,644
Okay.

100
00:05:21,635 --> 00:05:25,385
So this app, this server,
is going to listen,

101
00:05:26,828 --> 00:05:27,826
sorry, apologies for that,

102
00:05:27,826 --> 00:05:30,246
is going to listen on port 80,

103
00:05:30,246 --> 00:05:33,106
which is the default HTTP port.

104
00:05:33,106 --> 00:05:35,402
Whenever we access a
website on the internet,

105
00:05:35,402 --> 00:05:37,004
we're accessing on port 80.

106
00:05:37,004 --> 00:05:39,202
That's just the default.

107
00:05:39,202 --> 00:05:41,429
So listening on port 80
is going to be interesting

108
00:05:41,429 --> 00:05:45,078
because it means that users
don't have to do something

109
00:05:45,078 --> 00:05:46,828
like get mysite.com:.

110
00:05:51,903 --> 00:05:54,611
The colon will be omitted
whenever we access

111
00:05:54,611 --> 00:05:58,517
our rest api because
the default is port 80.

112
00:05:58,517 --> 00:06:00,257
So if we're listening on port 80,

113
00:06:00,257 --> 00:06:03,924
we don't have to do
specify the port number.

114
00:06:05,362 --> 00:06:08,945
Next, a couple of
configuration parameters.

115
00:06:09,884 --> 00:06:13,717
Real_ip_header is going
to be X-Forwarded-For,

116
00:06:15,865 --> 00:06:16,978
and all that's going
to do is it's going to

117
00:06:16,978 --> 00:06:20,943
forward the ip address of the requester

118
00:06:20,943 --> 00:06:22,969
over to our Flask app.

119
00:06:22,969 --> 00:06:25,091
Now, I understand that that
doesn't make a lot of sense,

120
00:06:25,091 --> 00:06:28,546
but it just means that whenever somebody

121
00:06:28,546 --> 00:06:30,812
makes a request to our server,

122
00:06:30,812 --> 00:06:34,979
Nginx is going to tell our
Flask app who made that request.

123
00:06:36,977 --> 00:06:41,144
Next, we're going to set the
real ip from to be 127.0.0.1.

124
00:06:44,657 --> 00:06:47,689
Although Nginx is going
to forward the ip address

125
00:06:47,689 --> 00:06:50,837
of the requester to our Flask app,

126
00:06:50,837 --> 00:06:52,718
it's also going to say that the request

127
00:06:52,718 --> 00:06:55,733
really is coming from 127.0.0.1,

128
00:06:55,733 --> 00:06:57,500
which as you know is local host,

129
00:06:57,500 --> 00:07:00,745
because Nginx is receiving the request,

130
00:07:00,745 --> 00:07:04,585
and then it's going to send
that request to the Flask app,

131
00:07:04,585 --> 00:07:07,168
which is running on local host,

132
00:07:08,214 --> 00:07:10,459
or which will be running on local host.

133
00:07:10,459 --> 00:07:14,626
And to do with that, server_name
is going to be localhost.

134
00:07:16,142 --> 00:07:17,179
Okay?

135
00:07:17,179 --> 00:07:20,165
So some config properties there.

136
00:07:20,165 --> 00:07:23,814
Next we're going to do location /,

137
00:07:23,814 --> 00:07:25,254
and what this is going to do is

138
00:07:25,254 --> 00:07:29,476
it's going to say
whenever somebody accesses

139
00:07:29,476 --> 00:07:32,143
the root location of the server,

140
00:07:33,258 --> 00:07:36,341
it's going to redirect them somewhere,

141
00:07:36,341 --> 00:07:40,689
and where it's going to redirect
them is to our Flask app.

142
00:07:40,689 --> 00:07:41,522
Okay?

143
00:07:42,984 --> 00:07:46,817
So what we have to do
is include uwsgi_params.

144
00:07:47,995 --> 00:07:49,320
Now, as you know we're running the app

145
00:07:49,320 --> 00:07:52,660
using uwsgi, as you know
from the Heroku deployments,

146
00:07:52,660 --> 00:07:54,499
and if you don't, then I'll tell you now.

147
00:07:54,499 --> 00:07:58,032
We're running the app using
uwsgi, which is a programme

148
00:07:58,032 --> 00:08:00,941
that allows our Flask
app to run multi-threaded

149
00:08:00,941 --> 00:08:03,965
more efficiently and to
restart threads as they fail

150
00:08:03,965 --> 00:08:04,882
if they do.

151
00:08:06,201 --> 00:08:09,379
And Nginx deals very well with uwsgi.

152
00:08:09,379 --> 00:08:11,577
Uwsgi is a very complex thing,

153
00:08:11,577 --> 00:08:14,501
but we have to include uwsgi_params

154
00:08:14,501 --> 00:08:18,668
so that it can communicate
with the uwsgi programme.

155
00:08:20,151 --> 00:08:20,984
Okay.

156
00:08:20,984 --> 00:08:23,606
Then we're going to do uwsgi_pass,

157
00:08:23,606 --> 00:08:25,747
and then we're going
to type the following:

158
00:08:25,747 --> 00:08:29,914
unix:/var/www/html/items-rest/socket.sock.

159
00:08:37,473 --> 00:08:39,710
What this is doing is it's going to pass

160
00:08:39,710 --> 00:08:44,002
to this file all the necessary parameters.

161
00:08:44,002 --> 00:08:45,845
And what this file is going to be

162
00:08:45,845 --> 00:08:47,947
is this is going to be
the connection point

163
00:08:47,947 --> 00:08:50,614
between our Flask app and Nginx.

164
00:08:51,816 --> 00:08:54,486
So this is really important.

165
00:08:54,486 --> 00:08:57,191
We don't have a socket.sock file yet,

166
00:08:57,191 --> 00:08:58,852
but we will have it soon.

167
00:08:58,852 --> 00:09:01,519
We're going to be creating that.

168
00:09:02,434 --> 00:09:06,017
Then we're going to
say uwsgi_modifier1 30.

169
00:09:07,926 --> 00:09:09,211
And what this is passing in

170
00:09:09,211 --> 00:09:13,378
is a uwsgi parameter that
tells the threads when to die

171
00:09:16,391 --> 00:09:18,707
if they become blocked.

172
00:09:18,707 --> 00:09:21,040
So just a bit of info there.

173
00:09:22,028 --> 00:09:23,630
Okay.

174
00:09:23,630 --> 00:09:26,769
And then we can add a
bunch more stuff here,

175
00:09:26,769 --> 00:09:28,436
such as error pages.

176
00:09:32,098 --> 00:09:35,726
If we return a 404 code, we can tell Nginx

177
00:09:35,726 --> 00:09:39,508
to redirect the user to the 404 page,

178
00:09:39,508 --> 00:09:43,675
and we can say location = /404.html,

179
00:09:45,922 --> 00:09:48,062
and what this means is when the location

180
00:09:48,062 --> 00:09:51,758
is exactly /404.html do the following,

181
00:09:51,758 --> 00:09:54,609
and what we're going to do is say

182
00:09:54,609 --> 00:09:58,776
root /usr/share/nginx/html,

183
00:10:00,388 --> 00:10:03,154
and what that's going to
do is it's going to serve

184
00:10:03,154 --> 00:10:06,487
the 404.html page from this folder here,

185
00:10:08,577 --> 00:10:12,350
which is /usr/share/nginx/html,

186
00:10:12,350 --> 00:10:16,689
and Nginx comes quite handily
with a page called 404.html,

187
00:10:16,689 --> 00:10:18,388
so the user is going to see that page

188
00:10:18,388 --> 00:10:21,662
if we return a 404, and also we can say

189
00:10:21,662 --> 00:10:25,829
error_page 500, 502, 503
and 504 go to /50x.html

190
00:10:31,051 --> 00:10:34,634
and we can say if the
location is 50x.html,

191
00:10:35,630 --> 00:10:40,469
do root /user/share/nginx/html.

192
00:10:40,469 --> 00:10:42,302
And once again, if we return error codes

193
00:10:42,302 --> 00:10:46,468
500, 502, 503 and 504,
we're going to serve the

194
00:10:46,468 --> 00:10:50,117
50x.html page, which is
going to tell the user

195
00:10:50,117 --> 00:10:52,784
that an internal error occurred.

196
00:10:54,167 --> 00:10:57,412
I appreciate that this is
particularly confusing.

197
00:10:57,412 --> 00:10:59,227
And the first few times
that I had to do this,

198
00:10:59,227 --> 00:11:00,917
I had no clue what was going on,

199
00:11:00,917 --> 00:11:03,240
but I just knew it worked.

200
00:11:03,240 --> 00:11:06,619
And if you want to
delve into the depths of

201
00:11:06,619 --> 00:11:09,942
what this all means,
then you'll have to go

202
00:11:09,942 --> 00:11:13,563
and investigate about the uwsgi protocol,

203
00:11:13,563 --> 00:11:16,749
and how these applications connect

204
00:11:16,749 --> 00:11:20,407
to the receiving server, which
can be something like Apache,

205
00:11:20,407 --> 00:11:22,960
or something like Nginx.

206
00:11:22,960 --> 00:11:24,747
You don't have to worry about that,

207
00:11:24,747 --> 00:11:27,569
because when you do this, it will work.

208
00:11:27,569 --> 00:11:30,439
So I've chosen not to delve into

209
00:11:30,439 --> 00:11:32,606
any of the uwsgi concepts.

210
00:11:34,049 --> 00:11:37,054
They get confusing very quickly.

211
00:11:37,054 --> 00:11:38,743
But just know that that is essentially

212
00:11:38,743 --> 00:11:40,058
what's happening here.

213
00:11:40,058 --> 00:11:42,324
Whenever we access any page,

214
00:11:42,324 --> 00:11:44,110
we're going to go
through to the Flask app,

215
00:11:44,110 --> 00:11:47,860
except if we go to the
404 and 50x end points

216
00:11:50,554 --> 00:11:52,916
and then we're going
to set up those pages.

217
00:11:52,916 --> 00:11:53,883
And then what we're doing here

218
00:11:53,883 --> 00:11:55,554
is we're listening on port 80,

219
00:11:55,554 --> 00:11:59,471
which is the default port
for HTTP connections,

220
00:12:00,343 --> 00:12:02,907
and then we're passing in
who requested the page.

221
00:12:02,907 --> 00:12:04,664
We're also passing in localhost,

222
00:12:04,664 --> 00:12:07,495
which is actually the Nginx server,

223
00:12:07,495 --> 00:12:08,925
and then we're passing in localhost,

224
00:12:08,925 --> 00:12:11,624
which is the Nginx server again.

225
00:12:11,624 --> 00:12:13,476
When we've written all of this stuff here,

226
00:12:13,476 --> 00:12:14,848
we're going to press Escape,

227
00:12:14,848 --> 00:12:17,431
and then :wq to write and quit,

228
00:12:18,478 --> 00:12:22,561
and that's our Nginx config
being made available.

229
00:12:23,414 --> 00:12:25,821
However, it's not been enabled.

230
00:12:25,821 --> 00:12:27,444
We have to enable the config,

231
00:12:27,444 --> 00:12:28,817
and the way we'll do that is by doing

232
00:12:28,817 --> 00:12:32,984
sudo
ln -s
/etc/nginx/sites-available/items-rest.conf,

233
00:12:39,559 --> 00:12:43,726
and then space, /etc/nginx/sites-enabled.

234
00:12:44,907 --> 00:12:46,308
Okay, so what this is
doing is it's going to

235
00:12:46,308 --> 00:12:50,658
create a link, a soft link
between the config file

236
00:12:50,658 --> 00:12:54,864
we just created and the
sites enabled folder,

237
00:12:54,864 --> 00:12:58,548
where Nginx reads the config properties.

238
00:12:58,548 --> 00:13:00,354
So we're going to do that.

239
00:13:00,354 --> 00:13:01,187
Okay.

240
00:13:02,436 --> 00:13:06,074
Now that we've done that,
Nginx will know about

241
00:13:06,074 --> 00:13:10,241
our config file, and it will
try to access that socket file.

242
00:13:11,594 --> 00:13:13,353
That socket file doesn't yet exist,

243
00:13:13,353 --> 00:13:16,433
but we're going to make sure
that it does in a moment.

244
00:13:16,433 --> 00:13:17,662
The next thing that we're going to do is

245
00:13:17,662 --> 00:13:21,829
sudo mkdir /var/www/html/items-rest.

246
00:13:25,953 --> 00:13:29,497
Remember, we used this
folder in the config file.

247
00:13:29,497 --> 00:13:33,661
This is where our
application is going to live.

248
00:13:33,661 --> 00:13:35,040
Now something interesting to remember

249
00:13:35,040 --> 00:13:39,836
is we've created this
directory as the root user,

250
00:13:39,836 --> 00:13:42,562
which means our user, Jose,

251
00:13:42,562 --> 00:13:45,085
is not going to have
access to that directory.

252
00:13:45,085 --> 00:13:48,349
So we have to make sure
to give the directory

253
00:13:48,349 --> 00:13:50,250
to the Jose user, and what that is

254
00:13:50,250 --> 00:13:53,754
is make the Jose user the
owner of that directory.

255
00:13:53,754 --> 00:13:58,151
So the way we do that is
sudo chown, change owner,

256
00:13:58,151 --> 00:14:02,318
Jose:jose /var/www/html/items-rest.

257
00:14:05,350 --> 00:14:08,615
So we're going to change
the owner of this directory

258
00:14:08,615 --> 00:14:12,119
to the Jose user and to the Jose group,

259
00:14:12,119 --> 00:14:15,750
as opposed to the root
user and the root group.

260
00:14:15,750 --> 00:14:16,949
Okay.

261
00:14:16,949 --> 00:14:20,449
Now we're going to go into that directory.

262
00:14:22,755 --> 00:14:25,184
And now as you can see
we are in that directory,

263
00:14:25,184 --> 00:14:26,771
and the next thing we have to do

264
00:14:26,771 --> 00:14:29,021
is to clone our Python app.

265
00:14:30,378 --> 00:14:31,905
So I'm going to use the same app

266
00:14:31,905 --> 00:14:35,831
as we used in the Heroku
section, in the last section.

267
00:14:35,831 --> 00:14:36,762
Okay.

268
00:14:36,762 --> 00:14:40,429
So I'm just going to
go ahead and copy that.

269
00:14:42,350 --> 00:14:43,194
And I'm going to paste it in here,

270
00:14:43,194 --> 00:14:45,152
and I'm going to explain
exactly what I'm doing.

271
00:14:45,152 --> 00:14:48,235
What I'm going to do is git clone the

272
00:14:49,165 --> 00:14:53,332
https://github.com/schoolofcode-me/stores-rest-api.git.

273
00:14:57,251 --> 00:14:59,878
So that's the git hub repository

274
00:14:59,878 --> 00:15:02,588
and with the stores-rest.api

275
00:15:02,588 --> 00:15:06,311
that we deployed in the last section.

276
00:15:06,311 --> 00:15:08,605
And after that, I'm going to put a space,

277
00:15:08,605 --> 00:15:12,859
and a full stop, and that
means clone all the items

278
00:15:12,859 --> 00:15:14,692
into this folder here.

279
00:15:17,431 --> 00:15:18,455
Okay.

280
00:15:18,455 --> 00:15:22,333
And there we have, we've now cloned it.

281
00:15:22,333 --> 00:15:23,166
Okay.

282
00:15:24,810 --> 00:15:28,977
Now, in order to be able to
produce logs for our app,

283
00:15:30,714 --> 00:15:33,853
we're going to have to create
a directory called log.

284
00:15:33,853 --> 00:15:35,603
So just do mkdir log.

285
00:15:36,705 --> 00:15:40,996
Because we own this folder,
we own the items-rest folder,

286
00:15:40,996 --> 00:15:44,346
the Jose user owns it, we
don't have to use sudo anymore,

287
00:15:44,346 --> 00:15:46,381
and indeed you shouldn't use sudo.

288
00:15:46,381 --> 00:15:47,881
Just do mkdir log.

289
00:15:49,110 --> 00:15:51,049
Okay, now I'm going to press ls,

290
00:15:51,049 --> 00:15:53,303
and you can see that we've
got a bunch of things here

291
00:15:53,303 --> 00:15:55,761
which are all our application files,

292
00:15:55,761 --> 00:15:58,630
as well as the log folder.

293
00:15:58,630 --> 00:15:59,463
Okay.

294
00:16:00,695 --> 00:16:02,192
The next thing we have to instal

295
00:16:02,192 --> 00:16:05,561
a bunch of Python related
things that we're going to need.

296
00:16:05,561 --> 00:16:09,190
So we're going to sudo apt-get instal,

297
00:16:09,190 --> 00:16:11,389
and the first thing we're
going to instal is pip,

298
00:16:11,389 --> 00:16:13,318
the Python package manager.

299
00:16:13,318 --> 00:16:14,485
So python-pip.

300
00:16:15,529 --> 00:16:17,891
Then we're going to instal a set of tools

301
00:16:17,891 --> 00:16:21,565
that Python needs to be
able to compile from source.

302
00:16:21,565 --> 00:16:24,315
So we're going to do python3-dev,

303
00:16:25,501 --> 00:16:27,018
and what that's going to allow us is to

304
00:16:27,018 --> 00:16:30,676
instal things like uwsgi and psycoPG,

305
00:16:30,676 --> 00:16:33,270
which is needed for SQLAlchemy.

306
00:16:33,270 --> 00:16:36,618
And finally we're going
to instal libpq-dev,

307
00:16:36,618 --> 00:16:40,785
which again, is going to
allow us to run psycoPG2.

308
00:16:42,061 --> 00:16:44,978
So we're going to instal all that.

309
00:16:45,843 --> 00:16:48,343
And this may take a wee while.

310
00:16:49,233 --> 00:16:52,564
Now that's finished,
we're ready to proceed,

311
00:16:52,564 --> 00:16:55,837
and we have to first instal virtualenv.

312
00:16:55,837 --> 00:16:59,274
So we're going to use
pip to instal virtualenv,

313
00:16:59,274 --> 00:17:01,281
and as you know, a virtual environment

314
00:17:01,281 --> 00:17:03,940
is just another Python installation

315
00:17:03,940 --> 00:17:05,486
that doesn't have the libraries that the

316
00:17:05,486 --> 00:17:07,731
main Python installation has.

317
00:17:07,731 --> 00:17:10,343
So we're going to use
virtualenv for this project

318
00:17:10,343 --> 00:17:14,413
so we don't pollute the main
Ubuntu Python installation

319
00:17:14,413 --> 00:17:16,717
with our libraries.

320
00:17:16,717 --> 00:17:18,820
We're going to create
a virtual environment

321
00:17:18,820 --> 00:17:21,651
within the same folder, and
the way we do that is with

322
00:17:21,651 --> 00:17:23,956
virtualenv, then the name of the folder

323
00:17:23,956 --> 00:17:25,924
which is going to host
our virtual environment.

324
00:17:25,924 --> 00:17:28,046
I'm calling it venv.

325
00:17:28,046 --> 00:17:30,796
And then --python=python3.5.

326
00:17:32,145 --> 00:17:34,880
Ubuntu comes with Python
3.5, which is great.

327
00:17:34,880 --> 00:17:38,174
We don't have to instal it ourselves.

328
00:17:38,174 --> 00:17:39,335
(sneezes)

329
00:17:39,335 --> 00:17:40,737
Apologies for that.

330
00:17:40,737 --> 00:17:44,260
Now that we have created
the virtual environment,

331
00:17:44,260 --> 00:17:46,794
we can see that it is there inside

332
00:17:46,794 --> 00:17:49,156
the same folder as everything else.

333
00:17:49,156 --> 00:17:51,450
And now we're going to go
into the virtual environment

334
00:17:51,450 --> 00:17:55,712
by doing source venv/bin/activate.

335
00:17:55,712 --> 00:17:58,823
Now, remember we are in Unix right now,

336
00:17:58,823 --> 00:18:02,451
so even if we're running
this terminal in Windows,

337
00:18:02,451 --> 00:18:06,095
you're still going to have
to run source venv activate.

338
00:18:06,095 --> 00:18:08,524
And then we're going to be in the venv.

339
00:18:08,524 --> 00:18:10,194
Finally, the last thing to do is to

340
00:18:10,194 --> 00:18:12,527
instal -r requirements.txt.

341
00:18:14,668 --> 00:18:18,835
So we're going to do pip
instal -r requirements.txt.

342
00:18:19,709 --> 00:18:20,542
And what that's going to do

343
00:18:20,542 --> 00:18:22,030
is it's going to get
all of the requirements

344
00:18:22,030 --> 00:18:25,878
from the requirements.txt
file, and it's going to

345
00:18:25,878 --> 00:18:29,497
instal them in this virtual environment.

346
00:18:29,497 --> 00:18:30,966
Remember that all of
these files were created

347
00:18:30,966 --> 00:18:34,799
in the last section in
the Heroku deployments.

348
00:18:41,719 --> 00:18:42,552
Okay.

349
00:18:42,552 --> 00:18:45,050
So what this has done is it has installed

350
00:18:45,050 --> 00:18:48,800
Flask-JWT, Flask-SQLAlchemy,
uwsgi, psycopg2,

351
00:18:50,964 --> 00:18:53,230
itsdangerous, which is part of Flask,

352
00:18:53,230 --> 00:18:57,397
aniso8601, which is for
dates and the iso standard,

353
00:18:59,378 --> 00:19:01,874
SQLAlchemy and MarkupSafe,
which is also part of Flask.

354
00:19:01,874 --> 00:19:05,484
So it's installed everything
that we need to run our app.

355
00:19:05,484 --> 00:19:07,058
Okay.

356
00:19:07,058 --> 00:19:08,651
Now, the next thing we have to do

357
00:19:08,651 --> 00:19:12,136
is set up uwsgi so that our application

358
00:19:12,136 --> 00:19:15,909
can communicate with uwsgi
and Nginx can communicate

359
00:19:15,909 --> 00:19:19,125
with uwsgi to create a complete chain.

360
00:19:19,125 --> 00:19:20,575
And we're going to do
that in the next video,

361
00:19:20,575 --> 00:19:22,552
because this one is getting pretty long,

362
00:19:22,552 --> 00:19:23,552
and so yeah.

363
00:19:24,492 --> 00:19:27,554
So if you have any problems
up until now, let me know.

364
00:19:27,554 --> 00:19:31,576
It's very important that
we don't get stuck in here.

365
00:19:31,576 --> 00:19:34,670
Without further adieu, I'll
see you in the very next video.

