WEBVTT
Kind: captions
Language: en

00:00:00.060 --> 00:00:04.560
While we're waiting for our server to
start in AWS let's go ahead and configure

00:00:04.560 --> 00:00:10.620
our project for deployment start by
downloading the deployed dot zip file

00:00:10.620 --> 00:00:17.430
that I added as a resource to this video
this file contains a zip of all the

00:00:17.430 --> 00:00:22.470
scripts and configuration files that we
need to deploy our project we're going

00:00:22.470 --> 00:00:26.220
to add the contents of this to our
project and then I'm going to talk you

00:00:26.220 --> 00:00:28.680
through what these scripts do

00:00:28.680 --> 00:00:32.009
So once it's been downloaded unzip the deployed

00:00:32.009 --> 00:00:37.120
dot zip file and copy the deploy
directory into our profiles REST API

00:00:37.120 --> 00:00:39.200
project

00:00:39.200 --> 00:00:42.660
then open up the atom editor and I'm going to walk you through each of

00:00:42.660 --> 00:00:46.000
the scripts in the deployed directory

00:00:46.000 --> 00:00:49.649
so here we have a set up dot SH script this

00:00:49.649 --> 00:00:53.430
is the script that we're going to use to
set up our server when it's first

00:00:53.430 --> 00:00:55.420
created

00:00:55.420 --> 00:01:00.390
before we can run this we need
to update the project git URL to the URL

00:01:00.390 --> 00:01:03.080
of your github project

00:01:03.080 --> 00:01:06.360
this is because the script is going to clone the

00:01:06.360 --> 00:01:10.110
contents of our project to the server
when we run it so we need to make sure

00:01:10.110 --> 00:01:16.259
that this is correct with the project
URL for our project so open up the

00:01:16.259 --> 00:01:20.250
browser and open up github to our
project and then we're going to click

00:01:20.250 --> 00:01:25.110
this clone or download button and we
want to make sure we are cloning with

00:01:25.110 --> 00:01:31.180
HTTPS and we're going to copy the link
of this HTTPS URL

00:01:31.180 --> 00:01:32.189
then head over to the

00:01:32.189 --> 00:01:35.400
atom editor and paste the contents in the
project

00:01:35.400 --> 00:01:39.580
git url variable

00:01:39.580 --> 00:01:41.820
don't forget to save the file and I'm going to talk you

00:01:41.820 --> 00:01:46.110
through what this script does so at the
top of the script we set some variables

00:01:46.110 --> 00:01:51.570
this is the URL of the github project
which we've just set and then we have

00:01:51.570 --> 00:01:55.829
the base path which is the directory
we're going to store our project on the

00:01:55.829 --> 00:01:57.320
server

00:01:57.320 --> 00:02:02.969
we're going to store our project in USR slash local slash apps slash profiles

00:02:02.969 --> 00:02:07.229
REST API this is where we're going to
clone the project to the server and it's

00:02:07.229 --> 00:02:09.660
where all of our source code is going to
be kept

00:02:09.660 --> 00:02:10.830
next we install some

00:02:10.830 --> 00:02:13.569
dependencies which we need to run our
application

00:02:13.569 --> 00:02:19.540
on a server so we need Python Python 3
and then we need the Python 3 virtual

00:02:19.540 --> 00:02:27.200
environment and we also need sqlite
Python pip supervisor nginx and git

00:02:27.200 --> 00:02:33.240
So git is going to be used to clone the project nginx is the webserver that is going to

00:02:33.250 --> 00:02:39.189
serve the static files and act as a
proxy to our uWSGI service that is

00:02:39.189 --> 00:02:41.920
going to run in supervisor

00:02:41.920 --> 00:02:44.409
and then of course we have Python pip which is the

00:02:44.409 --> 00:02:47.739
package manager that we're going to use
to install the packages and we have

00:02:47.739 --> 00:02:51.699
sqlite which we're going to use for
the database and then we have Python 3

00:02:51.699 --> 00:02:54.969
virtual environment which is going to be
used to set up our virtual environment

00:02:54.969 --> 00:02:59.409
finally we have Python 3 dev which is
going to be used to run Python on the

00:02:59.409 --> 00:03:01.780
server

00:03:01.780 --> 00:03:07.030
The next thing we do is we create
a directory to the project URL or the

00:03:07.030 --> 00:03:13.000
location on the server that we want to
create or store the project files so

00:03:13.000 --> 00:03:16.419
we need to create the project before we
can clone it because the next thing we

00:03:16.419 --> 00:03:21.909
do is we use git to clone the project
from our git URL to the base location

00:03:21.909 --> 00:03:26.049
where we want to store the code so this
is going to do a git clone and pull the

00:03:26.049 --> 00:03:28.320
source code down to the server

00:03:28.320 --> 00:03:30.970
Next we create the virtual environment on the

00:03:30.970 --> 00:03:37.780
server so this creates a new directory
in our project forward slash env to create a virtual

