In the previous tutorial, Deploy Django to DreamCompute on Ubuntu with Apache – Part 2, we got PuTTY and FileZilla set up to be able to access our Instance’s Shell and SFTP.

Throughout the remainder of this tutorial, we’ll be focusing heavily on the Shell terminal using PuTTY. If you have not already, go back to Part 2 and get PuTTY set up.

Installing The Packages

The very first thing we’d want to do after we boot up and connect to our Shell for the first time is updating our local package index. To do this run the following command:

ubuntu@instance_name:~$ sudo apt-get update

Once our local package index is updated, the next step is to check which python version we are running. Run the following:

ubuntu@instance_name:~$ python3 -V

At the time of writing this tutorial that latest that should be running on the Instance is Python 3.6.7.

Note: To use a different version of python than the one above, you would have to download and compile your chosen version of python. More information is in the DreamHost documentations here.

Once you have the correct version of python running, we need to install pip. To install pip run the following:

ubuntu@instance_name:~$ sudo apt install python3-pip

Finally, the last package we would need to install is our virtual environment. We can install that running:

ubuntu@instance_name:~$ sudo apt install python3-venv

You’ll then be prompt again to say Yes or No to continue. Type Y and press Enter.

Setting Up The Virtual Environment

With our packages now installed, we can move onto creating our virtual environment.

First, we’d want to run two commands. One is to create a new directory in our root directory. I like to name mine after the domain I’m running my application on to keep things organized. Ex.

The second command will tell the Shell that we want to go into the directory we’ve just created.

ubuntu@instance_name:~$ mkdir ~/
ubuntu@instance_name:~$ cd ~/

Once inside the directory, we can create our virtual environment by running the following command:

ubuntu@instance_name:~/$ python3.6 -m venv env_name

Where env_name is the name of your virtual environment, to keep things simple, I like to call mine env or venv.

The last step is to start our virtual environment. To start it all we need to do is enter the following:

ubuntu@instance_name:~/$ source /env_name/bin/activate

You should now see your terminal output the following:

(env_name) ubuntu@instance_name:~/$

To deactivate our virtual environment, all we need to do is type in:

(env_name) ubuntu@instance_name:~/$ deactivate

Installing Django

To install Django, we first must make sure we are in our virtual environment. You’ll be able to tell if you are because the env_name will be in parentheses in front of the username@instance_name.

(env_name) ubuntu@instance_name:~/$ pip3 install django

Running the command above will install the latest version of Django. You can run the following to check the Django version:

(env_name) ubuntu@instance_name:~/$ django-admin --version

At the time of writing this tutorial, the latest version is 2.2.1.

Creating a New Project

From this point, all we would need to do is create our new Django project and set up the appropriate configurations to run it when we install Apache successfully.

In the terminal run the following to create a new Django project:

(env_name) ubuntu@instance_name:~/$ django-admin startproject projectname

Where projectname is the name of your Django project, keep track of this as we will need this later for when we are configuring our Apache server.

Note that you should still be in your directory and have your virtual environment active.

Configuring The Project

Allowed Hosts and Database

Before we move onto making our new Django project publicly available for viewing, we now have to set up some appropriate configurations to establish our Allowed Hosts and Database settings.

To make these changes, we first need to set up a MySQL database if you haven’t done so already. This tutorial already assumes you have a database created, especially if you are using a different type other than MySQL. To create a new MySQL database, you can follow this guide here on DreamHost.

When this is ready, we can edit our with the following commands:

(env_name) ubuntu@instance_name:~/$ cd projectname/projectname
(env_name) ubuntu@instance_name:~/$ sudo nano

The above will open up our file. Locate ALLOWED_HOST = [] and enter the following:


Where is the domain you are deploying your application to. You can include multiple domains or IP addresses by separating them with a comma.

