1
1

00:00:00,552  -->  00:00:02,560
<v Instructor>Hi guys and welcome back.</v>
2

2

00:00:02,560  -->  00:00:04,773
In this video we're going to setup Nginx.
3

3

00:00:05,680  -->  00:00:08,480
Nginx is going to be the most forward facing
4

4

00:00:08,480  -->  00:00:10,640
application in our server which is what users
5

5

00:00:10,640  -->  00:00:12,760
are going to interact with.
6

6

00:00:12,760  -->  00:00:14,843
Let's begin by installing it.
7

7

00:00:17,980  -->  00:00:19,150
Now that we've got it installed
8

8

00:00:19,150  -->  00:00:21,110
we have to do some configuration,
9

9

00:00:21,110  -->  00:00:24,193
but first of all let's check our server's firewall,
10

10

00:00:26,860  -->  00:00:29,560
and you can see that the firewall is inactive.
11

11

00:00:29,560  -->  00:00:32,100
We're going to enable the firewall later on
12

12

00:00:32,100  -->  00:00:34,330
to help us keep things safe.
13

13

00:00:34,330  -->  00:00:35,940
But before we enable the firewall
14

14

00:00:35,940  -->  00:00:38,100
we have to do a couple of things.
15

15

00:00:38,100  -->  00:00:41,030
We have to allow Nginx HTTP
16

16

00:00:42,380  -->  00:00:47,120
so that we will be able to access Nginx
17

17

00:00:47,120  -->  00:00:48,710
from the outside world essentially
18

18

00:00:48,710  -->  00:00:51,300
make requests to our application
19

19

00:00:51,300  -->  00:00:53,850
and we also have to allow SSH
20

20

00:00:53,850  -->  00:00:57,320
so that we can stay logged in when we enable the firewall.
21

21

00:00:57,320  -->  00:01:00,310
Next, we're gonna do ufw enable.
22

22

00:01:00,310  -->  00:01:01,430
And it tells you that this
23

23

00:01:01,430  -->  00:01:03,350
may disrupt existing ssh connections
24

24

00:01:03,350  -->  00:01:06,200
but we have just allowed SSH earlier
25

25

00:01:06,200  -->  00:01:08,670
so we're gonna say yes.
26

26

00:01:08,670  -->  00:01:11,660
Next thing to do is we're gonna do sudo systemctl
27

27

00:01:11,660  -->  00:01:15,420
status nginx and you can see hopefully
28

28

00:01:15,420  -->  00:01:18,800
that Nginx is already active and running.
29

29

00:01:18,800  -->  00:01:21,000
You can always stop, restart,
30

30

00:01:21,000  -->  00:01:24,880
or reload Nginx by using the appropriate commands.
31

31

00:01:24,880  -->  00:01:28,970
So instead of status you can do start, stop, or reload.
32

32

00:01:28,970  -->  00:01:32,296
Reload is useful when you change any configuration of Nginx
33

33

00:01:32,296  -->  00:01:34,720
so that it will reload the configuration
34

34

00:01:34,720  -->  00:01:36,480
without having to restart.
35

35

00:01:36,480  -->  00:01:39,410
Now let's configure Nginx for our application.
36

36

00:01:39,410  -->  00:01:42,030
We're gonna do sudo vi etc nginx
37

37

00:01:42,030  -->  00:01:46,330
sites-available pricing service.conf.
38

38

00:01:46,330  -->  00:01:48,680
This will be an empty file where we're gonna define
39

39

00:01:48,680  -->  00:01:52,840
how Nginx is going to behave in this context.
40

40

00:01:52,840  -->  00:01:56,470
Nginx works by defining different server blocks
41

41

00:01:56,470  -->  00:01:59,020
and each server block is completely independent
42

42

00:01:59,020  -->  00:02:01,600
and runs 100% of the time.
43

43

00:02:01,600  -->  00:02:06,580
So, we will say server and then these curly braces.
44

44

00:02:06,580  -->  00:02:10,640
Next, we need to say where the server will be listening.
45

45

00:02:10,640  -->  00:02:13,500
So, if we say listen 80 for example,
46

46

00:02:13,500  -->  00:02:15,910
this server or all of the code
47

47

00:02:15,910  -->  00:02:18,040
that we include inside this server block
48

48

00:02:18,040  -->  00:02:22,320
will happen when a user makes a request on port 80.
49