00:03:37.780 --> 00:03:41.160
environment for Python

00:03:41.160 --> 00:03:44.439
Next we install the Python packages which are everything

00:03:44.439 --> 00:03:50.590
within our requirements dot txt file and
additionally this uWSGI which is a

00:03:50.590 --> 00:03:56.240
Python daemon for running Python code as a webserver

00:03:56.240 --> 00:03:58.239
Next we run the migrations

00:03:58.239 --> 00:04:02.739
which we've done a number of times every time we change our migrations

00:04:02.739 --> 00:04:09.129
or our models and then we run this
collect static command collect static will

00:04:09.129 --> 00:04:13.840
gather all of the static files for all
of the apps in our project into one

00:04:13.840 --> 00:04:16.120
directory

00:04:16.120 --> 00:04:18.669
when you use the Django management server it does this

00:04:18.669 --> 00:04:22.240
automatically for you but since we're
not going to be using the Django

00:04:22.240 --> 00:04:26.410
development server on production we need
to create a location

00:04:26.410 --> 00:04:32.560
with all of the static files that we can
use to serve the CSS and JavaScript for

00:04:32.560 --> 00:04:38.880
the Django admin and the Django rest
framework browsable api

00:04:38.880 --> 00:04:39.640
Next we're going

00:04:39.640 --> 00:04:45.580
to configure the supervisor supervisor
is a application on linux that allows

00:04:45.580 --> 00:04:51.010
you to manage processes this is what's
going to manage our Python process

00:04:51.010 --> 00:04:53.860
or our uWSGI server

00:04:53.860 --> 00:04:58.660
so what we do here is we copy the supervisor profiles

00:04:58.660 --> 00:05:04.180
API configuration dot config file into
the location on the server where the

00:05:04.180 --> 00:05:06.280
supervisor is kept

00:05:06.280 --> 00:05:10.390
then we run reread to update these supervisor configuration

00:05:10.390 --> 00:05:15.790
files and then we run update to update
all the processes next we restart our

00:05:15.790 --> 00:05:22.090
profiles API to make sure that our
service is started finally we configure

00:05:22.090 --> 00:05:26.950
out nginx server this is the webserver
that's going to be used to serve the

00:05:26.950 --> 00:05:32.740
static files so we need to create a
location for the configuration file and

00:05:32.740 --> 00:05:37.630
we copy the configuration file that
we've added here nginx profiles API

00:05:37.630 --> 00:05:41.560
dot conf to the sites available directory in

00:05:41.560 --> 00:05:47.169
nginx then we remove the default
configuration that's provided when you

00:05:47.169 --> 00:05:55.390
install nginx and we add a symbolic link
from our sites available to our

00:05:55.390 --> 00:05:59.120
sites enabled to enable our site

00:05:59.120 --> 00:06:03.490
finally we restart the nginx service and then

00:06:03.490 --> 00:06:08.890
the script exits with this message here
saying done so that's a setup script

00:06:08.890 --> 00:06:13.750
here we also have an update script which
is going to be used to update the code

00:06:13.750 --> 00:06:18.040
on the server whenever we make a change
so the setup script will be used the

00:06:18.040 --> 00:06:22.210
first time we set up the server but it
will only work once once the server

00:06:22.210 --> 00:06:28.210
setup you need to use the update script
to pull updates or changes from git on

00:06:28.210 --> 00:06:30.200
to the server

00:06:30.200 --> 00:06:33.190
So again we have the
project based path which is the location

00:06:33.190 --> 00:06:38.800
on the server where the source code is
going to be kept then we run git pull

00:06:38.800 --> 00:06:44.919
and then we run migrate in our virtual
environment and then we run the collect

00:06:44.919 --> 00:06:48.759
static in case you've added any new
dependencies that add anymore static

00:06:48.759 --> 00:06:53.949
files and finally we restart our
supervisor process to make the changes

00:06:53.949 --> 00:06:57.020
come into effect

00:06:57.020 --> 00:07:01.120
Ok so that's all we
need to do for the deploy configuration

00:07:01.120 --> 00:07:06.669
next we're going to make some changes to
our settings file for it to run better

00:07:06.669 --> 00:07:08.280
on the server

00:07:08.280 --> 00:07:13.509
So open up settings dot py within the profiles project and the

00:07:13.509 --> 00:07:16.949
first change we're going to make is
we're going to disable this debug mode

00:07:16.949 --> 00:07:22.419
it's best practice never to run the
server in debug mode when it's publicly

00:07:22.419 --> 00:07:26.319
accessible it's fine when you're running
it on your local machine because it's

00:07:26.319 --> 00:07:29.800
unlikely that anyone else is going to be
able to connect to the server but you

00:07:29.800 --> 00:07:33.250
should never run it when it's on a
publicly accessible server because it

00:07:33.250 --> 00:07:38.139
opens you up to vulnerabilities when you
run the server in debug mode that means

