Creating a Python App using the Django framework

From the cPanel SOFTWARE section, click the Setup Python App icon.

Click the + CREATE APPLICATION button. For this tutorial, we are going to create a simple application using the DJANGO framework. Complete the fields.

In our example, we will install the application in /home/krystald/myapp, and serve it from, using Python version 3.7.

Once the application has been created, you will be shown the following information, which includes how to switch to the virtual environment for your python application from the command line.

Log into your cPanel account via SSH.

We have separate guides for using SSH for Windows (link opens in a new window) and SSH for Mac (link opens in a new window).

Enter the virtual environment command line above.

source /home/krystald/virtualenv/myapp/3.7/bin/activate && cd /home/krystald/myapp

You'll see a response something like:

(myapp:3.7)[krystald@server myapp]$

You will notice that the command prompt changes, and is now prefixed with (myapp:3.7). This tells us that our shell environment is now setup for our application's own python installation. List the files in the current directory, using the following command:

(myapp:3.7)[krystald@server myapp]$ ls -la

which should look like this:

total 20
drwxr-xr-x 4 krystald krystald 4096 Oct 16 08:18 .
drwx--x--x 26 krystald krystald 4096 Oct 16 08:18 ..
-rw-r--r-- 1 krystald krystald 333 Oct 16 08:18
drwxr-xr-x 2 krystald krystald 4096 Oct 16 08:18 public
drwxr-xr-x 2 krystald krystald 4096 Oct 16 08:18 tmp

If we browse to our URL, in this case, we will see a simple message giving the Python version.

It works!

Python v3.7.3

This is because cPanel created a basic application inside, This file is used by Passenger, a web application server that uses the WSGI (Web Server Gateway Interface) convention to provide the glue between your python application and our webservers.

Now, install the DJANGO framework - As of 16/10/2019, the latest version of DJANGO requires a later version of sqlite3 than that which ships with Centos 7, so we are not installing the most current version.

pip install Django==2.1.*

You'll see a response something like:

Collecting Django==2.1.*
Downloading (7.3MB)
|████████████████████████████████| 7.3MB 5.4MB/s
Requirement already satisfied: pytz in /home/krystald/virtualenv/myapp/3.7/lib/python3.7/site-packages (from Django==2.1.*) (2019.3)
Installing collected packages: Django
Successfully installed Django-2.1.13

Now we will create an empty DJANGO project. This needs to be done outside of our application path, /home/krystald/myapp, because the project creates an unnecessary parent directory. Enter the following commands.

cd ~/ 
django-admin startproject dproj
cp -a dproj/. myapp/
rm -rf dproj
cd myapp

This will create the basic files needed to establish the DJANGO application and move them into your Python application directory, which should now have a structure like this:

|-- dproj
| |--
| |--
| |--
| `--
`-- public

We can test the installation is working

python myproject/ runserver

You'll see a response something like this:

Performing system checks...

System check identified no issues (0 silenced).

You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python migrate' to apply them.

October 16, 2019 - 07:34:52
Django version 2.1.13, using settings 'myproject.settings'
Starting development server at
Quit the server with CONTROL-C.

Press Ctrl-c to exit. Now we need to make some minor configuration changes to get the DJANGO framework running. Firstly, we should address the unapplied migrations reported above:

python migrate

You'll see a response something like:

Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying sessions.0001_initial... OK

To avoid a DisallowedHost error, we need to ensure that DJANGO knows the hostname our site is going to use. We do this by editing the file.

nano dproj/

and change the ALLOWED_HOSTS entry to include your domain name, as follows:


Ctrl-x and y to exit, saving changes.

Next, we need to tell Passenger where to find the application start point.


Edit the contents as follows:

import imp
import os
import sys

sys.path.insert(0, os.path.dirname(__file__))

wsgi = imp.load_source('wsgi', 'dproj/')
application = wsgi.application

Again, Ctrl-x and y to exit, saving changes.

At this point, you should be able to load (in this case) which will return a Page not found (404) error. This is normal because no application URLs have yet been added.

If all has gone well, should now display the Django administration screen.

How did we do?

Powered by HelpDocs (opens in a new tab)
© Krystal Hosting Ltd 2003–2019