49

00:02:22,320  -->  00:02:25,650
Port 80 is the default port for HTTP requests
50

50

00:02:25,650  -->  00:02:27,670
so whenever somebody accesses
51

51

00:02:27,670  -->  00:02:31,763
our server's address this block will run.
52

52

00:02:32,870  -->  00:02:37,870
So, we will say real ip header is X Forwarded For.
53

53

00:02:40,640  -->  00:02:43,040
And what this does is it instead
54

54

00:02:43,040  -->  00:02:46,410
of hiding the IP header from our Flask app
55

55

00:02:46,410  -->  00:02:50,110
it will forward the IP header to our Flask app.
56

56

00:02:50,110  -->  00:02:52,553
So, our Flask app knows who made which request.
57

57

00:02:53,710  -->  00:02:57,163
And we will set real ip from 127.0.0.1,
58

58

00:02:58,720  -->  00:03:03,120
the server name is going to be localhost for now.
59

59

00:03:03,120  -->  00:03:04,870
That's just a couple of configuration variables
60

60

00:03:04,870  -->  00:03:07,270
and now we get into the meat of things.
61

61

00:03:07,270  -->  00:03:10,460
What happens when we get a request
62

62

00:03:10,460  -->  00:03:14,660
made to the root of our application
63

63

00:03:14,660  -->  00:03:16,890
or anything that comes after it?
64

64

00:03:16,890  -->  00:03:19,060
When you define a location block
65

65

00:03:19,060  -->  00:03:20,900
the configuration inside this block
66

66

00:03:20,900  -->  00:03:25,540
will happen when a request is made to the slash endpoint.
67

67

00:03:25,540  -->  00:03:27,030
So, basically the root endpoint
68

68

00:03:27,030  -->  00:03:29,773
of our application or indeed anything else.
69

69

00:03:30,830  -->  00:03:33,640
If you wanted to define an application
70

70

00:03:33,640  -->  00:03:35,890
that starts with slash app for example
71

71

00:03:35,890  -->  00:03:37,930
you would do something like that and this block
72

72

00:03:37,930  -->  00:03:41,673
would only happen on requests that start with slash app,
73

73

00:03:42,820  -->  00:03:45,170
but because we are defining our app
74

74

00:03:45,170  -->  00:03:47,590
as the top level app we're gonna keep it as that.
75

75

00:03:47,590  -->  00:03:50,990
So, basically whenever a request accesses our application,
76

76

00:03:50,990  -->  00:03:55,000
we're gonna say include uwsg params
77

77

00:03:55,000  -->  00:03:57,450
and then we're gonna pass the request
78

78

00:03:57,450  -->  00:04:01,623
to the socket file that we defined earlier,
79

79

00:04:05,712  -->  00:04:07,380
and that's pretty much everything we need to do
80

80

00:04:07,380  -->  00:04:12,290
because Nginx is already geared talk to uWSGI.
81

81

00:04:12,290  -->  00:04:14,330
When we give the socket file Nginx
82

82

00:04:14,330  -->  00:04:16,880
will now how to pass the information there.
83

83

00:04:16,880  -->  00:04:20,390
As long as uWSGI is running it will be able to take that
84

84

00:04:20,390  -->  00:04:23,080
and respond with whatever our Flask application
85

85

00:04:23,080  -->  00:04:25,770
would respond with and send it back to Nginx
86

86

00:04:25,770  -->  00:04:26,770
to send to the user.
87

87

00:04:28,020  -->  00:04:30,600
In some cases, we may get some error pages
88

88

00:04:31,660  -->  00:04:34,400
so we're gonna define 404.html
89

89

00:04:35,280  -->  00:04:39,500
and what this does is when we get an 404 code
90

90

00:04:39,500  -->  00:04:41,810
from our Flask app we're gonna send
91

91

00:04:41,810  -->  00:04:45,460
the user to slash 404.html in their browser
92

92

00:04:45,460  -->  00:04:47,370
and this also defines that when the user
93

93

00:04:47,370  -->  00:04:50,400
accesses 404.html we're gonna serve them
94

94

00:04:50,400  -->  00:04:52,290
with this page here.
95

95

00:04:52,290  -->  00:04:55,113
Again, same thing if the error code is 500,
96

96

00:04:55,113  -->  00:04:57,410
502, 503, or 504, we're gonna send
97

