NEW

Deploy Django to DreamCompute on Ubuntu with Apache – Part 5

In the final part of this tutorial, we’ll talk about how to set up a secure HTTPS connection on Apache with an SSL certificate from Let’s Encrypt.

Deploy Django to DreamCompute on Ubuntu with Apache – Part 4

In the previous part of this tutorial, we set up and configured Django. With everything now set up, it is time to install and configure Apache.

Deploy Django to DreamCompute on Ubuntu with Apache – Part 3

In the previous tutorial we got PuTTY and FileZilla set up. In this tutorial, we’ll be focusing on using the Shell terminal to get Django installed and set up.

Deploy Django to DreamCompute on Ubuntu with Apache – Part 2

In the first part of this tutorial, we set up a DreamCompute Instance. In this part, we’ll walk through connecting to the system Shell and SFTP.

Deploy Django to DreamCompute on Ubuntu with Apache – Part 1

In this five-part tutorial, I’m going to show you how to deploy a Django application to DreamCompute on Ubuntu with Apache and an SSL certificate.

The Google Pop-up Experience

When I've first heard about Google opening up a hardware store in Chicago's Bucktown during mid-October, I was very excited. I've been following Google's development for quite some time now in the industry. I, myself, am a big fan of smart devices and find the concept...

VO Manager Pro – Released

You can now purchase the VO Manager Pro on the Unity Asset Store at http://u3d.as/1ee5 for $14.99!

View My Map

View My Map is an online web application that gathers Facebook user’s checked-in location data and places them on a Google Map. The app also allows the user to export screenshots of their map as well as share them online via a generated link.

Balancing School and Work

It’s no joke when it comes to balancing school and work. It can be difficult, and I have a hard time myself. How do any of us do it? Sheer willpower of course! As true as that may sound, there’s more to it than sheer willpower.

Deploy Django to DreamCompute on Ubuntu with Apache – Part 3

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. domain.com.

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

ubuntu@instance_name:~$ mkdir ~/domain.com
ubuntu@instance_name:~$ cd ~/domain.com

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

ubuntu@instance_name:~/domain.com$ 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:~/domain.com$ source /env_name/bin/activate

You should now see your terminal output the following:

(env_name) ubuntu@instance_name:~/domain.com$

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

(env_name) ubuntu@instance_name:~/domain.com$ 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:~/domain.com$ 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:~/domain.com$ 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:~/domain.com$ 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 domain.com 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 settings.py with the following commands:

(env_name) ubuntu@instance_name:~/domain.com$ cd projectname/projectname
(env_name) ubuntu@instance_name:~/domain.com/projectname/projectname$ sudo nano settings.py

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

ALLOWED_HOSTS = ['domain.com']

Where domain.com 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.

DATABASES = {
    '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 settings.py. Then scroll down until you see the STATIC_URL. You can replace the section with the following:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

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 domain.com 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:~/domain.com/projectname/projectname$ cd ~/domain.com
(env_name) ubuntu@instance_name:~/domain.com$ sudo groupadd varwwwusers
(env_name) ubuntu@instance_name:~/domain.com$ 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:~/domain.com$ sudo chgrp -R varwwwusers public
(env_name) ubuntu@instance_name:~/domain.com$ 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:~/domain.com$ cd public
(env_name) ubuntu@instance_name:~/domain.com/public$ sudo chgrp -R varwwwusers media
(env_name) ubuntu@instance_name:~/domain.com/public$ 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:~/domain.com/public$ 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:~/domain.com/public$ cd ../
(env_name) ubuntu@instance_name:~/domain.com$ cd projectname
(env_name) ubuntu@instance_name:~/domain.com/projectname$ python3 manage.py 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:~/domain.com/projectname$ 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:~/domain.com/projectname$ python3 manage.py 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:~/domain.com/projectname$ python3 manage.py 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.

Previous

Next

Submit a Comment

Your email address will not be published. Required fields are marked *