While we're waiting for our server to start in AWS let's go ahead and configure
our project for deployment start by downloading the deployed dot zip file
that I added as a resource to this video this file contains a zip of all the
scripts and configuration files that we need to deploy our project we're going
to add the contents of this to our project and then I'm going to talk you
through what these scripts do
So once it's been downloaded unzip the deployed
dot zip file and copy the deploy directory into our profiles REST API
project
then open up the atom editor and I'm going to walk you through each of
the scripts in the deployed directory
so here we have a set up dot SH script this
is the script that we're going to use to set up our server when it's first
created
before we can run this we need to update the project git URL to the URL
of your github project
this is because the script is going to clone the
contents of our project to the server when we run it so we need to make sure
that this is correct with the project URL for our project so open up the
browser and open up github to our project and then we're going to click
this clone or download button and we want to make sure we are cloning with
HTTPS and we're going to copy the link of this HTTPS URL
then head over to the
atom editor and paste the contents in the project
git url variable
don't forget to save the file and I'm going to talk you
through what this script does so at the top of the script we set some variables
this is the URL of the github project which we've just set and then we have
the base path which is the directory we're going to store our project on the
server
we're going to store our project in USR slash local slash apps slash profiles
REST API this is where we're going to clone the project to the server and it's
where all of our source code is going to be kept
next we install some
dependencies which we need to run our application
on a server so we need Python Python 3 and then we need the Python 3 virtual
environment and we also need sqlite Python pip supervisor nginx and git
So git is going to be used to clone the project nginx is the webserver that is going to
serve the static files and act as a proxy to our uWSGI service that is
going to run in supervisor
and then of course we have Python pip which is the
package manager that we're going to use to install the packages and we have
sqlite which we're going to use for the database and then we have Python 3
virtual environment which is going to be used to set up our virtual environment
finally we have Python 3 dev which is going to be used to run Python on the
server
The next thing we do is we create a directory to the project URL or the
location on the server that we want to create or store the project files so
we need to create the project before we can clone it because the next thing we
do is we use git to clone the project from our git URL to the base location
where we want to store the code so this is going to do a git clone and pull the
source code down to the server
Next we create the virtual environment on the
server so this creates a new directory in our project forward slash env to create a virtual
environment for Python
Next we install the Python packages which are everything
within our requirements dot txt file and additionally this uWSGI which is a
Python daemon for running Python code as a webserver
Next we run the migrations
which we've done a number of times every time we change our migrations
or our models and then we run this collect static command collect static will
gather all of the static files for all of the apps in our project into one
directory
when you use the Django management server it does this
automatically for you but since we're not going to be using the Django
development server on production we need to create a location
with all of the static files that we can use to serve the CSS and JavaScript for
the Django admin and the Django rest framework browsable api
Next we're going
to configure the supervisor supervisor is a application on linux that allows
you to manage processes this is what's going to manage our Python process
or our uWSGI server
so what we do here is we copy the supervisor profiles
API configuration dot config file into the location on the server where the
supervisor is kept
then we run reread to update these supervisor configuration
files and then we run update to update all the processes next we restart our
profiles API to make sure that our service is started finally we configure
out nginx server this is the webserver that's going to be used to serve the
static files so we need to create a location for the configuration file and
we copy the configuration file that we've added here nginx profiles API
dot conf to the sites available directory in
nginx then we remove the default configuration that's provided when you
install nginx and we add a symbolic link from our sites available to our
sites enabled to enable our site
finally we restart the nginx service and then
the script exits with this message here saying done so that's a setup script
here we also have an update script which is going to be used to update the code
on the server whenever we make a change so the setup script will be used the
first time we set up the server but it will only work once once the server
setup you need to use the update script to pull updates or changes from git on
to the server
So again we have the project based path which is the location
on the server where the source code is going to be kept then we run git pull
and then we run migrate in our virtual environment and then we run the collect
static in case you've added any new dependencies that add anymore static
files and finally we restart our supervisor process to make the changes
come into effect
Ok so that's all we need to do for the deploy configuration
next we're going to make some changes to our settings file for it to run better
on the server
So open up settings dot py within the profiles project and the
first change we're going to make is we're going to disable this debug mode
it's best practice never to run the server in debug mode when it's publicly
accessible it's fine when you're running it on your local machine because it's
unlikely that anyone else is going to be able to connect to the server but you
should never run it when it's on a publicly accessible server because it
opens you up to vulnerabilities when you run the server in debug mode that means
that if there's any errors or exceptions in the API it will return a full stack
trace on the screen this can reveal secret information like the Django
secret file or other things that may make your server vulnerable so it's best
practice to always disable this in production
Because we want to run
debug mode when we run the server on our vagrant server but we want
to disable it when we run it on the live server we're going to add some logic
here to pull in the configuration from an environment variable
so type bool int and then OS dot environ dot get debug comma 1
what this does
is it pulls in the value of the environment variable called debug we set
this environment variable here in the supervisor file so you can see in the
supervisor at the top of the configuration we set environment equals
debug equals zero
this sets the debug environment variable to zero when we run
our application
so when we pull in this debug variable
we'll get a zero and then by default all environment variables are strings so
there's no way to specify an integer as an environment variable so we need to
use this int function to convert our string of a 1 value or a 0 value to an
integer of a 0 and then we use the bool function to convert this to a boolean
the way Python booleans work is a 0 will convert into a false and a 1 will convert
into a true
That's why we have this one here because this is the default value
if the debug setting doesn't exist so when we run our application on our
vagrant server we're not going to specify debug equals zero so it's going
to default to 1 which is going to be converted to true that means when we run
our server on our local machine it's going to be in debug mode but then when
it's running on our server debug mode is going to be disabled
the next change we
need to do is add a static root to the bottom of our configuration so type
static underscore root equals static forward slash
the static root is the
location where Django will store all of the static files when we run our collect
static command so make sure you save the settings dot py file and then let's just
open up the setup again and you can see we run this collect static command now
we need to tell Django where to store the static files when we run this
command we do this by setting the static root
okay so that's all the
configuration changes we need to do we need to
run one more command to make sure that these setup scripts are executable so
when you send a file to a server the file needs to have the permissions to be
executable if you want to run it as an executable script we can do that by
opening up the terminal or the git bash window making sure you're on our project
and then running chmod +x deploy forward slash star dot sh
what this does
is it runs the chmod command to set executable to any file that ends in dot
sh in our deployed directory
so that is our setup and our update commands so hit
enter and then we're going to commit our changes to github so type git add dot
and then get commit - a and then we're going to add a doc string configured for
deployment
save the file and then push the changes up to git
ok so that's all
we need to do to configure our project for deployment on our server