97

00:04:57,410  -->  00:05:00,550
the user to slash 50x.html
98

98

00:05:00,550  -->  00:05:03,870
and I forgot the forward slash here.
99

99

00:05:03,870  -->  00:05:06,800
When they access slash 50x.html
100

100

00:05:06,800  -->  00:05:09,290
we're gonna serve them from this folder here.
101

101

00:05:09,290  -->  00:05:11,760
And I realise I said file earlier
102

102

00:05:11,760  -->  00:05:12,750
but really what's happening here
103

103

00:05:12,750  -->  00:05:14,080
is we're gonna serve the file
104

104

00:05:14,080  -->  00:05:19,080
slash usr slash share slash nginx slash html slash 404.html.
105

105

00:05:19,680  -->  00:05:23,010
So, that's what we're gonna give the user when this happens.
106

106

00:05:23,010  -->  00:05:25,680
And that's a default 404 page
107

107

00:05:25,680  -->  00:05:27,740
and a default 50x page.
108

108

00:05:27,740  -->  00:05:30,140
That's everything we need to do for Nginx config,
109

109

00:05:30,140  -->  00:05:34,195
so let's press escape colon wq to write and quit
110

110

00:05:34,195  -->  00:05:39,195
and now we have to do sudo rm etc nginx sites-enabled
111

111

00:05:42,530  -->  00:05:47,290
and remove the default server that's already there
112

112

00:05:47,290  -->  00:05:48,340
and then we're gonna do sudo
113

113

00:05:48,340  -->  00:05:53,340
ln-s etc nginx sites-available pricing service.conf
114

114

00:05:54,690  -->  00:05:57,490
to etc nginx sites-enabled.
115

115

00:05:57,490  -->  00:06:01,320
What this does is actually makes the site enabled.
116

116

00:06:01,320  -->  00:06:03,480
By default Nginx recommends
117

117

00:06:03,480  -->  00:06:06,090
that you store all your configurations
118

118

00:06:06,090  -->  00:06:09,480
inside a folder called sites-available.
119

119

00:06:09,480  -->  00:06:12,880
Those sites are not actually in Nginx
120

120

00:06:12,880  -->  00:06:17,120
until you put them inside sites-enabled.
121

121

00:06:17,120  -->  00:06:20,980
That way if you want to temporarily disable a site
122

122

00:06:20,980  -->  00:06:22,840
you can delete it from sites-enabled
123

123

00:06:22,840  -->  00:06:25,010
but you still have it in sites-available
124

124

00:06:25,010  -->  00:06:27,540
in case you wanna bring it back later.
125

125

00:06:27,540  -->  00:06:32,540
So, by doing ln-s that essentially makes like a shortcut
126

126

00:06:32,670  -->  00:06:37,630
from this in sites-available to sites-enabled.
127

127

00:06:37,630  -->  00:06:39,490
Now that we've gotten that we are going
128

128

00:06:39,490  -->  00:06:43,910
to do sudo systemctl reload nginx
129

129

00:06:43,910  -->  00:06:46,860
and make sure that your uWSGI service
130

130

00:06:46,860  -->  00:06:51,860
is also running and now we're ready to try out our app.
131

131

00:06:52,560  -->  00:06:55,870
I have gone over to the IP address of the server
132

132

00:06:55,870  -->  00:06:59,820
and you can see that we have our app here running.
133

133

00:06:59,820  -->  00:07:00,933
Let's try to sign up.
134

134

00:07:03,060  -->  00:07:04,090
There you have it.
135

135

00:07:04,090  -->  00:07:05,260
You can see that MongoDB
136

136

00:07:05,260  -->  00:07:08,850
is also running in the background because this succeeded.
137

137

00:07:08,850  -->  00:07:11,343
So, we've got everything working now.
138

138

00:07:12,290  -->  00:07:13,270
The next thing we'd like to do
139

139

00:07:13,270  -->  00:07:16,770
is of course to set up something that runs periodically
140

140

00:07:16,770  -->  00:07:19,130
to check our alerts and send an email
141

141

00:07:19,130  -->  00:07:21,870
when an alert does reach the desired price.
142

142

00:07:21,870  -->  00:07:23,640
Let's do that in the next video.
143

143

00:07:23,640  -->  00:07:24,590
I'll see you there.