00:07:38.139 --> 00:07:42.909
that if there's any errors or exceptions
in the API it will return a full stack

00:07:42.909 --> 00:07:47.560
trace on the screen this can reveal
secret information like the Django

00:07:47.560 --> 00:07:52.240
secret file or other things that may
make your server vulnerable so it's best

00:07:52.240 --> 00:07:56.420
practice to always disable this in
production

00:07:56.420 --> 00:07:57.969
Because we want to run

00:07:57.969 --> 00:08:01.779
debug mode when we run the server on our vagrant server but we want

00:08:01.779 --> 00:08:07.779
to disable it when we run it on the live
server we're going to add some logic

00:08:07.779 --> 00:08:11.940
here to pull in the configuration from
an environment variable

00:08:11.940 --> 00:08:25.360
so type bool int and then OS dot environ
dot get debug comma 1

00:08:25.360 --> 00:08:26.220
what this does

00:08:26.229 --> 00:08:32.380
is it pulls in the value of the
environment variable called debug we set

00:08:32.380 --> 00:08:36.729
this environment variable here in the
supervisor file so you can see in the

00:08:36.729 --> 00:08:40.360
supervisor at the top of the
configuration we set environment equals

00:08:40.360 --> 00:08:42.900
debug equals zero

00:08:42.900 --> 00:08:47.020
this sets the debug environment variable to zero when we run

00:08:47.020 --> 00:08:49.320
our application

00:08:49.320 --> 00:08:52.570
so when we pull in this debug variable

00:08:52.570 --> 00:08:58.240
we'll get a zero and then by default all
environment variables are strings so

00:08:58.240 --> 00:09:02.380
there's no way to specify an integer as
an environment variable so we need to

00:09:02.380 --> 00:09:08.740
use this int function to convert our
string of a 1 value or a 0 value to an

00:09:08.740 --> 00:09:14.340
integer of a 0 and then we use the bool
function to convert this to a boolean

00:09:14.340 --> 00:09:22.450
the way Python booleans work is a 0 will
convert into a false and a 1 will convert

00:09:22.450 --> 00:09:24.100
into a true

00:09:24.100 --> 00:09:27.880
That's why we have this one here because this is the default value

00:09:27.880 --> 00:09:33.580
if the debug setting doesn't exist so
when we run our application on our

00:09:33.580 --> 00:09:38.590
vagrant server we're not going to
specify debug equals zero so it's going

00:09:38.590 --> 00:09:43.630
to default to 1 which is going to be
converted to true that means when we run

00:09:43.630 --> 00:09:47.890
our server on our local machine it's
going to be in debug mode but then when

00:09:47.890 --> 00:09:52.917
it's running on our server debug mode is
going to be disabled

00:09:52.917 --> 00:09:54.400
the next change we

00:09:54.400 --> 00:10:01.120
need to do is add a static root to the
bottom of our configuration so type

00:10:01.120 --> 00:10:08.060
static underscore root equals static
forward slash

00:10:08.060 --> 00:10:09.610
the static root is the

00:10:09.610 --> 00:10:15.640
location where Django will store all of
the static files when we run our collect

00:10:15.640 --> 00:10:21.760
static command so make sure you save the
settings dot py file and then let's just

00:10:21.760 --> 00:10:26.770
open up the setup again and you can see
we run this collect static command now

00:10:26.770 --> 00:10:30.430
we need to tell Django where to store
the static files when we run this

00:10:30.430 --> 00:10:36.080
command we do this by setting the static
root

00:10:36.080 --> 00:10:37.720
okay so that's all the

00:10:37.720 --> 00:10:42.250
configuration changes we need to do we
need to

00:10:42.250 --> 00:10:48.910
run one more command to make sure that
these setup scripts are executable so

00:10:48.910 --> 00:10:55.750
when you send a file to a server the
file needs to have the permissions to be

00:10:55.750 --> 00:11:00.700
executable if you want to run it as an
executable script we can do that by

00:11:00.700 --> 00:11:05.980
opening up the terminal or the git bash
window making sure you're on our project

00:11:05.980 --> 00:11:16.500
and then running chmod +x deploy
forward slash star dot sh

00:11:16.500 --> 00:11:17.620
what this does

00:11:17.620 --> 00:11:23.590
is it runs the chmod command to set
executable to any file that ends in dot

00:11:23.590 --> 00:11:27.620
sh in our deployed directory

00:11:27.620 --> 00:11:31.240
so that is our setup and our update commands so hit

00:11:31.240 --> 00:11:37.060
enter and then we're going to commit our
changes to github so type git add dot

00:11:37.060 --> 00:11:44.170
and then get commit - a and then we're
going to add a doc string configured for

00:11:44.170 --> 00:11:46.720
deployment

00:11:46.720 --> 00:11:53.020
save the file and then push the changes up to git

00:11:53.020 --> 00:11:54.220
ok so that's all

00:11:54.220 --> 00:12:00.240
we need to do to configure our project
for deployment on our server

