Your application is developed and tested, time to bring it online. This tutorial explains how to do that.
The site that we will deploy is this site itself, pythoneatstail.com. We have created and tested it in previous tutorials. For the deployment it is not important that it is based on Wagtail, however we will use some Django related functionality. We will deploy on DigitalOcean, which has a complete tutorial for this and provides a number of other good tutorials; we are also indebted to the tutorial of SimpleIsBetterThanComplex. In this tutorial we will have the following steps:
This will enable us to run our project locally on the DigitalOcean server. In a follow-up tutorial we will install Gunicorn and Nginx to serve our application to the internet, as well as install Supervisor to manage both processes.
I will limit myself to a quick overview, since the provider makes it pretty easy; additionally the SimpleIsBetterThanComplex tutorial explains it well. Go to digitalocean.com, Products, Droplets, create an account. You will be given the opportunity to name your project. Try to choose a way of naming things that works for you. In the beginning you might get confused with the names of:
After signing in with your new account, go to your project and click on Create to create a droplet. You need to choose a plan, the amount of storage you need (and the price that comes with it) and the datacenter region. For authentication choose Password, unless you have SSH set up; we will do that later. Choose a name for the droplet; we'll name ours drp-pet. Click 'Create Droplet' to finish the process. You will receive an email from DigitalOcean with an IP-address, the username 'root' and a root password.
The droplet should be visible in your project overview. on DigitalOcean. On the right there are three dots that open a menu; click on "Add Domain". To be clear: DigitalOcean is not a domain registrar, so does not register your domain. There are hundreds if not thousands of domain registrars, such as domain.com, Bluehost, HostGator, GoDaddy etc. In our case the domain pythoneatstail.com has been registered with one of them. We now need to tell that registrar that DigitalOcean is going to manage it. DigitalOcean provides instructions for many registrars how to update your settings at the registrar. For most registrars this will amount to renaming the first three nameservers to ns1.digitalocean.com, ns2.digitalocean.com and ns3.digitalocean.com and saving them. As mentioned by DigitalOcean, it can take several hours or even longer before these changes have propagated.
Now in the DigitalOcean control panel, click on the three dots next to your droplet, click on 'Add Domain', fill in your domain and click 'Add Domain'. This will connect the domain to your droplet. The domain should now be visible in the control panel. If you click on it, there should be an A record with your IP-address. Also add an A record with the www. subaddress.
Go to your terminal and connect to the IP-address you received by mail with the
Use the password you received; you will be asked to change it the first time. You are now in the home directory of the root user. Get familiar with the file organization:
/home for home directories of ordinary users,
/etc for configuration files,
/bin for binary files and so on. We'll create a user for our project, because we don't want to execute all commands as a root user. At the prompt type:
and create a password for
usr_pet. Because the user will need to install a number of packages, it needs superuser privilege. Therefore add the user to the sudo group with the
usermod -aG sudo usr_pet
As part of initial setup it is also recommended to add public key authentication and a firewall, but we will do that later. From now on we can directly log in as
usr_pet. Log out of the server connection with
logout, then access again with
and we arrive at the home directory of
Installing and managing software is done with the Linux command
apt-get. Log in at the server as
usr_pet. To install all necessary software components for PostgreSQL type:
sudo apt-get update sudo apt install postgresql postgresql-contrib
psql as the default PostgreSQL user
sudo -u postgres psql
Create a database and a database user for the project, just as we have done locally on our development computer before:
CREATE USER usr_pet WITH PASSWORD '123'; CREATE DATABASE db_pet OWNER usr_pet;
You should obviously use a better password. Note that we have given the database user the same name as the server user; this is not necessary but convenient. Django recommends to add a few settings:
ALTER ROLE usr_pet SET client_encoding TO 'utf8'; ALTER ROLE usr_pet SET default_transaction_isolation TO 'read committed'; ALTER ROLE usr_pet SET timezone TO 'UTC';
Exit psql with
First install the
python3-dev (needed to build Python extensions) and
python3-venv (needed to create a virtual environment):
sudo apt install python3-pip python3-dev python3-venv
We will set up a virtual environment in the home directory of
usr_pet using the default Python command, and activate it:
python3 -m venv env source env/bin/activate
Note that we created the environment in our home directory; if you intend to have more environments for this user, create it in a subdirectory. Continue with cloning your project using the 'clone or download' link from Github:
git clone https://github.com/prijme/pet.git
Enter the directory of the project (in our case
pet) and install all requirements (this includes Django, Wagtail and psycopg2):
pip3 install -r requirements.txt
We put all our password related and other sensitive settings in a
local.py file that is not tracked, so we need to create that in our
settings directory. Go there, create the file
local.py and copy the contents of the
local.py file on your development computer. Note: the username and password of your database settings should obviously match the ones you have created above on your server!
Now we can migrate our database and create a superuser. Go in the project directory and type:
python3 manage.py makemigrations python3 manage.py migrate python3 manage.py createsuperuser
Supply a username and password for the superuser. Now we can check that the database is functioning by running the Django development server on
python3 manage.py runserver 0.0.0.0:8000
Now in your browser go to the IP-address of your server, followed by
:8000, in our case http://220.127.116.11:8000. You should see a welcome page, which might not say more than 'Home'. By appending
/admin to the address and using the superuser credentials you can log in to admin.
The project is now running on the server. In our next tutorial we are going to serve our site to the internet using Gunicorn, Nginx and Supervisor.
Comment on this article (sign in first or confirm by name and email below)