Deploying a Django Project, Part 2: Setting Up the Server - Practicality Beats Purity

Deploying a Django Project, Part 2: Setting Up the Server

In Part 2 of this tutorial series, we will continue to deploy our Django project to Digital Ocean using an Ubuntu server. In this part, we will log in to our server and begin configuring it, setting up an admin-level user, and installing some basic dependencies. If you are following along with this tutorial, you should already have created a Droplet on Digital Ocean and set up an SSH key to access it. If not, see Part 1 to learn how to create a Droplet.


Log In to the Server and Create Admin-Level User

In order to log in to your server, you need its public IP address. From your Digital Ocean dashboard, click the Droplets tab in the sidebar, where you should see your Droplet and its IP address.


Droplet IP address


To log in to your Droplet, i.e. your server, open up a terminal (or command-line or Powershell for Windows users). Type in the following command to log in as the root user.

ssh root@<your_server_IP_address>

It will prompt you with a warning about the host's authenticity. Type in "yes" to connect. If you have a passphrase for your SSH key, you will have to enter it in now.

You should now be logged in and should see the following:


We could technically continue with the rest of the development process as the root user, but due to its heightened privileges, it is not recommended. The best practice is to create an admin-level user with sudo privileges, which we will use moving forward.

In this series, I will use the name example_user, but you should replace that with whatever username you prefer. When you see example_user throughout this series, replace it with your own username. Create your user with the following command:

# adduser example_user

You will be prompted to create a password. Make sure it is a strong one! Note that your terminal will not show that you are typing anything in, but it is registering your keystrokes. Press enter and retype the password to confirm it.

It will also prompt you to fill out your user's name, room number, phone, et cetera, but these are optional. You can leave them blank or fill them out as you like. Finally, type y and press enter to create your user.

We need administrative privileges for our user so that we can run admin commands, which are preceded by the sudo keyword. We can grant our user these privileges by adding them to the sudo group with the following command. Make sure to always note the casing! Lowercase a and uppercase G.

# usermod -aG sudo example_user


Enabling SSH Access for Your User

Before leaving the root account, we need to set up SSH access for our new user. On Ubuntu, we can use the UFW firewall to manage connections for security. Check the UFW status by typing:

# ufw app list

You should see the following:

Available applications:

To allow SSH connections for our user and enable the firewall, type the following:

# ufw allow OpenSSH
# ufw enable

Confirm by typing y and the firewall will be active. Double-check by typing:

# ufw status

You should see something like the following output:

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)

Our firewall is now restricting all connections other than SSH, but our new user does not yet have our public SSH key. To resolve this, we will copy root's SSH directory to our new user and change the file owner. We can do this with the following command. Remember to change example_user with your own username!

# rsync --archive --chown=example_user:example_user ~/.ssh /home/example_user

We can now log in as our new user. Before logging out of root however, it is not a bad idea to open up a new terminal window to log in with your new user. That way you are still logged in as root if you have any problems connecting. Once you have connected successfully, you can log out of the root account in your original terminal window by typing in exit

# exit


Log In and Install Basic Requirements

To log in to your new user's account, type in:

ssh example_user@<your_server_IP_address>

Now that you are ready to go with an admin-level user, you can begin installing the necessary packages and dependencies. First, update the local apt package index files with the following command:

$ sudo apt update

Once that is done, you can install the packages we will need for the rest of the deployment process. Type in:

$ sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx curl

If you are using Python 2 instead of Python 3, type the same command as above, but just omit the 3 after python in both instances.

Well done! You have now set up your server to host your Django project. In Part 3, we will continue the deployment process by configuring our project settings, saving our sensitive information in an environment file, and setting up a PostgreSQL database for our project. You can find Part 3 here.


Full Deploying a Django Project tutorial series:

Part 1: Creating and Managing a Custom Domain Name

Part 2: Setting Up the Server

Part 3: Configuring Project Settings and Creating a PostgreSQL Database

Part 4: Clone Your Project onto the Server

Part 5: Launch Your Project with Gunicorn and NGINX

Part 6: SSL/TLS Certificate and Continuing Development


Additional resources:

complove5167 November, 6, 2020: 4:48

Very helpful.  Thanks

Add a new comment