Next, find DATABASES. You can delete everything here and use the following template for connecting to a MySQL database.

    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.mysql',
        'NAME': 'db_name',
        'USER': 'db_user',
        'PASSWORD': 'password',
        'HOST': 'host_url',
        'PORT': '3306',
        'OPTIONS': {
            'sql_mode': 'traditional',

Replace db_name with your database name, db_user with your database user, password with your database password, and host_url with your database host URL.

Note that the ENGINE will vary based on the database type you are using.

Once your edits are made, you can press Ctrl + X to exit, Press Y to save, and press Enter to save the current file.

Static and Media Files

Configuring our Static and Media Settings

On top of the changes above, if we wanted to serve static files for our Django project as well as media files, there are a couple more things we’d need to do.

First, go back into the Then scroll down until you see the STATIC_URL. You can replace the section with the following:

# Static files (CSS, JavaScript, Images)

STATIC_URL = '/static/'
STATIC_ROOT = os.path.dirname(BASE_DIR) + '/public/static/'

# Media files

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.dirname(BASE_DIR) + '/public/media/'

You can choose how you’d like to organize your static and media folder. I prefer to keep my static and media files in a public directory outside of my Django directory to keep things organized.

Once edited, you can press Ctrl + X to exit, Press Y to save, and press Enter to save the current file.

Granting Permissions to Our Media Directory

To allow our Django project to upload files to the media folder, we would now have to set up some permissions.

First, go to the root of the directory where the projectname and public directory is stored. Then enter the following command to create a group for our web services users and add www-data, which is the server making Django request.

(env_name) ubuntu@instance_name:~/$ cd ~/
(env_name) ubuntu@instance_name:~/$ sudo groupadd varwwwusers
(env_name) ubuntu@instance_name:~/$ sudo adduser www-data varwwwusers

Next, we want to change the ownership of the public folder and change the folder policy. To do that run the following:

(env_name) ubuntu@instance_name:~/$ sudo chgrp -R varwwwusers public
(env_name) ubuntu@instance_name:~/$ sudo chmod -R 770 public

Go into the public directory and do the same thing with the media folder with the following:

(env_name) ubuntu@instance_name:~/$ cd public
(env_name) ubuntu@instance_name:~/$ sudo chgrp -R varwwwusers media
(env_name) ubuntu@instance_name:~/$ sudo chmod -R 770 media

Finally, all that’s left is to add ubuntu to the group we’ve created by running the following:

(env_name) ubuntu@instance_name:~/$ sudo usermod -a -G varwwwusers ubuntu

Collecting Our Static Files

With our Static and Media directories now set up, let collect our static files. To do that, we must first navigate to the projectname folder and run the following:

(env_name) ubuntu@instance_name:~/$ cd ../
(env_name) ubuntu@instance_name:~/$ cd projectname
(env_name) ubuntu@instance_name:~/$ python3 collectstatic

Our static files should now be inside our public/static directory.

Migrating The Database

Lastly, to finish setting up our new Django project, we just need to migrate the database.

To migrate our database successfully, we first need to install a MySQL client package. The package may vary depending on your database type. Run the following command:

(env_name) ubuntu@instance_name:~/$ pip3 install mysqlclient

This will install the mysqlclient package we need to create our tables in our MySQL database.

Once installed, run the migrate command.

(env_name) ubuntu@instance_name:~/$ python3 migrate

Our tables are now on the database. Additionally, you can also run the makemigrations command before the migrate command to check for any migrations.

Checking If The Project Runs

To check if we’ve successfully installed and set up our Django project, run the runserver command.

(env_name) ubuntu@instance_name:~/$ python3 runserver

If everything runs successfully, you can press Ctrl + C to stop it. We are now ready to install Apache.

Note: Now that we’ve verified everything is working correctly, you can upload your actual Django project via SFTP. Just remember to perform your migrations and run the runserver command to ensure everything is working correctly.


My name is Tony, and I’m an Experience Designer with 8+ years of experience in design and development. At heart, I am a developer first and a designer second. I enjoy creating interactive experiences, but I also enjoy designing and learning about the user’s experiences.

Write A Comment